Abstract Factory of design patterns

Source: Internet
Author: User

Previous ArticleArticle, Introduced the factory method, and discussed the shortcomings of the factory method below:

And we know that in the factory method, each creator only creates a single instance. What if we want to create n Different class instances at a time?
And, what if we want to implement this method? :
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace factory Method
{
Public interface icreator <t> where T: idomain
{
Ispeciality <t> createrspeciality ();
// Add the creation method of other classes here
}
}

The abstract factory solves this problem well.

If you are on a farm, you can plant fruits and vegetables. The types of vegetables and fruits vary by region. For example, the fruits planted by farmers in the South are litchi, the vegetables are bitter gourd, And the fruits planted by farmers in the North are red dates, vegetables are Chinese cabbage. A farmer on a farm cannot ask his workers to plant the same thing. UseProgramAs follows:

Farmer interface:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace Abstract Factory
{
Public interface farmer Interface
{
Vegetable interface planting vegetables ();

Fruit planting through fruit interfaces ();

}
}

 

Fruit interface:

Using system;
Using system. Collections. Generic;
Using system. text;

namespace Abstract Factory
{< br> ///


// fruit interface
///
Public interface fruit interface
{< br> ///
// fruit flavor
///
///
string flavor ();
///
// fruit growth cycle
///
///
int fruit growth cycle ();

/// <Summary>
/// Fruit growth area
/// </Summary>
/// <Returns> </returns>
String growth region ();

}
}

Vegetable interface:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace Abstract Factory
{
/// <Summary>
/// Vegetable Interface
/// </Summary>
Public interface vegetable Interface
{
/// <Summary>
/// Vegetable growth area
/// </Summary>
/// <Returns> </returns>
String growth region ();

/// <Summary>
/// Whether a seed has only one fruit
/// </Summary>
/// <Returns> </returns>
Whether a bool seed has only one fruit ();
}
}

Various fruits:

Using system;
Using system. Collections. Generic;
Using system. text;

namespace Abstract Factory
{< br> ///


// fruit produced in southern litchi Category
///
public class litchi: fruit interface
{< br> Public String flavor ()
{< br> return "litchi is very sweet";
}

Public int fruit growth period ()
{
Return 1;
}
Public string growth region ()
{
Return "Southern ";
}
}
}

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace Abstract Factory
{
Public class jujube: fruit Interface
{
Public String taste ()
{
Return "red dates are sweet ";
}

Public int fruit growth period ()
{
Return 1;
}
Public string growth region ()
{
Return "North ";
}
}
}

Various vegetables:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace Abstract Factory
{
Public class Chinese cabbage: Vegetable Interface
{
Public string growth region ()
{
Return "North ";
}

Does a public bool seed have only one fruit ()
{
Return true;
}
}
}

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace Abstract Factory
{
Public class Bitter Gourd: Vegetable Interface
{
Public string growth region ()
{
Return "Southern ";
}

Does a public bool seed have only one fruit ()
{
Return false;
}
}
}

Farmers:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace Abstract Factory
{
Public class southern farmers: farmer Interface
{
Public vegetable interface planting vegetables ()
{
Return New Bitter Gourd ();
}

Public fruit interface planting fruit ()
{
Return new lychee ();
}
}
}

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace Abstract Factory
{
Public class Northern farmers: farmer Interface
{
Public vegetable interface planting vegetables ()
{
Return New Chinese cabbage ();
}

Public fruit interface planting fruit ()
{
Return new jujube ();
}
}
}

When using:

Private void button#click (Object sender, eventargs E)
{
Farmer interface: A farmer = new southern farmer ();
Fruit interface fruit = a farmer. planting fruit ();
Vegetable interface vegetables = a farmer. Planting Vegetables ();
}

In this way, farmers in the South can plant fruits in the south and vegetables in the south at the same time, which solves the problem that only one farmer can plant one thing, that is, to solve the problem of the factory method.

 

Abstract Factory and builder buileder are different:

First, the abstract factory returns a complete product object. The client can assemble more complex product Objects Based on these product objects.

The builder does not return a complete product object. The client needs to assemble all parts of the product.

If the two modes are used in combination, the situation can be as follows:

For example, in a game, for example, a swordsman developed by xishanju, a map requires building houses, roads, road signs, billboards, etc,

A map requires the creation of these things, and these houses and things have different styles. The Builder + abstract factory can meet the needs of the map creation, I don't know how the game is played. I am confused)

The above program is represented in Chinese. Haha, first, the test vs can use Chinese, and second, it looks easier.

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.