In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
It leads to the design problem of factory pattern.
◆ 1. In order to improve cohesion (Cohesion) and loose coupling (Coupling), we often abstract the common interfaces of some classes to form abstract base classes or interfaces. In this way, we can achieve the goal of polymorphism by declaring a pointer to the base class to point to the actual subclass implementation. A problem that can easily arise here is that many subclasses inherit from the abstract base class, and we have to write code such as new × ×; every time we need to use the subclass. Here are two questions:
Customer programmers must know the names of the actual subclasses (naming will be a difficult problem to deal with when the system is complex, and some names may not be very readable and memorable in order to deal with possible name conflicts, regardless of the strange personal preferences of different programmers).
The extensibility and maintenance of the program are becoming more and more difficult.
◆ 2. There is also a situation where you do not know which specific subclass to instantiate in the parent class. Suppose we want to use in class A that class B is an abstract parent class, and we don't know which subclass of B to instantiate in A, but it can be known in subclass D of class A. In A, we can't directly use statements like new, because we don't know what xxx is at all.
The above two questions lead to the two most important functions of the factory model:
Define the interface for creating objects, encapsulating the creation of objects
Causes the work of the materialized class to be delayed to the subclass.
For the factory pattern, in order to better solve the problems of many situations, it is divided into three categories: simple factory pattern (Simple Factory), factory method pattern (Factory Method), abstract factory pattern (Abstract Factory). Let's take care of them one by one.
Simple factory
Specific situation: there is a soap factory that produces all kinds of soaps, including Safeguard, Xia Shilian, Na Aisi and so on. Model this soap factory.
The UML figure is as follows:
For the structure diagram of a simple design pattern, we can clearly see its composition:
1) Factory role: this is the core of this model, which contains certain business logic and judgment logic.
2) Abstract product role: it is generally the parent class of concrete product inheritance or the interface of implementation.
3) specific product role: the object created by the factory class is an instance of this role.
The purpose of the simple design pattern is simple: to define an interface for creating objects.
Disadvantages: the modification is not closed, you have to modify the factory for newly added products. Violates the famous opening and closing rule (OCP).
Code implementation:
# include usingnamespace std; enum PRODUCTTYPE {SFJ,XSL,NAS}; class soapBase {public: virtual ~ soapBase () {}; virtualvoid show () = 0;}; class SFJSoap:public soapBase {public: void show () {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.