The Factory Pattern of Design Patterns

Get into the habit of writing together! This is the 17th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Introduction

The factory pattern is the most commonly used pattern for instantiating objects. It is a pattern that replaces the new operation with a factory method. The famous Jive forum uses a lot of factory patterns, which can be seen everywhere in the Java program system. Because the factory pattern is equivalent to creating new instance objects, we often need to generate instance objects according to the class Class, such as A a=new A() The factory pattern is also used to create instance objects, so we will need more attention when new in the future. Whether you can consider using the factory pattern, although doing so may do some more work, it will bring greater scalability to your system and minimize the amount of modification.

Masaaki

Thought Question How can the code that instantiates concrete classes be abstracted from the application, or encapsulated so that they do not interfere with other parts of the application? P111

  • Put the code for instantiating a specific class into an object for management, and decide to instantiate a specific class through different input parameters

Simple Factory
is not one of the 23 GOF design patterns, but more like a programming idiom. P117

image.png

Features

  • Instances are usually created using static methods, but there is no way to change the behavior of the created method through inheritance. P115

shortcoming

  • In violation of the open-closed principle, the factory class needs to be modified when adding products.

Factory Method Pattern

An interface for creating objects is defined, but it is up to the subclass to decide which class to instantiate. P134

image.png

Features

  • Factory methods let classes defer instantiation to subclasses. P134
  • "Decision" refers to which subclass to choose, which determines which subclass is actually created. P134
  • Adding a product or changing a product's implementation does not affect the factory interface. P135

shortcoming

  • When new products are added, new factories need to be added, increasing the complexity of the code.

The abstract factory pattern
provides an interface for creating families of related or dependent objects without explicitly specifying a concrete class. P156

image.png

Features

  • The methods of an abstract factory are often implemented as factory methods. P158
  • A collection of related products. P159

shortcoming

  • When a new related product is added, the interface and implementation class need to be modified. P159

Design Principles

  • Dependency Inversion Principle : Depend on abstraction, not on concrete classes. P139

High-level components cannot depend on low-level components, and both high-level components and low-level components should depend on abstractions. P139
low-level components depend on high-level abstractions. P141
Guidelines to avoid violating the Dependency Inversion Principle (you can try to follow it according to the actual situation) P143
Variables cannot hold references to specific classes:
that is, without importing specific classes, you can use factories to avoid references to specific classes
Do not let classes derive from specific classes:
[Explanation in the book] You may depend on concrete classes when you use them, but let the classes be derived from interfaces or abstract classes.
[My own thoughts] As long as the concrete classes are derived from interfaces or abstract classes, the classes can be derived from the concrete classes
without overriding the base class. Implemented methods:
[Explanation in the book] The implemented methods in the base class should be shared by all subclasses.
[My own thoughts] It was also mentioned earlier in the book that the base class can provide default methods, and subclasses can override them as their own. Implement P135\

what you think

  • In fact, when I usually write code, I often reverse my way of thinking. For example, when relying on different implementations of an interface to complete different small functions, I will not write the specific implementation first, but complete the upper-level code framework according to the interface. , and then complete each implementation class in detail.
  • Although the book talks about the difference between factory method and abstract factory, I still feel that the difference between the two is not big, but it is a little different in application scenarios. The factory method refers to the creation of a class of products, while the abstract factory key is related to multiple classes of products. An abstract factory is a factory method when there is only one class of related products.

Guess you like

Origin juejin.im/post/7087872450439938061