Design Patterns Table of content

Creational Patterns

  1. Singleton Pattern
  2. Builder Pattern
  3. Factory Method Pattern
  4. Prototype Pattern
  5. Abstract Factory Pattern

Structural Patterns

  1. Adapter Pattern
  2. Proxy Pattern
  3. Decorator Pattern
  4. Facade Pattern
  5. Composite Pattern
  6. Flyweight Pattern

Behavioral Patterns

  1. Stragegy Pattern
  2. Observer Pattern
  3. Template Method Pattern
  4. State Pattern
  5. Iterator Pattern



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

required
required


Flyweight Pattern

November 26, 2019

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"));
	}

November 26, 2019

Iterator Pattern

August 4, 2019

State Pattern

August 4, 2019

Template Method Pattern

August 4, 2019