Design pattern-Abstract Factory mode (C #)

Source: Internet
Author: User

Design Patterns-Abstract Factory mode (JAVA)

In the abstract factory model, a specific factory can produce a group of related specific products, such a group of products become product family, product family of each product belongs to a product inheritance and other hierarchical structure. Abstract Factory mode can be used when the specific product produced by the system is not a simple object, but a number of specific products that are in different product hierarchy and that belong to different types.

The biggest difference between the abstract factory model and the factory method pattern is that the factory method model is for a product hierarchy, whereas the abstract factory model requires multiple product hierarchy structures, and a plant hierarchy can be responsible for creating product objects in multiple different product hierarchy structures. Abstract Factory mode is simpler and more efficient than the factory method mode when a factory hierarchy can create all objects in a product family that belong to different product hierarchy structures.

Abstract Factory mode provides a solution for creating a set of objects. In contrast to the factory method model, the specific factories in the abstract factory model are not just creating a product, but are responsible for creating a set of products.

Abstract Factory Schema Definition: Provides an interface to create a series of related or interdependent objects without specifying their specific classes.

I. Structure of the abstract factory model

In abstract Factory mode, each specific factory provides multiple factory methods to produce a variety of different types of products that form a product family.

The abstract factory pattern consists of the following 4 roles:

(1) abstractfactory (abstract Factory): It declares a set of methods for creating a family of products, each of which corresponds to a product.

(2) Concretefactory (Factory specific): It implements the method of creating products declared in the abstract factory, producing a set of specific products that form a product family, each of which is in a product hierarchy.

(3) Abstractproduct (abstract product): It declares the interface for each product and declares the business method of the product in the abstract product.

(4) Concreteproduct (specific product): It defines specific plant-specific product objects, which implement the business methods declared in the abstract product interface.

Second, the realization of the abstract factory model

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201203/16/0_13318591595Q24.gif "alt=" 0_ 13318591595q24.gif "/>

Multiple factory methods are declared in an abstract factory to create different types of products, an abstract factory can be an interface, or it can be an abstract class or a concrete class.

Abstract class Abstractfactory

{

Public abstract abstractproduct createproduct (); Factory method

......

}

The concrete factory implements the abstract factory, each specific factory method creates a specific product object, and the product object created by the same factory is a product family. For each specific factory class:

Class Concretefactory:abstractfactory

{

Factory method

public override Abstractproduct Createproduct ()

{

return new Concreteproduct ();

}

......

}

As with the factory method pattern, abstract Factory mode can also provide a set of overloaded factory methods for each product, creating product objects in different ways.

III. application of Abstract factory model

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201203/16/0_1331859200u1VV.gif "alt=" 0_ 1331859200u1vv.gif "/>

In the above class diagram, the hatchback and sedan are called two different hierarchical structures, while 2.0-displacement vehicles and 2.4-displacement vehicles are called two different product families. A little more specific, 2.0-Displacement hatchback and 2.4-displacement hatchback belong to the same grade structure, 2.0-displacement sedan and 2.4-displacement sedan belong to another grade structure, while the 2.0-displacement hatchback and 2.0-displacement sedan belong to the same product family, 2.4-displacement hatchback and 2.4-displacement sedan belong to another product family.

Understand the concept of hierarchical structure and product family, understand the difference between the factory method pattern and the abstract factory model, if the factory products all belong to the same hierarchical structure, it belongs to the factory method mode; If the factory product comes from multiple hierarchies, it is an abstract factory pattern. In this case, If a factory model offers a 2.0-displacement hatchback and a 2.4-displacement hatchback, then he's in the Factory mode. If a factory model offers 2.4-displacement hatchback and 2.4-displacement sedan two products, then this factory model is abstract Factory mode, because the product he offers is divided into two different hierarchical structures. Of course, if a factory provides all four models of products, because the product is divided into two hierarchical structure, he certainly belongs to the abstract factory model.

Abstract Factory mode code:

Package Abstractfactorymodel;


Interface IProduct1 {

public void Show ();

}

Interface iproduct2{

public void Show ();

}

Class Product1 implements iproduct1{

public void Show () {

System.out.println ("This is a Type 1 product");

}

}

Class Product2 implements iproduct2{

public void Show () {

System.out.println ("This is a Type 2 product");

}

}

Interface ifactory{

Public IProduct1 CreateProduct1 ();

Public IProduct2 createProduct2 ();

}

Class Factory implements ifactory{

Public IProduct1 CreateProduct1 () {

return new Product1 ();

}

Public IProduct2 CreateProduct2 () {

return new Product2 ();

}

}


Client code:

Package Abstractfactorymodel;


public class Client {


public static void Main (string[] args) {

TODO auto-generated Method Stub

Ifactory factory=new Factory ();

Factory.createproduct1 (). Show ();

Factory.createproduct2 (). Show ();

}


}

Iv. advantages and disadvantages of the abstract factory model

Advantages:

(1) Isolate the generation of specific classes so that the client does not need to know what is being created. Just changing the instance of a specific factory can change the behavior of the entire software system to some extent.

(2) When multiple objects in a product family are designed to work together, it ensures that the client always uses only the same product family.

(3) The new product family is very convenient, no need to modify the existing system, in line with the opening and shutting principle

Disadvantages:

Add new product grade structure trouble, need to make large-scale modification of the original system, even need to modify the abstraction layer code, this obviously will bring great inconvenience, violate the opening and shutting principle.

V. Use of the scene

(1) A system should not rely on the details of how a product class instance is created, composed, and expressed, which is important for all types of factory patterns, and users do not need to be concerned about the creation of objects and decouple the creation and use of objects.

(2) There are more than one product family in the system, but each time only use one of the product families, can be configured by the way the user can dynamically change the product family, can also easily add new product families.

(3) products belonging to the same product family will be used together, this constraint must be reflected in the design of the system. Products in the same product family can be objects that have no relationship, but they all have some common constraints, like buttons and file boxes under an operating system, buttons and file boxes are not directly related, but they all belong to an operating system. At this point there is a common constraint: the type of the operating system.

(4) Product grade structure is stable, after the design is completed, will not add new product grade structure to the system or delete the existing product hierarchy structure.

This article is from the "Rangers" blog, please be sure to keep this source http://ccnupxz.blog.51cto.com/8803964/1875205

Design pattern-Abstract Factory mode (C #)

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.