Object creation mode (skip the direct use of new)

Object creation mode; it is through the object creation mode to bypass new, to avoid the tight coupling (depending on the specific class) caused in the process of object creation (new),
including
factory mode;
abstract factory;
prototype mode;
builder;
4 In fact, it is based on a little change in the factory model.
The factory pattern is to define an interface for creating objects and let the subclass decide which class to instantiate. The factory method is to delay the instantiation of a class to the subclass.

The object creation mode is actually to solve the problem of relying on the implementation class subclass formed by the new on the right side of the equal sign during assignment.
Factory mode;
such as file cutting class
ISplitter * splitter = new filestream(); the
left side of the equal sign can use polymorphism to solve the early binding problem, but the lack of the right side of the equal sign still depends on the specific subclass, then the class of this statement is called It is not a class with a very object-oriented design pattern, because it does not have the stability of transplantation, it needs to be changed according to the specific real class. Then the engineering mode is a mode created to solve this early binding.
The main method is to create an abstract class (factory class), and call it through the abstract class polymorphic pointer on the calling side. On the implementation side, there is a created virtual method of the object in the factory class, and then each instance class inherits it and returns its own new class, thus reaching the effect of delaying an instance of a class.
Insert picture description here

Interface-oriented programming, the most intuitive and most basic is that you have to declare a variable object as an abstract base class

The abstract factory is actually a little adjustment based on the factory model, which is to merge multiple factories into one. But the premise is also that these merged factory methods correspond to a set of related links.
For example, the database operation
database is divided into many types such as SQLite, MySQL, oralSql, etc., and each class has a corresponding connect, read, command and other methods, it is precisely because these methods are related, so they can use abstraction Factory (to build a factory, there are three methods to create these three objects, because they are related to each other, the operation must be the same database), but if the three of them are not connected, then you can only use the factory method, then you must Many types of multi-key.

Prototype mode is not used much, but it needs to be understood. The main method used is a cloning method.
Insert picture description here

Guess you like

Origin blog.csdn.net/zw1996/article/details/97618924