Abstract Factory mode is a set of factory method modes. The factory method gets a product, while the abstract factory model gets a series of products.
Let's take a general understanding of UML first:
I. Abstract Factory
Abstract Factory is a gamefactory that produces two types of products (hero, building. Of course, this article only provides two types ).
/// <Summary>
/// Abstract Factory
/// </Summary>
Public Abstract Class Gamefactory
{
Public Abstract Hero createhero ();
Public Abstract Environment createenvironment ();
}
The abstract factory is actually a collection of factory methods. In short, there are n methods. In this example, two methods are used to create a hero and a building:
Public Abstract Hero createhero ();
Public Abstract Environment createenvironment ();
Ii. Actual Factory ( Warfactory, Dotafactory )
Sub-classes are responsible for the implementation of specific methods. We now have two kinds of factories: one is a war battle of pure lineage, and the other is the RPG map Dota, so we have two actual factories:
/// <Summary>
/// Warcraft Factory
/// </Summary>
Public Class Warfactory: gamefactory
{
Public Override Hero createhero ()
{
Return New Blademaster ();
}
Public OverrideEnvironment createenvironment ()
{
Return NewPubs ();
}
}
/// <Summary>
/// Dota Factory
/// </Summary>
Public Class Dotafactory: gamefactory
{
Public Override Hero createhero ()
{
Return New Naix ();
}
Public Override Environment createenvironment ()
{
Return New Altar ();
}
}
We can see that the products in the two factories are not the same.
On the War battle map, heroes come out of the altar, while in Dota, heroes are bought from the tavern. Therefore, the building produced by warfactory is an "altar ", the building produced by dotafactory is a "tavern ".
In the war battle map, the zombie is only a common soldier, while in Dota, it is converted into abnormal heroes. Therefore, the hero produced by warfactory is the swordsman, and the hero produced by dotafactory is the zombie ".
In short, the difference between the factory method and the abstract factory is that the factory method creates a single product, while the abstract factory creates a series of products, such as heroes and buildings in this example, this series of "Products" builds our "Warcraft" world.
Test Code :
Landpyform. Form. outputresult ("war ");
Dotapatternlibrary. abstractfactory. gamefactory = new dotapatternlibrary. abstractfactory. warfactory ();
Dotapatternlibrary. abstractfactory. Hero hero = gamefactory. createhero ();
Dotapatternlibrary. abstractfactory. environment = gamefactory. createenvironment ();
Landpyform. Form. outputresult (hero. Name );
Landpyform. Form. outputresult (environment. Name );
Landpyform. Form. outputresult ("Dota ");
Gamefactory = new dotapatternlibrary. abstractfactory. dotafactory ();
Hero = gamefactory. createhero ();
Environment = gamefactory. createenvironment ();
Landpyform. Form. outputresult (hero. Name );
Landpyform. Form. outputresult (environment. Name );
The complete code is as follows: Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
UsingDotacommon;
UsingDotapatternlibrary. Attribute;
Namespace Dotapatternlibrary. abstractfactory
{
/// <Summary>
/// Abstract Factory
/// </Summary>
Public Abstract Class Gamefactory
{
Public Abstract Hero createhero ();
Public Abstract Environment createenvironment ();
}
/// <Summary>
/// Warcraft Factory
/// </Summary>
Public Class Warfactory: gamefactory
{
Public Override Hero createhero ()
{
Return New Blademaster ();
}
Public OverrideEnvironment createenvironment ()
{
Return NewPubs ();
}
}
/// <Summary>
/// Dota Factory
/// </Summary>
Public Class Dotafactory: gamefactory
{
Public Override Hero createhero ()
{
Return New Naix ();
}
Public Override Environment createenvironment ()
{
Return New Altar ();
}
}
/// <Summary>
/// Hero
/// </Summary>
Public Abstract Class Hero
{
Protected String _ Name;
Public String Name
{
Get { Return _ Name ;}
}
}
/// <Summary>
/// Jiansheng
/// </Summary>
Internal Class Blademaster: Hero
{
Public Blademaster ()
{
_ Name = " Blademaster " ;
}
}
/// <Summary>
/// Zombie
/// </Summary>
Internal Class Naix: Hero
{
Public Naix ()
{
_ Name = " Naix " ;
}
}
/// <Summary>
/// Environment (Building)
/// </Summary>
Public Abstract Class Environment
{
Protected String _ Name;
Public String Name
{
Get { Return _ Name ;}
}
}
/// <Summary>
/// Tavern
/// </Summary>
Internal Class Pubs: Environment
{
Public Pubs ()
{
_ Name = " Pubs " ;
}
}
/// <Summary>
/// Altar
/// </Summary>
Internal Class Altar: Environment
{
Public Altar ()
{
_ Name = " Altar " ;
}
}
}