Abstract factory pattern of design pattern
1. Definition of abstract factory pattern
The Abstract Factory Pattern is to create other factories around a super factory. The super factory is also called the factory of other factories. This type of design pattern belongs to the creation pattern, which provides the best way to create objects.
In the abstract factory pattern, interfaces are responsible for creating factories for related objects without explicitly specifying their classes. Each generated factory can provide objects according to the factory pattern.
- Intention: Provide an interface for creating a series of related or interdependent objects without specifying their specific classes.
- The main solution: mainly to solve the problem of interface selection.
- When to use: The system has more than one product family, and the system consumes only one of them.
- How to solve it: Define multiple products within a product family.
- Key Code: Aggregate multiple similar products in a factory.
2. Code implementation
2.1 Define the same product family product interface
public interface ICpu {
public void pCpu();
}
public interface IMainBorad {
public void pMainBorad();
}
2.2 Definition of abstract factory interface
public interface AbstractFactory {
public ICpu createCpu();
public IMainBorad createMBoard();
}
2.3 Definition of Products
public class AmdCpu implements ICpu{
@Override
public void pCpu() {
System.out.println("amd cpu");
}
}
public class AmdMBoard implements IMainBorad{
@Override
public void pMainBorad() {
System.out.println("amd Mainboard");
}
}
public class InterCpu implements ICpu {
@Override
public void pCpu() {
System.out.println("Inter cpu");
}
}
public class InterMBoard implements IMainBorad {
@Override
public void pMainBorad() {
System.out.println("Inter mainBoard");
}
}
2.4 Definition of Factory
public class AmdFactory implements AbstractFactory{
@Override
public ICpu createCpu() {
return new AmdCpu();
}
@Override
public IMainBorad createMBoard() {
return new AmdMBoard();
}
}
public class InterFactory implements AbstractFactory{
@Override
public ICpu createCpu() {
return new InterCpu();
}
@Override
public IMainBorad createMBoard() {
return new InterMBoard();
}
}
2.5 call
public class Main {
public static void main(String[] args) {
AmdFactory amdFactory = new AmdFactory();
ICpu createCpu = amdFactory.createCpu();
createCpu.pCpu();
IMainBorad createMBoard = amdFactory.createMBoard();
createMBoard.pMainBorad();
InterFactory interFactory = new InterFactory();
ICpu createCpu2 = interFactory.createCpu();
createCpu2.pCpu();
IMainBorad createMBoard2 = interFactory.createMBoard();
createMBoard2.pMainBorad();
}
}
3. summary
Suppose a subsystem needs some product objects, and these products belong to more than one product hierarchy.
In order to separate the responsibility of consuming these product objects from the responsibility of creating them,
The abstract factory model can be introduced. In this way, the consumer does not need to be directly involved in the creation of the product.
Instead, you only need to request the required product from a common factory interface.