Abstract Factory mode
Abstract Factory pattern is the creation of other factories around a super-factory. The Super Factory is also known as a factory in other factories. This type of design pattern belongs to the Create pattern, which provides an optimal way to create an object.
In abstract Factory mode, an interface is the factory responsible for creating a related object and does not need to explicitly specify its class. Each generated factory is able to provide objects according to the factory pattern.
Intent: provides an interface that creates a series of related or interdependent objects without specifying their specific classes.
Main Solution: The main solution to the problem of interface selection.
when to use: System products have more than one product family, and the system only consumes the products of one of the families.
How to solve: within a product family, define multiple products.
Key code: aggregating multiple similar products in one factory.
Application Examples: The following example mouse, keyboard, headset for the product, HP, Dell for factory, abstract Factory mode is not only the production of mouse, while the production of keyboards. That is, the PC manufacturer is a parent class, has the production mouse, the production keyboard two interfaces. Dell Factory, the HP factory inherits it,
You can produce Dell Mouse + Dell keyboards Separately, and HP mouse + HP keyboards. When the factory is created, it is created by The Dell factory. Follow-up plant. Production mouse () production of Dell Mouse, factory. The production keyboard () produces a Dell keyboard.
Here is the implementation code, first of all the code structure diagram:
import Cn.review.day03.abstractfactory.product.Keboy; import Cn.review.day03.abstractfactory.product.Mouse; /** * PC Manufacturer factory Abstract class * @author Dyq * */ public abstract class pcfactory { public abstract Mouse Createmouse (); public abstract Keboy Createkeboy ();}
ImportCn.review.day03.abstractfactory.product.DellKeyboy;ImportCn.review.day03.abstractfactory.product.DellMouse;ImportCn.review.day03.abstractfactory.product.Keboy;ImportCn.review.day03.abstractfactory.product.Mouse;/*** Dell Manufacturing Plant *@authorDyq **/ Public classDellfactoryextendspcfactory{@Override PublicMouse Createmouse () {return NewDellmouse (); } @Override PublicKeboy Createkeboy () {return NewDellkeyboy (); }}
ImportCn.review.day03.abstractfactory.product.HpKeboy;ImportCn.review.day03.abstractfactory.product.HpMouse;ImportCn.review.day03.abstractfactory.product.Keboy;ImportCn.review.day03.abstractfactory.product.Mouse;/*** HP Manufacturing plant *@authorDyq **/ Public classHpfactoryextendspcfactory{@Override PublicMouse Createmouse () {return NewHpmouse (); } @Override PublicKeboy Createkeboy () {return NewHpkeboy (); }}
/*** Generate factory class, through different manufacturers to obtain different production plants *@authorDyq **/ Public classFactoryproducer { Public Staticpcfactory getfactory (String choice) {if(Choice.equalsignorecase ("DELL")){ return Newdellfactory (); } Else if(Choice.equalsignorecase ("HP")){ return Newhpfactory (); } return NULL; }}
/**@author*/Publicinterface Mouse { public String sayhi ();}
/** * Product Keyboard interface * @author Dyq * */ public interface Keboy { public String Sayhi ();}
/** * Dell Keyboard Production class * @author Dyq * */ public class dellkeyboy Implements keboy{@Override public String Sayhi () { return "produced a Dell keyboard" ; }}
/** * Dell Mouse Production class * @author Dyq * */ public class dellmouse Implements mouse{@Override public String Sayhi () { return "produced Dell Mouse" ; }}
/** * * @author Dyq * */ public class hpkeboy implements keboy{@Override public String Sayhi () { return "produced hp keyboard" ; }}
Public class Implements mouse{ @Override public String sayhi () { return "produced HP Mouse"; }}
Finally, the test class:
ImportCn.review.day03.abstractfactory.factory.FactoryProducer;Importcn.review.day03.abstractfactory.factory.PcFactory;ImportCn.review.day03.abstractfactory.product.Mouse;/*** Test Abstract Factory mode *@authorDyq **/ Public classDesignpatternabstractfactory { Public Static voidMain (string[] args) {pcfactory dellpcfactory= Factoryproducer.getfactory ("DELL"); Mouse Dellmouse=Dellpcfactory.createmouse (); String mouse1=Dellmouse.sayhi (); System.out.println (MOUSE1); Pcfactory hppcfactory= Factoryproducer.getfactory ("HP"); Mouse Hpmouse=Hppcfactory.createmouse (); String Mouse2=Hpmouse.sayhi (); System.out.println (MOUSE2); }}
In the abstract factory model, suppose we need to add a product
Assuming we add the headset to this product, first we need to add the headset to the parent class, plus the Dell headset, HP headset, two subcategories.
Then in the PC maker this parent class, the production of the headset interface is increased. Finally, in the Dell Factory, HP factory, the two categories, respectively, the production of Dell headsets, HP headset features.
Java Common design pattern (one, abstract Factory mode)