This is the 10th day of my participation in the August Text Challenge.More challenges in August

The factory pattern

Factory, as its name implies, is to create products. It can be divided into simple factory mode and factory method mode according to whether the product is a specific product or a specific factory. It can be divided into factory method mode and abstract factory mode according to the degree of abstraction of the factory. This pattern is used to encapsulate and manage object creation and is a creation pattern.Copy the code

Simple factory

Schema is the easiest way to manage object creation, because it simply encapsulates the creation of different types of objects.Copy the code

Advantages: The simple factory pattern can determine which concrete class objects should be created based on information given from the outside world. It clearly distinguishes their responsibilities and powers, which is conducive to the optimization of the entire software architecture.

disadvantages: It is clear that the factory class centralizes the creation logic of all instances and is liable to violate GRASPR’s highly cohesive responsibility assignment principle

demo

1. Abstract products

public interface clothes {
    void make(a);
}
Copy the code

2. Specific products

public class Lining implements clothes {
    public Lining(a) {
        this.make();
    }
    @Override
    public void make(a) {
        System.out.println("Make Li Ning clothes!"); }}public class Erke implements clothes {
    public Erke(a) {
        this.make();
    }
    @Override
    public void make(a) {
        System.out.println("Make hongxing Erke clothes!"); }}Copy the code

3. Specific factory

public class ClothesFactory {
    publicclothesmakeClothes(String name) {
        if ("Lining".equals(name)) {
            return new Lining(); 
        }
        if ("Erke".equals(name)) {
            return new Erke();
        }
        return null; }}Copy the code

4, presentations,

public class Client {
    public static void main(String[] args) {
        ClothesFactory factory = new ClothesFactory();
        factory.makeClothes("Lining"); // Make Li Ning clothes!
        factory.makeClothes("Erke"); // Make hongxing Erke clothes!}}Copy the code

The factory pattern

In contrast to the simple factory pattern, where the factory is responsible for producing all products, the factory method pattern distributes the task of producing specific products to specific product factories; Instead of being responsible for all product creation, the core factory class becomes an abstract factory role, giving only the interfaces that concrete factory subclasses must implement and not the details of which product classes should be instantiated.Copy the code

advantages

  • Users only need to know the name of the specific factory to get the product they want, without knowing the specific creation process of the product.
  • Increased flexibility, for the creation of new products, only need to write a corresponding factory class.
  • A typical decoupling framework. A high-level module only needs to know the abstract class of the product and does not need to care about other implementation classes, satisfying Demeter’s law, dependency inversion principle and Richter’s substitution principle.

disadvantages

  • It is easy to have too many classes and add complexity
  • The system is abstract and difficult to understand
  • Abstract products can only produce one product, which can be solved by using the abstract factory model.

demo

Make changes on the simple factory demoCopy the code

Change ClothesFactory to abstract factory

public interface ClothesFactory {
    clothesmakeClothes(a);
}
Copy the code

2. Add factories for specific products

public class LiningFactory implements ClothesFactory {
    @Override
    publicclothesmakeClothes(a) {
        return newLining(); }}public class ErkeFactory implements ClothesFactory{
    @Override
    publicclothesmakeClothes(a) {
        return newErke(); }}Copy the code

3, presentations,

public class Client {
    public static void main(String[] args) {
        ClothesFactory lining = new LiningFactory();
        lining.makeClothes(); // Make Li Ning clothes!
        ClothesFactory erke = new ErkeFactory();
        erke.makeClothes(); // Make hongxing Erke clothes!}}Copy the code

The abstract factory

A product needs to be made in the corresponding concrete product factory, and other products of the product series can also be made in the concrete product factory. Therefore, the difference between the factory model and the abstract factory model is that a concrete factory produces one product and a concrete factory produces multiple different productsCopy the code

demo

Make changes based on the factory modelCopy the code

1. Add an abstract product

public interface Shoe {
    void make(a);
}
Copy the code

2. Add specific products

public class LiningShoe implements Shoe {
    public LiningShoe(a) {
        this.make();
    }
    @Override
    public void make(a) {
        System.out.println("Make Li Ning shoes!"); }}public class ErkeShoe implements Shoe{
    public ErkeShoe(a) {
        this.make();
    }
    @Override
    public void make(a) {
        System.out.println("Make Hongxing Erke shoes!"); }}Copy the code

Modify the abstract factory

public interface ClothesFactory {
    clothesmakeClothes(a);
    Shoe makeShoe(a);
}
Copy the code

4. Modify specific factories

public class LiningFactory implements ClothesFactory {
    @Override
    publicclothesmakeClothes(a) {
        return new Lining();
    }
    @Override
    public Shoe makeShoe(a) {
        return newLiningShoe(); }}public class ErkeFactory implements ClothesFactory{
    @Override
    publicclothesmakeClothes(a) {
        return new Erke();
    }
    @Override
    public Shoe makeShoe(a) {
        return newErkeShoe(); }}Copy the code

5, presentations,

public class Client {
    public static void main(String[] args) {
        ClothesFactory lining = new LiningFactory();
        lining.makeClothes(); // Make Li Ning clothes!
        lining.makeShoe(); // Make Li Ning shoes!
        ClothesFactory erke = new ErkeFactory();
        erke.makeClothes(); // Make hongxing Erke clothes!
        erke.makeShoe(); // Make hongxing Erke shoes!}}Copy the code

conclusion

The factory pattern is ultimately designed to decouple, reduce code duplication, and reduce maintenance costs

The three factory modes have their own application scenarios and can be used according to the actual scenarios