Essay about CompositeDesignPattern Group3

Submitted By michel1989
Words: 765
Pages: 4

Composite Design Pattern
Dilip Singh
Kamal Chanda
Sai Mahesh

Design Pattern
Design pattern is a general reusable soluton to a commonly occurring problem within a given context in sofware design.

Patterns [cont..]
• Design Patterns provide easy to recognize and use OOP solutons to common problems.
• They're inherently easy to maintain, because many people are familiar with them, just like a standard.

Patterns [cont..]
• This is very similar to how google works.
• Everyone knows how to google
• So when you get a query like "What is the purpose of design patterns", you can very quickly use this common interface to solve a problem. Patterns are primarily categorized as:
• Creatonal
• Structural
• Behavioral
Structural design patterns are design patterns that ease the design by identfying a simple way to realize relatonships between enttes

Composite Design Pattern
• Allows you to treat individual objects and compositons of the objects uniformly. • It allows us to represent part-whole hierarchies.

When
• The composite pattern is a design pattern that is used when creatng hierarchical object models.
• Composite should be used when clients ignore the difference between compositons of objects and individual objects

Hierarchical Object Models

Component
• Component is the interface for all objects in the compositon
• It can be an interface or an abstract class with some methods common to all the objects.
Leaf
• Defines the behavior for the elements in the compositon. It is the building block for the compositon and implements base component.
It doesn’t have references to other Components.

Composite
• It consists of leaf elements and implements the operatons in component. Client
• The client manipulates objects in the hierarchy using the component interface. Scenario
In a small organizaton, there are 5 employees. At top positon, there is
1 general manager. Under general manager, there are two employees, one is manager and other is developer and further manager has two developers working under him. We want to print name and salary of all employees from top to bottom.

Implementation

Sample code

Employee (Component)

public interface Employee { public void add(Employee employee); public void remove(Employee employee); public Employee getChild(int i); public String getName(); public double getSalary(); public void print();
}

Manager (Composite)
Public Class Manager implements Employee
{
private String name; private double salary; public Manager(String name,double salary){ this.name = name; this.salary = salary;
}
List<Employee> employees = new ArrayList<Employee>(); public void add(Employee employee) { employees.add(employee); } public Employee getChild(int i) { return employees.get(i);
}

public String getName() { return name;
}

public double getSalary() { return salary;
}
}

Developer (Leaf) public String getName() { return name;

public class Developer implements Employee{
}
private String name;

public double getSalary() { return salary;

private double salary;
}
public Developer(String name,double salary){ this.name = name;

public void print() {
System.out.println("-------------");

this.salary = salary;
}

System.out.println("Name ="+getName());

public void add(Employee employee) {

System.out.println("Salary ="+getSalary());

//this is leaf node so this method is not applicable to this class.

System.out.println("-------------");
}

} public void remove(Employee employee) { public Employee getChild(int i) {
//this is leaf node so this method is not applicable to this class.