java design pattern -- Abstract Factory Pattern
1. Introduction to abstract factory mode
Abstract factory pattern: compared with builder pattern, abstract factory pattern is to create other factories around a super factory, rather than to define what specific functions he needs in a factory. In the abstract factory mode, different factories under the super factory can be distinguished according to different interfaces.
Intent: provide an interface to create a series of related or interdependent objects without specifying their specific classes.
Usage scenario: the system has more than one product family, and the system only uses one of them. Take fish for example. Fish is equivalent to a system, and the system is divided into freshwater fish and marine fish two product families. For these two product families, there are big fish and small fish. When we want to create a large freshwater fish, we don't need to know how to create this object, just call the method of creating a large fish class in the freshwater fish.
2. Role definition
Abstract factory class: core, which defines the interface used to generate abstract products.
Abstract product class: the abstract interface or abstract class of various products.
Concrete factory class: implement the methods in the abstract factory to generate a set of concrete products. (what we generate is a set of products, that is, the factories in the super factories mentioned above) but we can choose to generate only one of them according to our needs.
Concrete product class: the class that implements the interface of abstract product.
3. Code rendering
Step 1: create an abstract factory class
interface fAbstractFactory{ Divide the fish into big fish and small fish. So in this super factory, we only need to know these two categories public Bfish createBfish(String name); public Sfish createSfish(String name); }
Step 2: create an abstract product class
interface Bfish{ void printName(); } interface Sfish{ void printName(); }
Step 3: create a specific factory class
I subdivide big fish and small fish into freshwater fish and sea fish. As two sub products in a product family
class FreshwaterFactory implements fAbstractFactory{ Freshwater fish factory(product1) public Bfish createBfish(String name) { Create freshwater fish return new FreshwaterBFish(name); } public Sfish createSfish(String name) { Create freshwater fish return new FreshwaterSFish(name); } } class SeaFactory implements fAbstractFactory{ Marine fish factory(product2) public Bfish createBfish(String name) { Create a sea fish return new SeaBFish(name); } public Sfish createSfish(String name) { Create sea fish return new SeaSFish(name); } }
Step 3: create a specific product class
class FreshwaterBFish implements Bfish{ private String name; public FreshwaterBFish(String name) { this.name = name; } public String getName() { return this.name; } public void printName() { System.out.println("freshwater"+this.name); } } class SeaBFish implements Bfish{ private String name; public SeaBFish(String name) { this.name = name; } public String getName() { return this.name; } public void printName() { System.out.println("seawater"+this.name); } } class FreshwaterSFish implements Sfish{ private String name; public FreshwaterSFish(String name) { this.name = name; } public String getName() { return this.name; } public void printName() { System.out.println("freshwater"+this.name); } } class SeaSFish implements Sfish{ private String name; public SeaSFish(String name) { this.name = name; } public String getName() { return this.name; } public void printName() { System.out.println("seawater"+this.name); } }
Test:
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub fAbstractFactory FreshwaterFactory = new FreshwaterFactory(); Bfish fish1 = FreshwaterFactory.createBfish("Big fish 1"); Sfish fish2 = FreshwaterFactory.createSfish("Little fish 2"); fish1.printName(); fish2.printName(); fAbstractFactory SeaFactory = new SeaFactory(); Bfish fish3 = SeaFactory.createBfish("Big fish 3"); Sfish fish4 = SeaFactory.createSfish("Little fish 4"); fish3.printName(); fish4.printName(); } }
Freshwater fish 1 Freshwater fish 2 Sea fish 3 Sea fish 4
4, summary
When using the abstract factory pattern to design a system, the system is independent of the creation, composition, and representation of its products. But when you want to expand the products in the system, you need to modify each product under it, that is, to modify each subclass.
Compared with builder mode, abstract factory mode pays more attention to the result of creation. Therefore, in the abstract factory class, the emphasis is on returning an instance. The builder model pays more attention to the process of creating things.