JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Composite Pattern in java


The composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

When dealing with Tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code more complex, and therefore, error prone. The solution is an interface that allows treating complex and primitive objects uniformly. In object-oriented programming, a composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality. This is known as a "has-a" relationship between objects. The key concept is that you can manipulate a single instance of the object just as you would manipulate a group of them. The operations you can perform on all the composite objects often have a least common denominator relationship. For example, if defining a system to portray grouped shapes on a screen, it would be useful to define resizing a group of shapes to have the same effect (in some sense) as resizing a single shape.

Composite should be used when clients should ignore the difference between compositions of objects and individual objects. If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice; it is less complex in this situation to treat primitives and composites as homogeneous. Below is the UML structure of composite pattern.

Class representing Component:

package com.java2novice.dp.composite.pattern;

public interface Employee {

	public void showEmployeeDetails();
}

Class representing Leaf

package com.java2novice.dp.composite.pattern;

public class Engineer implements Employee {

	private String name;
	private long empId;
	private String department;

	public Engineer(long empId, String name, String department){
		this.empId = empId;
		this.name = name;
		this.department = department;
	}
	
	@Override
	public void showEmployeeDetails() {
		System.out.println(empId+" ***** "+name+" ***** "+department);
	}
}

Class representing Leaf

package com.java2novice.dp.composite.pattern;

public class Accountant implements Employee {

	private String name;
	private long empId;
	private String department;

	public Accountant(long empId, String name, String department){
		this.empId = empId;
		this.name = name;
		this.department = department;
	}
	
	@Override
	public void showEmployeeDetails() {
		System.out.println(empId+" ***** "+name+" ***** "+department);
	}
}

Class representing Composite

package com.java2novice.dp.composite.pattern;

import java.util.ArrayList;
import java.util.List;

public class CompanyDirectory implements Employee {

	private List<Employee> employeeList = new ArrayList<Employee>();
	
	@Override
	public void showEmployeeDetails() {
		for(Employee emp:employeeList){
			emp.showEmployeeDetails();
		}
	}
	
	public void addEmployee(Employee emp){
		employeeList.add(emp);
	}
	
	public void removeEmployee(Employee emp){
		employeeList.remove(emp);
	}
}

Class representing Client

package com.java2novice.dp.composite.pattern;

public class Company {

	public static void main(String a[]){
		
		Engineer eng1 = new Engineer(100, "Nataraj", "Engineering");
		Engineer eng2 = new Engineer(101, "Ravi", "Engineering");
		CompanyDirectory engDirectory = new CompanyDirectory();
		engDirectory.addEmployee(eng1);
		engDirectory.addEmployee(eng2);
		
		Accountant acc1 = new Accountant(200, "Gopi", "Accounts");
		Accountant acc2 = new Accountant(201, "RamGopal", "Accounts");
		CompanyDirectory accDirectory = new CompanyDirectory();
		accDirectory.addEmployee(acc1);
		accDirectory.addEmployee(acc2);
	
		CompanyDirectory directory = new CompanyDirectory();
		directory.addEmployee(engDirectory);
		directory.addEmployee(accDirectory);
		directory.showEmployeeDetails();
	}
}

Output:
100 ***** Nataraj ***** Engineering
101 ***** Ravi ***** Engineering
200 ***** Gopi ***** Accounts
201 ***** RamGopal ***** Accounts
<< Previous Program | Next Program >>
blog comments powered by Disqus

Java design pattern examples

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Builder Design Pattern
  4. Prototype Pattern
  5. Adapter Pattern
  6. Composite Pattern
  7. Proxy Pattern
Knowledge Centre
Interface and its usage
Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for declaring methods that one or more classes are expected to implement, capturing similarities between unrelated classes without forcing a class relationship and determining an object's programming interface without revealing the actual body of the class.
Famous Quotations
Insanity: doing the same thing over and over again and expecting different results.
-- Albert Einstein

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.