Abstraction Factory (abstract Factory) mode

Source: Internet
Author: User
-->
Add to Favorites

Contact Us

Web Search
--> Home Alternative life. NET technology database Technology JavaScript Technology Web page design related Technical topics project System Blog website Forum
Ease to fly → project system → software methodology → design pattern

Abstract Factory (Abstracts Factory) mode

Abstract Factory pattern is the most abstract and most general form of the factory pattern in all forms.

In order to facilitate the introduction of the abstract factory model, introduce a new concept: Product family (Products Family). The so-called product family, is located in different product hierarchy structure, the function of the related products composed of family. As shown in figure:

There are four product families in the figure, distributed in three different product hierarchy. The only way to determine a product is to identify the product family and the hierarchy it belongs to.

Introduction of abstract Factory model

An abstract factory is a factory hierarchy that creates all the objects in a family of products that are divided into different product hierarchy structures. If you use a graph to describe, the following figure:

II. structure of the Abstract factory model:


The things depicted in the picture are described in the product family as follows:

Abstract Factory role: This role is at the heart of the factory approach model, which is irrelevant to the business logic of the application system.

Specific factory (concrete Factory) Role: This role creates an instance of the product directly under the call of the client. This role contains the logic to choose the right product object, which is closely related to the business logic of the application system.

Abstract Product Role: The class that holds this role is the parent class of objects created by the factory method pattern, or the interfaces they share.

Specific product (concrete product) Role: Any Product object created by the abstract factory pattern is an instance of a specific product class. This is what the client ultimately needs, and its interior must be full of the business logic of the application system.


Iii. Examples of procedures:

This program demonstrates the structure of an abstract factory and does not have any real value in itself. Abstract Factory Pattern--structural example
Using System;

"Abstractfactory"
Abstract class Abstractfactory
{
Methods
Abstract public abstractproducta createproducta ();
Abstract public ABSTRACTPRODUCTB CREATEPRODUCTB ();
}

"ConcreteFactory1"
Class Concretefactory1:abstractfactory
{
Methods
Override public Abstractproducta createproducta ()
{
return new ProductA1 ();
}
Override public ABSTRACTPRODUCTB CREATEPRODUCTB ()
{
return new ProductB1 ();
}
}

"ConcreteFactory2"
Class Concretefactory2:abstractfactory
{
Methods
Override public Abstractproducta createproducta ()
{
return new ProductA2 ();
}

Override public ABSTRACTPRODUCTB CREATEPRODUCTB ()
{
return new ProductB2 ();
}
}

"Abstractproducta"
Abstract class Abstractproducta
{
}

"ABSTRACTPRODUCTB"
Abstract class ABSTRACTPRODUCTB
{
Methods
Abstract public void interact (abstractproducta a);
}

"ProductA1"
Class Producta1:abstractproducta
{
}

"ProductB1"
Class PRODUCTB1:ABSTRACTPRODUCTB
{
Methods
Override public void interact (Abstractproducta a)
{
Console.WriteLine (this + "interacts with" + a);
}
}

"ProductA2"
Class Producta2:abstractproducta

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.