03. Factory mode

The factory pattern can be refined into three categories: simple factories, factory methods, and abstract factories.

1. Simple Factory

The simple factory mode is equivalent to a factory with various products, created in a class, the customer does not need to know the name of the specific product, but only needs to know the parameters corresponding to the product class. However, the responsibility of the factory is too heavy, and when there are too many types, it is not conducive to the expansion and maintenance of the system.

Code case

File 01: Car.java

package cn.itchao.factory.simple;

public interface Car {
    void run();
}

File 02: BydCar.java

package cn.itchao.factory.simple;

public class BydCar implements Car {
    @Override
    public void run() {
        System.out.println("制造一台比亚迪汽车");
    }
}

File 03: AuDiCar.java

package cn.itchao.factory.simple;

public class AuDiCar implements Car{
    @Override
    public void run() {
        System.out.println("制造一台奥迪汽车");
    }
}

File 04: CarFactory.java

package cn.itchao.factory.simple;

public class CarFactory {
    public static Car newCar(String name){
        if(null==name||"".equals(name)){
            return null;
        }
        if("比亚迪".equals(name)){
            return new BydCar();
        }
        if("奥迪".equals(name)){
            return new AuDiCar();
        }
        return null;
    }
}

Test code:

package cn.itchao.factory.simple;

public class Test01 {
    public static void main(String[] args) {
        Car BydCar = CarFactory.newCar("比亚迪");
        Car AuDiCar = CarFactory.newCar("奥迪");
        BydCar.run();
        AuDiCar.run();
    }
}

operation result:

/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/lib/tools.jar:/local/idea_work/study/itchao-design-mode/target/classes cn.itchao.factory.simple.Test01
制造一台比亚迪汽车
制造一台奥迪汽车

Process finished with exit code 0

Summarize:

        Advantages: The simple factory pattern can decide which specific class object should be created according to the information given by the outside world. The respective responsibilities and powers are clearly distinguished, which is beneficial to the optimization of the entire software architecture.

        Disadvantages: It is obvious that the factory class concentrates the creation logic of all instances, which is easy to violate the high cohesive responsibility distribution principle of GRASPR.

2. Factory method

   The factory method pattern, also known as the factory pattern, polymorphic factory pattern and virtual constructor pattern, is responsible for defining the public interface for creating objects by defining the factory parent class, while the subclass is responsible for generating specific objects.

Code case:

(1) Create an abstract factory class and define the public interface of the concrete factory

package cn.itchao.factory.method;

abstract class AbstractFactory {
    //定义一个抽象的 "制造方法"
    public abstract AbstractProduct manufacture();
}

(2) Create an abstract product class  and define the public interface of a specific product

package cn.itchao.factory.method;

abstract class AbstractProduct {
    public abstract void show();
}

(3) Create specific product classes (inherit abstract product classes) and define specific products to be produced

package cn.itchao.factory.method;
/**
 * @Description:具体的奥迪汽车生产类
 */
public class AuDiCar extends AbstractProduct {
    @Override
    public void show() {
        System.out.println("生产一台奥迪汽车");
    }
}
package cn.itchao.factory.method;
/**
 * @Description:具体的比亚迪汽车生产类
 */
public class BydCar extends AbstractProduct {
    @Override
    public void show() {
        System.out.println("生产一台比亚迪汽车");
    }
}

(4), create a concrete factory class (inherit the abstract factory class), and define the method of creating the corresponding concrete product instance

package cn.itchao.factory.method;
/**
 * @Description:比亚迪工厂类生产比亚迪汽车
 */
public class BydFactory extends AbstractFactory {
    @Override
    public AbstractProduct manufacture() {
        return new BydCar();
    }
}
package cn.itchao.factory.method;
/**
 * @Description:奥迪工厂类生产奥迪汽车
 */
public class AuDiFactory extends AbstractFactory {
    @Override
    public AbstractProduct manufacture() {
        return new AuDiCar();
    }
}

 

(5) The outside world creates instances of different specific product classes by calling the methods of specific factory classes

package cn.itchao.factory.method;

public class Test01 {
    public static void main(String[] args) {
        //客户需要比亚迪汽车
        BydFactory bydFactory = new BydFactory();
        bydFactory.manufacture().show();
        //客户需要奥迪汽车
        AuDiFactory auDiFactory = new AuDiFactory();
        auDiFactory.manufacture().show();
    }
}

Test Results:

Summarize:

  Advantages:
    (1) It is more in line with the open-closed principle. When adding a new product, you only need to add the corresponding specific product category and the corresponding factory subclass. The
         simple factory mode needs to modify the judgment logic of the factory class.

     (2) In line with the principle of single responsibility, each specific factory class is only responsible for creating the corresponding product.
         The factory class in a simple factory has complex switch logic judgments or if judgments
     (3) Do not use static factory methods, which can form a hierarchy based on inheritance Structure,
         the factory class of the simple factory pattern uses static factory methods. The
     factory method pattern can be said to be a further abstraction and extension of the simple factory pattern. While retaining the encapsulation advantages of the simple factory, it makes the extension simple and inheritance feasible. Added polymorphism

3. Abstract Factory

The factory method mentioned above has a serious deficiency. A specific factory can only create one type of product, but in actual work, a factory often needs to produce multiple types of products. In order to solve this deficiency, we have another A New Design Pattern: Abstract Factory Pattern

Definition: Abstract Factory Pattern, namely Abstract Factory Pattern, provides an interface for creating a series of related or interdependent objects without specifying their specific classes; specific factories are responsible for implementing specific product instances

Role: Allows the use of abstract interfaces to create a set of related products without needing to know or care what the actual product is actually produced, so that it can be decoupled from the concrete product.

Code case:

(1) Create an abstract factory class and define the public interface of the concrete factory

 

 

 

 

 

 

Reference: https://blog.csdn.net/carson_ho/article/details/54910287

 

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325241227&siteId=291194637