content
factory pattern
effect:
Implements the separation of creator and caller
Core essence:
Instantiate objects without using new. Use factory methods instead
The implementation class will be selected, and the objects will be created for unified management and control. This decouples the caller from our implementation class.
Three modes:
Simple Factory Pattern (Static Factory Pattern)
Used to produce any product in the same grade structure (for adding new products, the existing code of the ball cover is required)
Factory Method Pattern
Used to produce fixed products in the same hierarchical structure (support for adding arbitrary products)
Abstract Factory Pattern
Create other factories around a gigafactory. The Gigafactory is also known as the factory of other factories.
The Seven Principles of OOP
- Open-Closed Principle: A software entity should be open for extension and closed for modification
- Dependency Inversion Principle: Program for the interface, not for the implementation
- The Law of Demeter: Only communicate with your immediate friends and avoid communication with strangers
Simple Factory Pattern
Car
public interface Car {
void name();
}
WuLing
public class WuLing implements Car{
@Override
public void name() {
System.out.println("五菱宏光!");
}
}
Tesla
public class Tesla implements Car{
@Override
public void name() {
System.out.println("特斯拉!");
}
}
Consumer
public class Consumer {
public static void main(String[] args) {
//接口,所有实现类!
WuLing car1 = new WuLing();
Tesla car2 = new Tesla();
car1.name();
car2.name();
}
}
Carfactory factory
public class CarFactory {
public static Car getCar(String car) {
if (car.equals("五菱")) {
return new WuLing();
} else if (car.equals("特斯拉")) {
return new Tesla();
}else {
return null;
}
}
}
Question: At this time, a car is being added, and the code of the factory needs to be modified
//简单工厂模式 (静态工厂模式)
// 增加一个新的产品 如果你不修改代码,做不到!
//没有满足 开闭原则
public class CarFactory {
//方法一
public static Car getCar(String car) {
if (car.equals("五菱")) {
return new WuLing();
} else if (car.equals("特斯拉")) {
return new Tesla();
} else if (car.equals("大众")) {
return new DaZhong();
} else {
return null;
}
}
//方法二
public static Car getWuLingCar() {
return new WuLing();
}
public static Car Tesla() {
return new Tesla();
}
}
Factory Method Pattern
Car CarFactory
public interface Car {
void name();
}
// 工厂方法模式
public interface CarFactory {
Car getCar();
}
WuLing WuLingFactory
public class WuLing implements Car {
@Override
public void name() {
System.out.println("五菱宏光!");
}
}
public class WuLingFactory implements CarFactory{
@Override
public Car getCar() {
return new WuLing();
}
}
Tesla TeslaFactory
public class Tesla implements Car {
@Override
public void name() {
System.out.println("特斯拉!");
}
}
public class TeslaFactory implements CarFactory {
@Override
public Car getCar() {
return new Tesla();
}
}
Consumer
// 工厂方法模式
public class Consumer {
public static void main(String[] args) {
Car car1 = new WuLingFactory().getCar();
Car car2 = new TeslaFactory().getCar();
car1.name();
car2.name();
}
}
At this time, add a car without modifying the original code
MoBai MoBaiFactory
public class MoBai implements Car{
@Override
public void name() {
System.out.println("共享单车");
}
}
public class MoBaiFactory implements CarFactory{
@Override
public Car getCar() {
return new MoBai();
}
}
Structural complexity: simple
Code complexity is simple
Programming complexity: simple
Management complexity is simple
According to the design principle : the factory method pattern!
According to the actual business : simple factory mode!