While reading the "Westward design mode", but the difference between the three types of plant models, understanding is not very thorough, then this article to explain the pros and cons about the differences between the three.
Simple factory pattern (Simple Factory)
First define a common interface to a product category
public interface Product{
//价格
int price();
//产品名
String getName();
}
There are three products ProductA, ProductB, ProductC, are to achieve Product Interface
Products
public class ProductA implements Product {
@Override
public int price() {
return 100;
}
@Override
public String getName() {
return "ProductA";
}
}
ProductB
public class ProductB implements Product {
@Override
public int price() {
return 200;
}
@Override
public String getName() {
return "ProductB";
}
}
ProductC
public class ProductC implements Product {
@Override
public int price() {
return 300;
}
@Override
public String getName() {
return "ProductC";
}
}
Define a class factory production, the production of products corresponding to the input type
public class Factory {
/**
* 根据生产类型生产对应的产品
* @param type
* @return
*/
public static Product createProduct(String type){
Product product =null;
switch (type){
case "A":
product = new ProductA();
break;
case "B":
product = new ProductB();
break;
case "C":
product = new ProductC();
break;
}
return product;
}
}
The production of the type corresponding to the input products produced
Product productA = Factory.createProduct("A");
System.out.println("productA name="+productA.getName()+",getPrice:"+productA.getPrice());
Product productB = Factory.createProduct("B");
System.out.println("productB name="+productB.getName()+",getPrice:"+productB.getPrice());
Product productC = Factory.createProduct("C");
System.out.println("productC name="+productC.getName()+",getPrice:"+productC.getPrice());
Output:
productA name=ProductA,getPrice:100
productB name=ProductB,getPrice:200
productC name=ProductC,getPrice:300
The above is a typical example of simple factory mode, when the user needs to add product ProductD, judgment must be increased corresponding branch factory production process class, so simple factory pattern violates the principle of the open closure.
Simple factory pattern, using static method to generate a corresponding product according to the input parameters, the hidden details of the product instance.
Summary: simple factory mode maximum advantage that factory class contains the necessary logic to determine, based on the selection condition associated with the class of the client dynamically instantiated, for the client, in addition to rely on specific products. But when demand changes, the need to modify the original class, contrary to the open closed principle.
Factory method model (Factory Method)
Through the factory method pattern, we can solve the problem simple factory pattern.
First, declare a factory interface, all factories must implement this interface
public interface IFactory {
Product createProduct();
}
ProductA production plant FactoryA
public class FactoryA implements IFactory {
@Override
public Product createProduct() {
return new ProductA();
}
}
ProductB production plant FactoryB
public class FactoryB implements IFactory {
@Override
public Product createProduct() {
return new ProductB();
}
}
Similarly, the production plant FactoryC ProductC more like mode.
Now to be produced according to the new factory method pattern
IFactory factoryA = new FactoryA();
Product productA = factoryA.createProduct();
System.out.println("productA name="+productA.getName()+",getPrice:"+productA.getPrice());
IFactory factoryB = new FactoryB();
Product productB = factoryB.createProduct();
System.out.println("productB name="+productB.getName()+",getPrice:"+productB.getPrice());
IFactory factoryC = new FactoryB();
Product productC = factoryC.createProduct();
System.out.println("productC name="+productC.getName()+",getPrice:"+productC.getPrice());
Output:
productA name=ProductA,getPrice:100
productB name=ProductB,getPrice:200
productC name=ProductC,getPrice:300
When you need to add a new product ProductD, only need to create the corresponding FactoryD production function can be achieved without any impact on existing code, it is consistent with the open closed principle , but because each additional product, we need to add the corresponding production factories, leading to additional development effort.
Summary: the disadvantages of the use of polymorphism, plant overcomes the simple principle of open closed plant breached, while maintaining the advantages of an encapsulated object creation process.
Abstract Factory (Abstract Factory)
Suppose now required for each product corresponding gifts, do we want to add a Gift manufacturing factory? In fact, it is not necessary, because in this scenario, each product must be accompanied by a gift, so we can use the existing factories to produce gifts.
A first set of common interfaces Gift
public interface Gift {
String getGiftName();
}
Increase GiftA, GiftB, GiftC
public class GiftA implements Gift {
@Override
public String getGiftName() {
return "GiftA";
}
}
Factory Interface modification, a method to increase production Gift
public interface IFactory {
Product createProduct();
Gift createGift();
}
FactoryA method modified in the factory mode, FactoryB, FactoryC
public class FactoryA implements IFactory {
@Override
public Gift createGift() {
return new GiftA();
}
@Override
public Product createProduct() {
return new ProductA();
}
}
Production of products and gifts
IFactory factoryA = new FactoryA();
Product productA = factoryA.createProduct();
Gift giftA = factoryA.createGift();
Summary: abstract factory pattern provides a create a series of related or dependent objects interface, without having to develop their specific class. Abstract factory interface, abstract methods should include all the products created, we can define implement more than one interface, a factory can produce more than one product category, and the same factory method pattern, abstract factory pattern also achieved the development of closed principle
description link : https: //www.jianshu.com/p/d27d698802fd