Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

preface

A design pattern is a set of repeated, familiar, catalogued code design lessons. It describes some recurring problems in the software design process, and the solutions to this problem. That is to say, it is a series of routines to solve a specific problem. It is a summary of the code design experience of predecessors. It has a certain universality and can be used repeatedly. The goal is to improve code reusability, code readability, and code reliability.

It is a summary of the most valuable lessons learned in object-oriented design. Before speaking may be relatively familiar with some of the design pattern, but carefully pondering or some questions, just at present relatively have more spare time, you can re-learn the design pattern!

This article is about the abstract factory pattern. Contains the concepts and differences of the abstract factory pattern

define

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying concrete classes. Head First Design Pattern

As described by this definition, there seems to be no difference from the factory method pattern. It also provides an abstract class or interface that provides a method to create an object without specifying the concrete class. For factory Method Patterns see This article, Easy to Understand Design Patterns (Creative) 2. Factory Method Patterns

Here is a general UML diagram of an abstract factory:

As far as the graph is concerned, the factory method pattern provides only one way to create an instance. For the structure on the diagram, a specific factory needs to implement multiple instance creation methods specified by the interface.

Depending on the specific factory being injected, the client code ends up fetching specific instances of a set of products

Public class Person {FoodFactory factory = new (); Public void eat() {// get Food = factory. Ceate; Lao Xu Fen Bao = factory. Ceate baozi (" fen bao "); // eat system.out.println (" I'm eating "+ Lao Xu noodles + Lao Xu noodles); }}Copy the code

doubt

It’s true that there are more methods to abstract a factory, but the factory method pattern can do the same. What’s the difference?

The factory method pattern provides only one abstract factory method, and the return type of this unique factory method is relatively large if the scope of objects created by the abstract factory is the same. And it’s up to the subclasses to provide what concrete classes are created under the large type that are ultimately received by the class. In the case of abstract methods, the types to be created are specified directly. In this way, the type is neither concrete nor abstract, making it easy to use, and each set of products has its own point of use.

A factory method lets subclasses decide which class to instantiate, whereas an abstract factory decides which class to instantiate.

Where are these factory patterns used in Java?

Simple factory

 public interface BeanFactory {
     Object getBean(String beanName);
 }
Copy the code

Spring’s BeanFactory is a simple factory pattern, passing in the bean name through the getBean method to get the corresponding instance. The concrete instance is created at the beginning of the configuration scan, loaded into the map, and returned when fetched by getBean. Simple factory Map implementation is adopted.

The factory method

There is also a factory method pattern embodiment of AbstractFactoryBeans in SpringAll other operations on the object are done by other methods, and the creation of the createInstance abstract method is implemented by its subclasses. These subclasses they don’t have to do anything subsequent or prior to the object just to create it. All other operations are guaranteed to follow a fixed process, but with a variety of objects created.

The abstract factory

For example, in the java.sql package, there is an interface called Connection (factory interface), which should be familiar to database Connection objects. Need various database vendors to provide implementation like MySQL, Oracle, etc

As shown below:

This interface explicitly specifies the methods for each product to be created, and either MySQL or Oracle has a set of implementations to create instances of concrete classes in the product family. They also have a specific set of implementations for product classes. When a factory instance is provided by MySQL, the Statement (abstract product) created is MySQL’s concrete class for that product. However, the code is always abstract. There are only interfaces such as Connection in the factory without concrete implementation classes, and the product BLOBs, CLOBs, and statements obtained in the code are also received by these interfaces.

When the type of a specific set of products used needs to change, it is only necessary to configure the new plant to produce the current set of products. The entire code flow is abstract and fixed. Change the code without specific changes.

conclusion

Recall that the entire factory pattern is used to encapsulate the creation of objects, thereby reducing the coupling of programs to concrete classes. It follows our dependency inversion principle — avoid relying on concrete types and rely on abstractions as much as possible. The factory approach differs from the abstract factory approach in that the former postpones the instantiation of a class to subclasses, while the latter focuses more on the specification of a set of product types on the interface, and each implementation class or subclass implements the creation of its own set of products. It can be said that the factory pattern is a best practice for abstract programming.