Github source address
An overview of 23 design patterns
- Java Language Design – An overview of 23 design patterns
Creation pattern
- Factory Method Pattern
- Abstract Factory Pattern
- Builder Mode
- Prototype mode
- Singleton
Structural mode
- Facade Pattern
- Adapter mode (Adapter)
- Proxy mode
- Composite mode
- Flyweight Mode
- Decorator pattern
- Bridge mode (Bridge)
Behavioral pattern
- Mediator Mode
- Observer Model
- Command mode
- Iterator pattern (Iterator)
- Template Method
- Strategy Pattern
- State mode
- Memento Mode
- Interpreter mode
- Chain of Responsibility model
- Visitor Pattern
define
Define an interface to create an object and let subclasses decide which factory class to instantiate. The factory pattern delays the creation of the object until the subclass.
Usage scenarios
The factory method pattern can be used anywhere complex objects need to be generated. Complex objects are suitable for factory patterns. Objects that can be created with new do not need factory patterns
implementation
We will create a Shape interface and an entity class that implements the Shape interface. The next step is to define the factory class ShapeFactory.
The FactoryPatternDemo class uses ShapeFactory to get Shape objects. It passes information to the ShapeFactory (CIRCLE/RECTANGLE/SQUARE) to get the type of the object it needs.
Step 1
Create an interface:
public interface Shape {
void draw(a);
}
Copy the code
Step 2
Create an entity class that implements the interface.
public class Rectangle implements Shape {
@Override
public void draw(a) {
System.out.println("Inside Rectangle::draw() method."); }}Copy the code
public class Square implements Shape {
@Override
public void draw(a) {
System.out.println("Inside Square::draw() method."); }}Copy the code
public class Circle implements Shape {
@Override
public void draw(a) {
System.out.println("Inside Circle::draw() method."); }}Copy the code
Step 3
Create a factory that generates objects based on the entity class given the information.
public class ShapeFactory {
// Use getShape to get an object of the shape type
public Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
} else if (shapeType.equalsIgnoreCase("SQUARE")) {
return new Square();
}
return null; }}Copy the code
Step 4
Use this factory to get objects of the entity class by passing type information.
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
// Get the Circle object and call its draw method
Shape shape1 = shapeFactory.getShape("CIRCLE");
// Call Circle's draw method
shape1.draw();
// Get the Rectangle object and call its draw method
Shape shape2 = shapeFactory.getShape("RECTANGLE");
// Call the Rectangle draw method
shape2.draw();
// Get the Square object and call its draw method
Shape shape3 = shapeFactory.getShape("SQUARE");
// Call Square's draw methodshape3.draw(); }}Copy the code
Step 5
Execute the program and output the result:
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Copy the code
Code Address:
- Github:github.com/leifu1107/a…