Simple Factory Design Pattern

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

Simple Factory Design Pattern

A design pattern is not a grammar, it is a clever way of writing, which can make the program more flexible.
Architectural patterns are bigger than design patterns, architectural patterns are strategies, and design patterns are tactics.

Design patterns are divided into 3 types: creational, behavioral, and structural.
Simple factory design patterns belong to creational design patterns, but do not belong to the scope of 23 design patterns. They belong to the entry pattern of factory methods among the 23 design patterns, also known as static. Factory method pattern. The simple factory pattern is that a factory object decides which instance of the product class to create.

The advantage of the simple factory design pattern is that object creation is hidden, and objects can be replaced at will without relying on objects. In the factory pattern, all the methods in the factory class return the same abstract product class. The upgrade and expansion of the code only needs to inject new functional methods into the factory class to return the abstract product class, or write another interface to add this The abstract product class is forced to be converted into this interface, so that the concrete product class can implement it.

Example of a conductor:

image

image

Unzip the Jfreechart compressed package--open lib--open the chart folder--find the ChartFactory.class file--drag into IDEA for decompilation--click structure to view the structure Jfreechart is made in a typical factory mode. Jfreechart is a product in the engineering mode. All returned in all creat methods are the same Jfreechart. No matter what graphics are created, this object is created. Extensions and upgrades only need to inject new ones without changing the source code. The creat method code can be used. Special methods are written on another interface. The entire pattern does not depend on subclasses, that is, implementation classes, and does not expose implementation classes.

image

Guess you like

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