Java OOPS Design Principles

1) Composition Vs Aggregation

When studying UML , one of the most important questions which comes to mid of a programmer is , What is the difference between Aggregation and Composition ?

Composition implies that the child objects share a lifespan with the parent. Aggregation doesn't. For example, a chess board is composed of chess squares - the chess squares don't really exist without the board. However, a car is an aggregation of parts - a car exhaust is still a car exhaust if it's not part of a car at the time.



Composition and Aggregation are types of associations. They are very closely related and in terms of programming there does not appear much difference.  I will try to explain the difference between these two by java code examples
Aggregation: the object exists outside the other, is created outside, so it is passed as an argument (for example) to the construtor. Ex: People – car. The car is create in a different context and than becomes a person property.
Composition: the object only exists, or only makes sense inside the other, as a part of the other. Ex: People – heart. You don’t create a heart and than passes it to a person.
Code example for aggregation:
// WebServer is aggregated of a HttpListener and a RequestProcessor
public class WebServer {
private HttpListener listener;
private RequestProcessor processor;
public WebServer(HttpListener listener, RequestProcessor processor) {
   this.listener = listener;
   this.processor = processor;
  }
}
Code example for composition
// WebServer is an composition of HttpListener and RequestProcessor and controls  //their lifecycle
public class WebServer {
private HttpListener listener;
private RequestProcessor processor;
public WebServer() {
   this.listener = new HttpListener(80);
   this.processor = new RequestProcessor(“/www/root”);
  }
}
In composition, whole has responsibility of preventing garbage collection of part.

2) Composition over Inheritance

composition over inheritance as it is more malleable / easy to modify later, but do not use a compose-always approach. With composition, it's easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type.. So the goose is more or less cooked once you derive from Class A.
My acid test for the above is:
  • Does TypeB want to expose the complete interface (all public methods no less) of TypeA such that TypeB can be used where TypeA is expected? Indicates Inheritance.
e.g. A Cessna biplane will expose the complete interface of an airplane, if not more. So that makes it fit to derive from Airplane.
  • Does TypeB only want only some/part of the behavior exposed by TypeA? Indicates need for Composition.
e.g. A Bird may need only the fly behavior of an Airplane. In this case, it makes sense to extract it out as an interface / class / both and make it a member of both classes.
Inheritance
This encourages the use of classes. Inheritance is one of the three tenets of OO design (inheritance, polymorphism, encapsulation).
class Person {
   String Title;
   String Name;
   Int Age
}

class Employee : Person {
   Int Salary;
   String Title;
}
This is inheritance at work. The Employee "is a" Person or inherits from Person. All inheritance relationships are "is-a" relationships. Employee also shadows the Title property from Person, meaning Employee.Title will return the Title for the Employee not the Person.

Composition

Composition is favoured over inheritance. To put it very simply you would have:
class Person {
   String Title;
   String Name;
   Int Age;

   public Person(String title, String name, String age) {
      this.Title = title;
      this.Name = name;
      this.Age = age;
   }
}
class Employee {
   Int Salary;
   private Person person;

   public Employee(Person p, Int salary) {
       this.person = p;
       this.Salary = salary;
   }
}

Person johnny = new Person ("Mr.", "John", 25);
Employee john = new Employee (johnny, 50000);
Composition is typically "has a" or "uses a" relationship. Here the Employee class has a Person. It does not inherit from Person but instead gets the Person object passed to it, which is why it "has a" Person.

Composition over Inheritance

Now say you want to create a Manager type so you end up with:
class Manager : Person, Employee {
   ...
}
This example will work fine, however, what if Person and Employee both declared Title? Should Manager.Title return "Manager of Operations" or "Mr."? Under composition this ambiguity is better handled:
Class Manager {
   public Title;
   public Manager(Person p, Employee e)
   {
      this.Title = e.Title;
   }
}
The Manager object is composed as an Employee and a Person. The Title behaviour is taken from employee. This explicit composition removes ambiguity among other things and you'll encounter fewer bugs.

3) Association Vs Aggregation Vs Composition

Association represents the ability of one instance to send a message to another instance. This is typically implemented with a reference instance variable, although it might also be implemented as a method argument, or the creation of a local variable.

Aggregation [...] is the typical whole/part relationship. This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (i.e. a part cannot contain its whole).

Composition [...] is exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'. This control may be direct or transitive. That is, the 'whole' may take direct responsibility for creating or destroying the 'part', or it may accept an already created part, and later pass it on to some other whole that assumes responsibility for it.

Java Code Review

