Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composite design pattern #24

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Composite Design Pattern class diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Composite design pattern tree example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# DesignPatternsJava9
This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns.
# What is Composite Design Pattern
* Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy.
* This pattern creates a class that contains group of its own objects.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/composite-pattern/diagrams/Composite%20Design%20Pattern%20class%20diagram.png "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/composite-pattern/diagrams/Composite%20design%20pattern%20tree%20example.png "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/composite-pattern/diagrams/composite%20sequence%20diagram.png "Diagram")

### When to use Composite Design Pattern
Composite Pattern should be used when clients need to ignore the difference between compositions of objects and individual objects and needs to handles them in the same way.

### The Composite Pattern has four participants:
* Component – Component declares the interface for objects in the composition and for accessing and managing its child components. It also implements default behavior for the interface common to all classes as appropriate.
* Leaf – Leaf defines behavior for primitive objects in the composition. It represents leaf objects in the composition.
* Composite – Composite stores child components and implements child related operations in the component interface.
* Client – Client manipulates the objects in the composition through the component interface.

### Learn Design Patterns with Java by Aseem Jain
This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain".

### Course link:
https://www.packtpub.com/application-development/learn-design-patterns-java-9-video

### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem

### Authors blog on design patterns:
https://premaseem.wordpress.com/category/computers/design-patterns/

### Software Design pattern community face book page:
https://www.facebook.com/DesignPatternGuru/

### Note:
* This code base will work on Java 9 and above versions.
* `diagrams` folders carry UML diagrams.
* `pattern` folder has code of primary example.
* `patternBonus` folder has code of secondary or bonus example.
Binary file added composite sequence diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/composite sequence diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 75 additions & 2 deletions pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,86 @@
package com.premaseem;

import java.util.HashMap;
import java.util.Map;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication

*/
public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
System.out.println(" Composite Design Pattern Organizational Tree");

// Treating all employees in uniform way ( no discrimination )
Employee CTO = new Employee("Aseem",1);

Employee level_1_1 = new Employee("Kinesh",2);
Employee level_1_2 = new Employee("Karteek",3);
Employee level_1_3 = new Employee("Ash",4);

Employee level_2_1 = new Employee("Amy",5);
Employee level_2_2 = new Employee("Courty",6);
Employee level_2_3 = new Employee("Jackson",7);
Employee level_2_4 = new Employee("Leticia",8);

Employee level_3_1 = new Employee("Peter",9);
Employee level_3_2 = new Employee("Jodi",10);
Employee level_3_3 = new Employee("William",11);
Employee level_3_4 = new Employee("Darly",12);

// Using tree structure in natural way with Leaf and branch
CTO.addSubordinate(level_1_1);
CTO.addSubordinate(level_1_2);
CTO.addSubordinate(level_1_3);

// adding level 1 subordinates in tree
level_1_1.addSubordinate(level_2_1);
level_1_1.addSubordinate(level_2_2);

level_1_2.addSubordinate(level_2_3);
level_1_2.addSubordinate(level_2_4);

// adding level 2 subordinates in tree
level_2_2.addSubordinate(level_3_1);
level_2_2.addSubordinate(level_3_2);

level_2_3.addSubordinate(level_3_3);
level_2_3.addSubordinate(level_3_4);

// Manager details
System.out.println("Details of Leaf - Non Manager");
level_2_2.showEmployeeDetails();


// Non Manager details
System.out.println("Details of Branch - Manager");
level_3_1.showEmployeeDetails();

System.out.println("Details of full tree / entire organization ");
CTO.showEmployeeDetails();



/* OLD discarded code */

// ManagerEmployee managerEmployee = new ManagerEmployee("Aseem");
// ManagerEmployee managerEmployee1 = new ManagerEmployee("Kinesh");
// ManagerEmployee managerEmployee2 = new ManagerEmployee("Karteek");
//
// NonManagerEmployee nonManagerEmployee1 = new NonManagerEmployee("Ash");
// NonManagerEmployee nonManagerEmployee2 = new NonManagerEmployee("Amy");
//
// Map managerEmployees = new HashMap();
// // Manager can have another manager under him
// managerEmployees.put(managerEmployee,managerEmployee1);
// // Manager can have non manager under him
// managerEmployees.put(managerEmployee,nonManagerEmployee1);
// managerEmployees.put(managerEmployee,nonManagerEmployee2);
// managerEmployees.put(managerEmployee1,managerEmployee2);
//
// System.out.println(managerEmployees);

}
}
46 changes: 46 additions & 0 deletions pattern/src/com/premaseem/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.premaseem;

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

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class Employee {
private String empName;
private Integer empId;

// Composition of same object type
private List<Employee> subordinateEmployee = new ArrayList<>();

public Employee (String empName, Integer empId) {
this.empName = empName;
this.empId = empId;
}

public void addSubordinate(Employee emp){
subordinateEmployee.add(emp);
}

public void removeSubordinate(Employee emp){
subordinateEmployee.remove(emp);
}

public void showEmployeeDetails(){

System.out.println(toString());
}

@Override
public String toString () {
String node = subordinateEmployee.size() == 0 ? "No subordinates" : subordinateEmployee.toString();
String level = subordinateEmployee.size() == 0 ? "Non Manger" : "Manager ";

return "\n"+ level +" {" +
"empName='" + empName + '\'' +
", subordinates =" + node +
"}";
}
}
24 changes: 24 additions & 0 deletions pattern/src/com/premaseem/ManagerEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class ManagerEmployee {


private String empName;

public ManagerEmployee (String empName) {

this.empName = empName;
}

@Override
public String toString () {
return "ManagerEmployee{" +
"empName='" + empName + '\'' +
'}';
}
}
23 changes: 23 additions & 0 deletions pattern/src/com/premaseem/NonManagerEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class NonManagerEmployee {
private String name;

public NonManagerEmployee (String name) {

this.name = name;
}

@Override
public String toString () {
return "NonManagerEmployee{" +
"name='" + name + '\'' +
'}';
}
}