In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Design pattern (3)-- Abstract Factory pattern 1, Abstract Factory pattern 1, Abstract Factory pattern
Definition: provides an interface for creating a set of related or interdependent objects without specifying their specific classes.
UML class diagram:
2. Abstract factory pattern roles
(1) Abstract factory AbstractFactory: abstract factory class, which provides interfaces CreateProductA and CreateProductB for creating two products, which are implemented by derived concrete factory classes
(2) specific factory: including specific factory FactoryM and specific factory FactoryN. Specific factory FactoryM is used to produce specific product MProductA and specific product MProductB, and specific factory FactoryN is used to produce specific product NProductA and specific product NProductB.
(3) Abstract products: AbstractProductA and AbstractProductB: represent two different types of products, which are implemented by specific product derived classes.
(4) concrete products: including concrete products MProductA and NProductA corresponding to abstract product AbstractProductA, and concrete products MProductB and NProductB corresponding to abstract product AbstractProductB.
3. Advantages and disadvantages of abstract factory model.
Advantages: the abstract factory pattern is an improvement on the factory method pattern, which is used to deal with situations where there is more than one category of products. In addition to the advantages of the factory method pattern, the most important advantage of the abstract factory pattern is that the product family can be constrained within the class. The so-called product family generally has a certain association more or less, and the abstract factory pattern can define and describe the association relationship of the product family within the class, without the need to introduce a new class for management.
The abstract factory pattern separates the concrete class, which separates the implementation of the customer from the class.
Disadvantages:
It is difficult to support new types of products and is not easy to expand. The expansion of the product family will be a laborious task, and if a new product needs to be added to the product family, almost all factory classes need to be modified. Therefore, when using the abstract factory pattern, the division of the product hierarchy is very important.
The difference between the abstract factory pattern and the factory method pattern lies in the complexity of creating objects. And the abstract factory pattern is the most abstract and general of the three.
4. Abstract factory pattern usage scenario
Abstract factory pattern usage scenarios:
A. the abstract factory pattern can be used when the object you need to create is a series of interrelated or interdependent product families. In an inheritance system, if there are multiple hierarchical structures (that is, there are multiple abstract classes), and there are certain associations or constraints among the implementation classes belonging to each hierarchical structure, the abstract factory pattern can be used. If there are no associations or constraints between the implementation classes in each hierarchy, it is more appropriate to use multiple separate factories to create the product.
B. A system should not rely on the details of how product class instances are created, combined, and expressed.
C. Products belonging to the same product family are used together, and constraints must be reflected in the design of the system.
D, the system provides a library of product classes, and all products appear with the same interface, so that the client does not depend on the implementation.
2. Abstract factory pattern implements abstract factory AbstractFactory class: # ifndef ABSTRACTFACTORY_H#define ABSTRACTFACTORY_H class AbstractProductA;class AbstractProductB;class AbstractFactory {public: virtual AbstractProductA* createProductA () = 0; virtual AbstractProductB* createProductB () = 0;} # endif / / ABSTRACTFACTORY_HFactoryM factory class: # ifndef FACTORYM_H#define FACTORYM_H#include "AbstractFactory.h" # include "MProductA.h" # include "MProductB.h" class FactoryM: public AbstractFactory {public: virtual AbstractProductA* createProductA () {AbstractProductA* product = new MProductA (); return product;} virtual AbstractProductB* createProductB () {AbstractProductB* product = new MProductB (); return product;}} # endif / / FACTORYM_HFactoryN factory class: # ifndef FACTORYN_H#define FACTORYN_H#include "AbstractFactory.h" # include "NProductA.h" # include "NProductB.h" class FactoryN: public AbstractFactory {public: virtual AbstractProductA* createProductA () {AbstractProductA* product = new NProductA (); return product;} virtual AbstractProductB* createProductB () {AbstractProductB* product = new NProductB (); return product;}} # endif / / FACTORYN_H AbstractProductA Abstract Product Class A: # ifndef ABSTRACTPRODUCTA_H#define ABSTRACTPRODUCTA_H class AbstractProductA {public: virtual void productMethod () = 0;}; # endif / / ABSTRACTPRODUCTA_HMProductA Product Class: # ifndef MPRODUCTA_H#define MPRODUCTA_H#include "AbstractProductA.h" # include using std::endl;using std::cout; class MProductA: public AbstractProductA {public: virtual void productMethod () {cout productMethod (); delete factoryM; delete mproductA; delete mproductB Delete factoryN; delete nproductA; delete nproductB; return 0;}
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.