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 >>

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
What is abstract class or abstract method?
We cannot create instance for an abstract class. We can able to create instance for its subclass only. By specifying abstract keyword just before class, we can make a class as abstract class.

public abstract class MyAbstractClass{

}

Abstract class may or may not contains abstract methods. Abstract method is just method signature, it does not containes any implementation. Its subclass must provide implementation for abstract methods. Abstract methods are looks like as given below:

public abstract int getLength();
Famous Quotations
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

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