C # Abstract Factory mode

Source: Internet
Author: User

First look at the general structure of the abstract factory

To understand this type of diagram above, we must first define a concept,

Product Family:

In the above list of products, there are two product families, one is "specific product a--1" and "specific product b--1" a family composed of

There is also a "specific product a--2" and "specific product b--2" composed of a family.

Product family is a family of products that are related to function in different product hierarchy.

Here is the introduction of the abstract factory (some of the content is jerky, you can read the Demo back to browse)

The following is a definition of the abstract factory:

Provides an interface to create a series of related or interdependent objects without specifying their specific classes.

And look at the clothes. Detailed class diagram

In fact, from the above class diagram can be seen, each of the specific factory, it is only responsible for creating a product family of products in the instance,

Specific factories derived from abstract factories that produce the same products (these products inherit from the same parent class),

For example, createproducta in ConcreteFactory1 and ConcreteFactory2 is a way to produce abstractproducta this type of product,

But the implementation of the product is different, such as ConcreteFactory1 in the createproducta to achieve is to produce a ConcreteProductA-1 product,

And the createproducta in ConcreteFactory2 is to produce a ConcreteProductA-2 product,

In general, different specific factories produce different product families,

The abstract factory defines an interface that is responsible for creating a set of products (that is, a product family).

For example, there are only two product families in the class diagram above, so it is only necessary to define two interfaces in an abstract factory.

The following is an analysis of the advantages and disadvantages of the abstract factory

The biggest benefit of an abstract factory is that it is very convenient to exchange the product family, because the specific factory class is only required to occur at initialization time in an application.

This makes it very easy to change the specific factory of an application, it only needs to change the specific factory to use different product configurations,

For example, I used a factory ConcreteFactory1 to generate product family 1 in my application,

Now the demand has changed, no longer using product family 1, but must use the product family 2 o'clock,

At this point, you only need to initialize the location of the ConcreteFactory1 in the client code,

Change the ConcreteFactory1 to ConcreteFactory2 can be, so that the product family 1 successfully switched to use product family 2 above,

Secondly, the abstract factory lets the concrete creation instance process and the client detach, the client is manipulates the instance through their abstract interface,

The specific class name of the product is also categorized by the implementation of the specific factory and will not appear in the client code.

At this point, simple factories and factory methods are also doing well, that is, relying on abstraction.

At the same time, if the need to expand, for example, to re-add a product family, which is very good,

Only need to add a specific factory, and then add the product family to it,

In short, the abstract factory follows the principle of open-close and dependency inversion very well.

Let's take a look at a demo and see the advantages of the abstract factory from this demo.

Let's start by showing the specific class diagram.

The class diagram above shows that there are two specific factories, one is the manufacture of Linux controls, and the other is the manufacture of Windows controls.

Then, there are two product families, one is a TextBox product family composed of Windowstextbox and Linuxtextbox,

There is also a Button product family composed of Windowsbutton and Linuxbutton.

Let's write the class now.

Let's start with the factory class.

namespace abstractfactory
{
Public abstract class Abstractfactory
{
In an abstract factory, you should include abstract methods for all product creation
Public abstract Button Createbutton();
Public abstract TextBox createtextbox();
}
}

namespace abstractfactory
{
public class Windowsfactory:abstractfactory
{
public override Button Createbutton()
{
return new Windowsbutton ();
}

public override TextBox createtextbox()
{
return new Windowstextbox ();
}
}
}

namespace abstractfactory
{
public class Linuxfactory:abstractfactory
{
public override Button Createbutton()
{
return new Linuxbutton ();
}

public override TextBox createtextbox()
{
return new Linuxtextbox ();
}
}
}

All product classes are given below.

namespace abstractfactory
{
Public abstract class Button
{
Public abstract void Displaybutton();
}
}

Using System;

namespace abstractfactory
{
Class Linuxbutton:button
{
Public override void Displaybutton ()
{
Console.WriteLine ("My type is: {0}",
This . GetType (). ToString ());
}
}
}

Using System;

namespace abstractfactory
{
Class Windowsbutton:button
{
Public override void Displaybutton()
{
Console.WriteLine ("My type is: {0}",
This . GetType (). ToString ());
}
}
}

namespace abstractfactory
{
Public abstract class TextBox
{
Public abstract void Displaytextbox ();
}
}

Using System;

namespace abstractfactory
{
Class Linuxtextbox:textbox
{
Public override void Displaytextbox()
{
Console.WriteLine ("My type is: {0}",
This . GetType (). ToString ());
}
}
}

Using System;

namespace abstractfactory
{
Class Windowstextbox:textbox
{
Public override void Displaytextbox()
{
Console.WriteLine ("My type is: {0}",
This . GetType (). ToString ());
}
}
}

The above is the entire Demo class, the following is a look at the Main function and effect

Using System;
Using abstractfactory;

namespace  abstractfactorytest  

    class Program  
    { 
        static void Main (string[] args)  
        { 
             abstractfactory.abstractfactory factory; 
             button button; 
             textbox TextBox;

Operations under Windows
Factory = new Windowsfactory();
button = Factory. Createbutton ();
TextBox = Factory. CreateTextbox ();
button. Displaybutton ();
Textbox. Displaytextbox ();

Console.WriteLine ();

Operating under Linux
Factory = new Linuxfactory ();
button = Factory. Createbutton ();
TextBox = Factory. CreateTextbox ();
button. Displaybutton ();
Textbox. Displaytextbox ();

Console.ReadLine ();
}
}
}

From the Main function above, if your system is originally Linux-based, you only need to change one line of code

Factory = new Windowsfactory();
To change the system to Windows,

Abstract factories are very useful in this case, for example, if you want to implement a background database to convert from Oracle to SQL Server,

The idea of using abstract factories is the best.

Let's summarize the pros and cons of the abstract factory

First of all, abstract factory words, it can be more convenient to achieve the exchange of a product line,

Like the Demo above, you can easily convert from Linux to Windows,

At the same time, the client code relies on abstractions rather than concrete implementations,

However, the abstract factory also has shortcomings, in fact, this shortcoming is also very obvious, that is to appear too bloated,

Although there are only two product families in the above Demo, the class diagram looks a bit ugly,

If more than one product family, then the total number of classes is several times the increase, so that the entire structure becomes too complex,

The structure of the class will also become larger.

C # Abstract Factory mode

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.