This is one of most important aspect of quality software development.  'Code Review' question is generally asked for senior java developer or java team lead position. This is very important task performed by team lead to confirm the coding standard of application.
  • Adherence to standards and guidelines (This means you should have coding standards set for your project)
  • Documentation. All good code must be documented cleanly.
  • repetitive code (candidate for refactoring), usage of OOPS principle
  • meaningful variable names
  • variable and method scopes (private vs public)
  • Use of good design patterns (if possible)
  • Good test cases associated with the code. Preferably automated test cases.(it is not easy to automate all test cases, though it is desirable. If such is the case, it should document how to test the given functionality)
  • Closing all the Streams in finally block
  • Exception handling. We prefer to use the Logger to log instead of ex.printStackTrace() in our company
  • Remove unused methods/variables
  • Look for potential cause of NullPointerException
In addition since you spoke about Java, there are many tools available allow you to analyze the code and check for style and code cleanliness and even potential issues.
All of these can be configured with an automated build/continuous integration process and check for issues even before you go through a manual code review. I used PMD and Checkstyle at my last project and I was pleasantly surprised that how many issues were we able to discover and fix even before we sat down for a code review.

Spring Boot



Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Provide a radically faster and widely accessible getting started experience for all Spring development.
Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration).
Absolutely no code generation and no requirement for XML configuration.







AutoConfiguration
>Embedded Servlet Container , Datsource, JDBCTemplate, JPA
ØBatch Processing : Event & async processing
Ø


Actuator –
 
Non functional features – MVC endpoints
Security,
Audit,
Metrics
  & trace  


Sample POM-


xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-crud-with-vaadin</artifactId>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>7.6.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project> 


Example

package com.example.mvc;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/hello-world")
public class HelloWorldController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping(method=RequestMethod.GET)
    public @ResponseBody Greeting sayHello(@RequestParam(value="name", required=false, defaultValue="Stranger") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

}

package com.example.mvc;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

}


RestController



package com.example.rest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @RequestMapping("/thing")
    
    public MyThing thing() {
            return new MyThing("Vikalp");
    }

}



package com.example.rest;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MyThing {
public MyThing()
{
}

public MyThing(String name){
this.name = name;
}
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


Boot Application



package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class);
}

@Bean
public CommandLineRunner loadData(CustomerRepository repository) {
return (args) -> {
// save a couple of customers
repository.save(new Customer("Jack", "Bauer"));
repository.save(new Customer("Chloe", "O'Brian"));
repository.save(new Customer("Kim", "Bauer"));
repository.save(new Customer("David", "Palmer"));
repository.save(new Customer("Michelle", "Dessler"));

// fetch all customers
log.info("Customers found with findAll():");
log.info("-------------------------------");
for (Customer customer : repository.findAll()) {
log.info(customer.toString());
}
log.info("");

// fetch an individual customer by ID
Customer customer = repository.findOne(1L);
log.info("Customer found with findOne(1L):");
log.info("--------------------------------");
log.info(customer.toString());
log.info("");

// fetch customers by last name
log.info("Customer found with findByLastNameStartsWithIgnoreCase('Bauer'):");
log.info("--------------------------------------------");
for (Customer bauer : repository
.findByLastNameStartsWithIgnoreCase("Bauer")) {
log.info(bauer.toString());
}
log.info("");
};
}

}




package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.vaadin.annotations.Theme;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

/*
 * The UI code (and the Servlet declaration) used by the application stub can be found
 *  in the MyUI.java file. Let us read it through to see how it works. The init() 
 *  method of a UI class is triggered when a user enters your web application.
 *   The VerticalLayout is one of the most used layout components, 
 *   which are used to position and display other Vaadin components in your UI classes.
 *    The example code creates one TextField to allow the user to input her name and a 
 *    Button whose click listener dynamically adds a new Label component to the main layout.
 *     In the end of the init() method, we just configure the main layout and place
 *      components into it and set it to be the content of MyUI.
*  To test your first Vaadin application, right-click on the project and choose Debug as
 ▸ Maven build…​. The debug mode is slightly slower than the basic run mode, but it often 
 helps you to figure out what is happening in your application.
 */

