Design Mode: factory Mode
Disadvantages of abstract factory Model
It is not easy to expand new products
If you want to add a new product to the entire product family, you need to modify the abstract factory so that all factory implementation classes can be modified.
==============================
The factory is actually a process of abstracting the commonalities or similarities of things, defining common interfaces, and handing them over to specific sub-classes for processing.
Understand the concept of product family and level:
1. Abstract Factory: Define factory interfaces, including methods for producing plain and strawberry-flavored yogurt;
2. Define the factory implementation class: Mengniu factory and Erie factory, and implement the interface method;
3. Abstract Factory: Define the taste interface, including the original flavor interface and the strawberry flavor interface. Each interface contains the price method of the taste yogurt;
4. define the implementation class of the taste interface: Each factory can produce both the original flavor and the strawberry flavor yogurt. Therefore, each factory contains the implementation class of the original flavor interface and the implementation class of the strawberry flavor interface respectively, the implementation class provides the price for this taste of yogurt.
5. Customer: if the customer buys the yogurt, the manufacturer who wants to buy the yogurt will get the price information of the yogurt of different tastes under the manufacturer;
The Code is as follows:
Define the strawberry flavor interface:
package iv.factory.feidanyi.yuanshi.interfaces;public interface CaoMeiWei {void getPrice();}
Define the implementation class of strawberry flavor: Mengniu strawberry flavor
Package iv. factory. feidanyi. yuanshi. classes; import iv. factory. feidanyi. yuanshi. interfaces. caoMeiWei; public class MengNiuCmw implements CaoMeiWei {private int price = 0; public MengNiuCmw (int price) {this. price = price;} public void getPrice () {// TODO Auto-generated method stubSystem. out. println (Mengniu strawberry flavor Price: + this. price );}}
Define the implementation class of strawberry flavor: Erie strawberry flavor
Package iv. factory. feidanyi. yuanshi. classes; import iv. factory. feidanyi. yuanshi. interfaces. caoMeiWei; public class YiLiCmw implements CaoMeiWei {private int price = 0; public YiLiCmw (int price) {this. price = price;} public void getPrice () {// TODO Auto-generated method stubSystem. out. println (Erie strawberry flavor Price: + this. price );}}
Define the original interface:
package iv.factory.feidanyi.yuanshi.interfaces;public interface YuanWei {void getPrice();}
Define the implementation class of the original interface: Mengniu Original:
Package iv. factory. feidanyi. yuanshi. classes; import iv. factory. feidanyi. yuanshi. interfaces. yuanWei; public class MengNiuYw implements YuanWei {private short price = 0; private String brand =; public MengNiuYw (int price) {this. price = (short) price;} public void getPrice () {// TODO Auto-generated method stubSystem. out. println (Original Price: + this. price);} public void getBrand () {// TODO Auto-generated method stubSystem. out. println (brand: + this. brand );}}
Define the implementation class of the original interface: Erie Original:
Package iv. factory. feidanyi. yuanshi. classes; import iv. factory. feidanyi. yuanshi. interfaces. yuanWei; public class YiLiYw implements YuanWei {private int price = 0; private String brand =; public YiLiYw (int price) {this. price = price;} public void getPrice () {// TODO Auto-generated method stubSystem. out. println (Original Price: + this. price );}}
Define the manufacturer's interface:
package iv.factory.feidanyi.yuanshi.interfaces;public interface AbstractFactory {YuanWei createYuanWei();CaoMeiWei createCaoMeiWei();}
Define the implementation class of the manufacturer's interface: Mengniu manufacturer:
package iv.factory.feidanyi.yuanshi.classes;import iv.factory.feidanyi.yuanshi.interfaces.AbstractBrandFactory;import iv.factory.feidanyi.yuanshi.interfaces.AbstractFactory;import iv.factory.feidanyi.yuanshi.interfaces.CaoMeiWei;import iv.factory.feidanyi.yuanshi.interfaces.YuanWei;public class MengNiuFactory implements AbstractFactory,AbstractBrandFactory {public CaoMeiWei createCaoMeiWei() {// TODO Auto-generated method stubreturn new MengNiuCmw(14);}public YuanWei createYuanWei() {// TODO Auto-generated method stubreturn new MengNiuYw(15);}public String getBrand() {// TODO Auto-generated method stubreturn new MengNiu().getBrand();}}
Define the manufacturers interface implementation class: Erie manufacturers:
package iv.factory.feidanyi.yuanshi.classes;import iv.factory.feidanyi.yuanshi.interfaces.AbstractBrandFactory;import iv.factory.feidanyi.yuanshi.interfaces.AbstractFactory;import iv.factory.feidanyi.yuanshi.interfaces.CaoMeiWei;import iv.factory.feidanyi.yuanshi.interfaces.YuanWei;public class YiLiFactory implements AbstractFactory,AbstractBrandFactory {public CaoMeiWei createCaoMeiWei() {// TODO Auto-generated method stubreturn new YiLiCmw(11);}public YuanWei createYuanWei() {// TODO Auto-generated method stubreturn new YiLiYw(12);}public String getBrand() {// TODO Auto-generated method stubreturn new YiLi().getBrand();}}
Define customer class:
package iv.factory.feidanyi.yuanshi.classes;import iv.factory.feidanyi.yuanshi.interfaces.AbstractFactory;import iv.factory.feidanyi.yuanshi.interfaces.CaoMeiWei;import iv.factory.feidanyi.yuanshi.interfaces.YuanWei;public class Customer {private YuanWei yw =null;private CaoMeiWei cmw = null;public void buyMilk(AbstractFactory af){yw = af.createYuanWei();cmw = af.createCaoMeiWei();yw.getPrice();cmw.getPrice();}}
Define the main method class:
package iv.factory.feidanyi.yuanshi.classes;import iv.factory.feidanyi.yuanshi.interfaces.AbstractBrandFactory;import iv.factory.feidanyi.yuanshi.interfaces.AbstractFactory;public class client {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubCustomer cus = new Customer();AbstractBrandFactory af0 = new MengNiuFactory();AbstractFactory af = new MengNiuFactory();System.out.println(af0.getBrand());cus.buyMilk(af);AbstractBrandFactory af11 = new YiLiFactory();AbstractFactory af1 = new YiLiFactory();System.out.println(af11.getBrand());cus.buyMilk(af1);}}
Complete.
Dynamic Loading class:
Class. forName (package path of the String type Class). newInstance ();
============================
Encapsulation of "change" is the principle of the design pattern.
When to use the factory model: if an instance of an object needs to be produced in many places in the project, you can consider using the factory model to let the factory produce instances, to modify the production instance mode, you only need to modify the project and do not need to modify it everywhere.
Under what circumstances should I use the abstract factory model?
1. A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all forms of factory models.
2. There are more than one product family in this system, and the system only consumes products of one of them.
3. products belonging to the same product family are used together. This constraint must be reflected in the system design.(For example, the Intel motherboard MUST use an Intel CPU or an Intel chipset)
4. The system provides a product library. All products use the same interface, so that the client does not rely on implementation.
Advantages of abstract factory Model
Separation interfaces and Implementations
The client uses an abstract factory to create the required objects, and the client does not know the specific implementation. The client is only for product-oriented interface programming. That is to say, the client is decoupled from the specific product implementation.
Easy to switch product families
Because a specific factory implementation represents a product family, for example, from the Intel series to the AMD series in the above example, you only need to switch the specific factory.
Disadvantages of abstract factory Model
It is not easy to expand new products
If you want to add a new product to the entire product family, you need to modify the abstract factory so that all factory implementation classes can be modified.