Factory Pattern
Problem
Who should be responsible for creating objects when there are special considerations, such as complex creation logic, a desire to separate the creation responsibilities for better cohesion, and so forth?
Solution
Create a Pure Fabrication object called a Factory that handles the creation.
Shape.java
public interface Shape {
void draw();
}Rectangle.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("rectangle draw");
}
}Square.java
public class Square implements Shape {
@Override
public void draw() {
System.out.println("square draw");
}
}Circle.java
ShapeFactory.java
FactoryPatternDemo.java
Original example is from tutorialspoint
Think
Think cohesion and coupling
Think future reuse and modification
Last updated
Was this helpful?