Abstract factory pattern abstract factory pattern

Source: Internet
Author: User
Tags abstract definition define abstract
1. Abstract Factory model Overview

Abstract Factory mode is the most common and abstract form of representation in the factory mode. It is a further expansion of the factory method mode. The factory method model can manage the construction of a type of product and hand over the construction process of a specific product to a specific subclass. What if we need to manage the construction process of two or more products? Naturally, you can think of multiple factory methods. The problem with this is that, first, the factories are too scattered and difficult to manage; second, if there is a close relationship between these products, their construction process cannot be completely separated, there must be a complex dependency between multiple builders. Therefore, a better way is: Since the construction process of these products is related, they will be concentrated in a large construction factory. This is the complexity of the product to be created. If it is just a car tire factory, the production of products is limited to only one type of tires (may have different models), this is a typical factory method problem. If it is a more responsible automobile manufacturing plant, in addition to producing automobile tires, it also produces automobile engines, body and other products (there may be different models ), there is a close relationship between these products. For example, a specific type of car consists of a specific type of tires, engine and drivers-this combination is a closely related series, so, this is an abstract factory problem. Gof's intention to describe the abstract factory pattern is to provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes. 2. Abstract Factory model example there is a car manufacturer that produces two types of cars: commercial vehicles and racing cars. Assume that a car is composed of two parts: the car body and the engine. In this way, the method for producing two kinds of cars is simple: you only need to assemble different bodies and engines: commercial vehicles use the bodya and Benz engines, while racing cars use the bodyb and Ferrari engines. For example, the program code is as follows: namespace abstractfactory
...{
// Abstract definition of vehicle body
Public abstract class body
...{
Public abstract void show ();
}
// Body
Public class bodya: Body
...{
Public override void show ()
...{
Console. Write ("bodya ");
}
}
// Body B
Public class bodyb: Body
...{
Public override void show ()
...{
Console. Write ("bodyb ");
}
}
// Engine abstract definition
Public abstract class Engine
...{
Public abstract void show ();
}
// Benz engine
Public class Benz: Engine
...{
Public override void show ()
...{
Console. Write ("Benz ");
}
}
// Ferrari engine
Public class Ferrari: Engine
...{
Public override void show ()
...{
Console. Write ("Ferrari ");
}
}
// Abstract definition of automobile manufacturing plants
Public abstract class carfactory
...{
Public Abstract Body getbody (); // body factory Method
Public abstract engine getengine (); // Engine Factory method
Public Virtual void produce ()
...{
Console. Write ("this is a car with the body ");
Getbody (). Show ();
Console. Write ("and the engine ");
Getengine (). Show ();
}
}
// Business vehicle manufacturer
Public class businesscarbranch: carfactory
...{
Public override body getbody ()
...{
Return new bodya ();
}
 
Public Override engine getengine ()
...{
Return New Benz ();
}
}
// Racing car manufacturing plant
Public class sportscarbranch: carfactory
...{
Public override body getbody ()
...{
Return new bodyb ();
}
 
Public Override engine getengine ()
...{
Return new Ferrari ();
}
}
// Client
Class Client
...{
Static void main (string [] ARGs)
...{
Carfactory factory = new sportscarbranch ();
Factory. Produce ();
Console. readkey ();
}
}
}
3. Abstract Factory model the role and structure the general structure of the abstract factory model is shown in:
  • Abstractproducta/B: Abstract Product role, defining abstract interfaces of a class of products.
  • Concreteproducta1/A2/B1/B2: specific product roles and specific products produced by the factory implement the corresponding abstract product interfaces.
  • Abstractfactory: Abstract Factory roles that define abstract methods for factory production products.
  • Concretefactory1/2: A factory role that abstracts factory-defined methods and produces specific products.
  • Client/Caller: Client/calling program, which only depends on the abstract factory and abstract product definition.
4. abstract Factory mode abstract Abstract Factory mode is the most general and abstract factory method. It encapsulates the details of product creation to the maximum extent and completely isolates customers' dependencies on specific products. The customer can replace the product at a specific factory without knowing the product creation details. The abstract factory model has good support for the increase of product series. For example, in the above example, if you want to add a new product series, such as industrial vehicles, you only need to add industrial vehicle manufacturing plants, specific industrial vehicle parts (body, engine) you can. This is a loyal embodiment of the OCP principle. Abstract Factory mode is applicable to the following scenarios (from gof design mode):
  • A system must be independent of the creation, combination, and representation of its products.
  • A system must be configured by one of multiple product generations. (The above two types of cars)
  • When you want to emphasize the design of a series of related product objects, you can use them together. (Multiple components constitute an automobile)
  • Provides a class library and only displays interfaces.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.