Simple facotry, acfactory, and abstract factory)

Source: Internet
Author: User

1. Simple Factory)

Gof does not list simple factories as one of the design patterns. But to learn about the two factory models, let's take a look at the simple factory.

Purpose:It is up to a class to decide which product class to instantiate.
Implementation points:Many people may unconsciously use simple factories. A simple factory is to put the method that decides which class to instantiate into a separate class (the so-called factory class.
UML:

Code:

    Abstract     Class  Product
{
Public Abstract Void Showproduct ();
}
Class Producta: Product
{
Public Override Void Showproduct ()
{
Console. writeline ( " This is producta " );
}
}
Class Productb: Product
{
Public Override Void Showproduct ()
{
Console. writeline ( " This is productb " );
}
}
// Factory
Class Factory
{
Public Product getproduct ( Int Type)
{
If (Type = 0 )
{
Return New Producta ();
}
Else
{
Return New Productb ();
}
}
}
// --------------- Run -----------------
Class Program
{
Static Void Main ( String [] ARGs)
{
Factory F = New Factory ();
Product p1 = F. getproduct ( 0 );
Product p2 = F. getproduct ( 1 );
P1.showproduct ();
P2.showproduct ();
Console. Readline ();
}
}

 

Ii. Factory method)

Purpose:The factory method is the "re-abstraction" of a simple factory. Instead of directly deciding which product class to instantiate in a class, the factory method delays this decision to its subclass. (For how to abstract the parent class, see the previousArticle"Parent class references to subclass objects ").
Implementation points:The abstract factory class determines which product class to instantiate by its subclass.
UML:

Code:

  Abstract     Class Product
{
Public Abstract Void showproduct ();
}
Class producta: Product
{
Public Override Void showproduct ()
{
Console. writeline ( " This is producta ");
}
}
Class productb: Product
{
Public Override Void showproduct ()
{
Console. writeline ( " This is productb ");
}
}
// Factory
Abstract Class Factory
{
Public Abstract product getproduct ();
}
Class facotrya: Factory
{
Public Override product getproduct ()
{
Return New producta ();
}
}
Class facotryb: Factory
{
Public Override product getproduct ()
{
Return New productb ();
}
}
// -------------------- Run ------------------------
Class Program
{
Static Void Main ( String [] ARGs)
{
Factory [] F = New Factory [ 2];
F [ 0 ] = New facotrya ();
F [ 1 ] = New facotryb ();
Foreach (Factory myfactory In F)
{
Product P = Myfactory. getproduct ();
P. showproduct ();
}

Console. Readline ();
}
}

 

 
 

3. Abstract Factory)

Purpose:The customer class needs different product combinations to meet their own needs. The customer only cares about the effect of the "product portfolio", rather than the specific product details.
Implementation points:
The subclass of the abstract factory class (home class in this example) determines which specific product groups (choose bed, sofa, door, desk, etc.) to select. When using this factory, the client class first declares an abstract factory class. For example, the constructor of the client class in this example needs a parameter of the home type, when the caller instantiates the client class (Client1 and Client2 in the main function), the parameter is directly passed into the specific factory class (simplehome or advancedhome in this example, in this way, the caller does not need to know the specific product categories (such as bed, sofa, door, desk in this example), and can select the factory type (simplehome or advancedhome in this example) to obtain a series of product types (bed, sofa, door, desk, etc ).
UML:

 
 

Code:

  Abstract    Class  Products
{
Public Abstract Void Showproduct ();
}
Class Productbed: Products
{
Public Override Void Showproduct ()
{
Console. writeline ( " Exist a bed " );
}
}
Class Productdesk: Products
{
Public Override Void Showproduct ()
{
Console. writeline ( " Exist a desk " );
}
}
Class Productdoor: Products
{
Public Override Void Showproduct ()
{
Console. writeline ( " Exist a door " );
}
}
Class Productsofa: Products
{
Public Override Void Showproduct ()
{
Console. writeline ( " Exist a sofa " );
}
}

// Abstractfactory, here is an abstract "home"
Abstract Class Home
{
Protected Products mybed, mydesk, mydoor, mysofa;
Public Abstract Void Showmyhouse ();
}
// Simple Home: only the door and bed are needed
Class Simplehome: Home
{
Public Simplehome ()
{
Mybed = New Productbed ();
Mydoor = New Productdoor ();
}
Public Override Void Showmyhouse ()
{
Console. writeline ( " This is a simple home: " );
Mybed. showproduct ();
Mydoor. showproduct ();
}
}
// Advanced home: SOFA, tables, and other furniture
Class Advancedhome: Home
{
Public Advancedhome ()
{
Mybed = New Productbed ();
Mydoor = New Productdoor ();
Mysofa = New Productsofa ();
Mydesk = New Productdesk ();
}
Public Override Void Showmyhouse ()
{
Console. writeline ( " This is a advanced home: " );
Mybed. showproduct ();
Mydoor. showproduct ();
Mysofa. showproduct ();
Mydesk. showproduct ();
}
}
// Customer class that calls Abstract Factory
Class Client
{
Home myhome;
Public Client (home newhome)
{
This . Myhome = Newhome;
}
Public Void Showhome ()
{
Myhome. showmyhouse ();
}
}
// -------------------- Run -------------------
Class Program
{
Static Void Main ( String [] ARGs)
{
// The customer does not need to select specific furniture, but only needs to select a specific house type. The furniture is automatically configured.
Client Client1 = New Client ( New Simplehome ());
Client1.showhome ();
Client Client2 = New Client ( New Advancedhome ());
Client2.showhome ();
Console. Readline ();
}

}
 
 
There are always people looking for differences between Abstract Factory methods and factory methods. They have different purposes, so it is worse:
The factory method is actually an idea. I want to use one thing, but now I cannot determine what to do, leaving an interface to be implemented by other objects. In this way, the variable and uncertain parts can be separated, that is, decoupling.
Abstract factories are encapsulation. The best example is to buy office peripherals. You directly said to an agent: Give me a combination of office peripherals, so he gave you a series of products: HP printers, Canon copiers, HP projectors, ricoh shredder... For convenience, you are concerned with a series of product groups, and agents play the role of Abstract Factory, giving you a series of office products. (The previous computer examples are not very relevant, because the builder mode is later introduced)

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.