Composite Design Pattern

Merhaba, bir önceki yazıda Decorator design pattern den bahsetmiştim. Bu yazıda ise başka bir structural bir pattern olan composite patternden bahsediyorum.

Ne zaman kullanılır

Eğer nesnelerin arasındaki yapı bir tree(ağaç) yapısında ise kullanılır. Yani hiyerarşik bir düzen varsa kullanılabilir.

En çok sık verilen örneklerden birkaçı bir şirketin içinde yapısal ilişkidir. CEO-CTO-Mudur-calısan ilişkisi buna bir örnektir.

package ornek2;

import java.util.ArrayList;
import java.util.Iterator;

public abstract class CompanyDirectory extends Employee implements Iterable<Employee>{

	private ArrayList<Employee> employeies = new ArrayList<Employee>();
	
	public CompanyDirectory(String name,String lastName) {
		super(name, lastName);
	}

	public ArrayList<Employee> getEmployeies() {
		return employeies;
	}

	public void addSubOrdinates(Employee emp) {
		 getEmployeies().add(emp);
	}

	@Override
	public Iterator<Employee> iterator() {
		return null;
	}

	@Override
	public void showDetails() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String toString() {
		return super.toString();
	}
}
package ornek2;

public abstract class Employee {
	
	private String name;
	private String lastName;
	
	public Employee(String name,String lastName) {
		this.name=name;
		this.lastName=lastName;
	}
	
	public abstract void showDetails();

	public String getName() {
		return name;
	}

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

	public String getLastName() {
		return lastName;
	}

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

	@Override
	public String toString() {
		return getName()+" "+getLastName();
	}
}
package ornek2;

import java.util.Iterator;

public class CEO  extends CompanyDirectory{

	
	public CEO(String name,String lastName) {
		super(name,lastName);
	}

	@Override
	public void showDetails() {
		System.out.println("CEO:"+getName()+" "+getLastName());
	}

	@Override
	public Iterator iterator() {
		return getEmployeies().iterator();
	}
}
package ornek2;

import java.util.Iterator;

public class CTO extends CompanyDirectory{

	public CTO(String name, String lastName) {
		super(name, lastName);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void showDetails() {
		System.out.println("CTO:"+getName()+" "+getLastName());
	}
	
	public void removeEmployee(Employee emp) {
		getEmployeies().remove(emp);
	}

	@Override
	public Iterator iterator() {
		return getEmployeies().iterator();
	}

}
package ornek2;

import java.util.Iterator;

public class Developer extends Employee {

	public Developer(String name,String lastName) {
		super(name,lastName);
	}

	@Override
	public void showDetails() {
		System.out.println("Devoloper:"+getName()+" "+getLastName());
	}

}
package ornek2;

import java.util.Iterator;

public class Manager extends CompanyDirectory {

	public Manager(String name, String lastName) {
		super(name, lastName);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void showDetails() {
		System.out.println("Manager"+getName()+" "+getLastName());
	}

	@Override
	public Iterator iterator() {
		return getEmployeies().iterator();
	}
}
package ornek2;

public class Test {

	public static void main(String[] args) {
		
		Developer dev = new Developer("omer","beden");
		Developer dev2 = new Developer("arif","gider");
		CEO ceo = new CEO("Elon","Musk");
		CTO cto = new CTO("arif","mamedov");
		Manager manager = new Manager("fatih","çetin");
		ceo.addSubOrdinates(cto);
		cto.addSubOrdinates(manager);
		manager.addSubOrdinates(dev);
		manager.addSubOrdinates(dev2);
		
		System.out.println(ceo);
		for (Employee employee : ceo.getEmployeies()) {
			System.out.println(" "+employee);
			
			for (Employee employee2 : cto.getEmployeies()) {
				System.out.println("  "+employee2);
			
				for (Employee employee3 : manager.getEmployeies()) {
					System.out.println("    "+employee3);
				}
			}
			
		}

	}

}

kaynak

https://refactoring.guru/design-patterns/composite

https://sourcemaking.com/design_patterns/composite