Composite Pattern

public interface Employee {

	/**
	 * @return the name of the employee
	 */
	String getName();

	/**
	 * @param e add this employee to the list of employees
	 */
	void add(Employee e);

	/**
	 * @param e remove this employee from the list of employees
	 */
	void remove(Employee e);

	/**
	 * @return the list of employees
	 */
	List<Employee> getEmployees();

	/**
	 * This method estimates the costs in ManDays for the given project. Managers
	 * delegate this request to their employees, developers return an estimate.
	 *
	 * @param projectDescription
	 * @return
	 */
	int estimateProject(String projectDescription);
}

public class VP extends Manager {
	public VP(String name) {
		super(name);
	}

	@Override
	public String toString() {
		return "I am " + getName() + ", VP";
	}

	/**
	 * VP doubles the estimated amount.
	 */
	@Override
	public int estimateProject(String projectDescription) {
		System.out.println("I am " + getName() + ", the VP, and calling for an estimate...");
		final int projectEstimate = super.estimateProject(projectDescription);
		System.out.println("Original estimate: " + projectEstimate);
		return Math.toIntExact(Math.round(projectEstimate * 2));
	}
}
public class TeamLeader extends Manager {
	public TeamLeader(String name) {
		super(name);
	}

	@Override
	public String toString() {
		return "I am " + getName() + ", Team Leader";
	}
}
public abstract class Manager implements Employee {
	List<Employee> employees = new ArrayList<>();
	String name;

	public Manager(String name) {
		this.name = name;
	}

	@Override
	public List<Employee> getEmployees() {
		return this.employees;
	}

	@Override
	public void add(Employee e) {
		if (e != null) {
			this.employees.add(e);
		}
	}

	@Override
	public void remove(Employee e) {
		if (e != null) {
			this.employees.remove(e);
		}
	}

	@Override
	public int estimateProject(String projectDescription) {
		if (this.employees.isEmpty()) {
			return 0;
		}
		return Math.round(this.employees.stream().mapToInt(e -> {
			System.out.println(e);
			return e.estimateProject(projectDescription);
		}).sum() / this.employees.size());
	}

	@Override
	public String getName() {
		return this.name;
	}

}
public class Developer implements Employee {
    String name;

	public Developer(String name) {
		this.name = name;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void add(Employee e) {
	}

	@Override
	public void remove(Employee e) {
	}

	@Override
	public List<Employee> getEmployees() {
		return null;
	}

	@Override
	public int estimateProject(String projectDescription) {
		return new Random().nextInt(24);
	}

	@Override
	public String toString() {
		return "I am " + getName() + ", Developer";
	}
}
	public static void main(String... args) {
        final Developer d1 = new Developer("Jack");
        final Developer d2 = new Developer("Jill");
        final Developer d3 = new Developer("Brian");
        final Developer d4 = new Developer("Bob");
        
        final Manager m1 = new TeamLeader("Marc");
        final Manager m2 = new TeamLeader("Christian");
        final Manager m3 = new TeamLeader("Phil");
        m1.add(d3);
        m1.add(d2);
        m2.add(d1);
		m3.add(d4);
		
		final VP vp = new VP("Joseph");
		vp.add(m1);
		vp.add(m2);
		
		System.out.println("Our estimate is: " + vp.estimateProject("New exotic feature"));
	}




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.