@SpringUI
@Theme("valo")
public class VaadinUI extends UI {

private final CustomerRepository repo;

private final CustomerEditor editor;

private final Grid grid;

private final TextField filter;

private final Button addNewBtn;

@Autowired
public VaadinUI(CustomerRepository repo, CustomerEditor editor) {
this.repo = repo;
this.editor = editor;
this.grid = new Grid();
this.filter = new TextField();
this.addNewBtn = new Button("New customer", FontAwesome.ALIGN_CENTER);
}

@Override
protected void init(VaadinRequest request) {
// build layout
HorizontalLayout actions = new HorizontalLayout(filter, addNewBtn);

VerticalLayout mainLayout = new VerticalLayout(actions, grid, editor);
setContent(mainLayout);

// Configure layouts and components
actions.setSpacing(true);
mainLayout.setMargin(true);
mainLayout.setSpacing(true);

grid.setHeight(300, Unit.PIXELS);
grid.setColumns("id", "firstName", "lastName");

filter.setInputPrompt("Filter by last name");

// Hook logic to components

// Replace listing with filtered content when user changes filter
filter.addTextChangeListener(e -> listCustomers(e.getText()));

// Connect selected Customer to editor or hide if none is selected
grid.addSelectionListener(e -> {
if (e.getSelected().isEmpty()) {
editor.setVisible(false);
} else {
editor.editCustomer((Customer) grid.getSelectedRow());
}
});

// Instantiate and edit new Customer the new button is clicked
addNewBtn.addClickListener(e -> editor.editCustomer(new Customer("", "")));

// Listen changes made by the editor, refresh data from backend
editor.setChangeHandler(() -> {
editor.setVisible(false);
listCustomers(filter.getValue());
});

// Initialize listing
listCustomers(null);
}

// tag::listCustomers[]
private void listCustomers(String text) {
if (StringUtils.isEmpty(text)) {
grid.setContainerDataSource(new BeanItemContainer(Customer.class, repo.findAll()));
} else {
grid.setContainerDataSource(
new BeanItemContainer(Customer.class, repo.findByLastNameStartsWithIgnoreCase(text)));
}
}
// end::listCustomers[]

}



package com.example;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CustomerRepository extends JpaRepository {

List findByLastNameStartsWithIgnoreCase(String lastName);
    
}





package com.example;

import org.springframework.beans.factory.annotation.Autowired;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.event.ShortcutAction;
import com.vaadin.server.FontAwesome;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.UIScope;
import com.vaadin.ui.Button;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;

/**
 * A simple example to introduce building forms. As your real application is
 * probably much more complicated than this example, you could re-use this form in
 * multiple places. This example component is only used in VaadinUI.
 *
 * In a real world application you'll most likely using a common super class for all your
 * forms - less code, better UX. See e.g. AbstractForm in Virin
 * (https://vaadin.com/addon/viritin).
 */
@SpringComponent
@UIScope
public class CustomerEditor extends VerticalLayout {

private final CustomerRepository repository;

/**
* The currently edited customer
*/
private Customer customer;

/* Fields to edit properties in Customer entity */
TextField firstName = new TextField("First name");
TextField lastName = new TextField("Last name");

/* Action buttons */
Button save = new Button("Save", FontAwesome.SAVE);
Button cancel = new Button("Cancel");
Button delete = new Button("Delete", FontAwesome.TRASH_O);
CssLayout actions = new CssLayout(save, cancel, delete);

@Autowired
public CustomerEditor(CustomerRepository repository) {
this.repository = repository;

addComponents(firstName, lastName, actions);

// Configure and style components
setSpacing(true);
actions.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
save.setStyleName(ValoTheme.BUTTON_PRIMARY);
save.setClickShortcut(ShortcutAction.KeyCode.ENTER);

// wire action buttons to save, delete and reset
save.addClickListener(e -> repository.save(customer));
delete.addClickListener(e -> repository.delete(customer));
cancel.addClickListener(e -> editCustomer(customer));
setVisible(false);
}

public interface ChangeHandler {

void onChange();
}

public final void editCustomer(Customer c) {
final boolean persisted = c.getId() != null;
if (persisted) {
// Find fresh entity for editing
customer = repository.findOne(c.getId());
}
else {
customer = c;
}
cancel.setVisible(persisted);

// Bind customer properties to similarly named fields
// Could also use annotation or "manual binding" or programmatically
// moving values from fields to entities before saving
BeanFieldGroup.bindFieldsUnbuffered(customer, this);

setVisible(true);

// A hack to ensure the whole form is visible
save.focus();
// Select all text in firstName field automatically
firstName.selectAll();
}

public void setChangeHandler(ChangeHandler h) {
// ChangeHandler is notified when either save or delete
// is clicked
save.addClickListener(e -> h.onChange());
delete.addClickListener(e -> h.onChange());
}

}


package com.example;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Customer {

@Id
@GeneratedValue
private Long id;

private String firstName;

private String lastName;

protected Customer() {
}

public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public Long getId() {
return id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id,
firstName, lastName);
}

}

 Deployable war file

The first step in producing a deployable war file is to provide a SpringBootServletInitializer subclass and override its configure method. This makes use of Spring Framework’s Servlet 3.0 support and allows you to configure your application when it’s launched by the servlet container.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
   
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
       
return application.sources(Application.class);
    }
public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.
class, args);
    }
}
Applications can fall into more than one category:
  • Servlet 3.0+ applications with no web.xml.
  • Applications with a web.xml.
  • Applications with a context hierarchy.
  • Applications without a context hierarc