Simple factory mode: multiple types are derived from abstract classes. Instances of objects are generated through static methods in the factory class. /* -- ===-------------------------------------------- = ---
Simple factory mode (build Mode)
-- ===-------------------------------------------- = --- */
Using System;
NamespaceXumh
{
PublicAbstract ClassFruit
{
Public Abstract StringGetname ();
}
Public Class Apple: Fruit
{
Public Override String Getname ()
{
Return " Apple " ;
}
}
Public Class Pear: Fruit
{
Public Override String Getname ()
{
Return " Pear " ;
}
}
Public Class Fruitfactory
{
Public Static Apple createapple ()
{
Return New Apple ();
}
Public Static Pear createpear ()
{
Return New Pear ();
}
}
Public Class Runmyapp
{
Static Void Main ()
{
Fruit [] fruits = New Fruit [ 2 ];
Fruits [ 0 ] = Fruitfactory. createapple ();
Fruits [ 1 ] = Fruitfactory. createpear ();
Foreach (Fruit In Fruits)
Console. writeline (fruit. getname ());
}
}
}
Abstract Factory mode: /*
★New problem:
Implementation dependencies cannot cope with changes to the "Specific instantiation type.
★Solution:
Encapsulation changes-where to change, where to encapsulate, no changes, no need to encapsulate.
★The origin of the factory Model
1). The change point is "Object creation", so it encapsulates "Object creation"
2). Interface-oriented programming-interfaces from the beginning, rather than dependency implementation.
Class roadfactory
{
Public static road createroad ()
{
Return New Road ();
}
}
// Create a road object
Road = roadfactory. createroad ();
★Key points:
1). If the requirement for "Multi-series object building" is not changed, abstract factory pattern is not necessary and can be used in a simple static factory mode.
2). "series objects" refer to the dependency between these objects.
3) Abstract Factory model to cope with "new series" demand changes, the disadvantage is that it is difficult to cope with "new object" demand changes.
4) The Abstract Factory mode often works with the factory method mode to cope with changes in the "Object creation" requirement.
-- ===-------------------------------------------- = ---
★Experience:
All object classes use abstract classes; factory classes use abstract methods to cope with the creation of specific classes in each series of objects;
In the customer section, factory classes of different styles can be used as parameters to generate objects of different styles.
* */
Using System;
Namespace Abstractfactory
{
// Road
Public Abstract Class Road
{
}
// House
Public Abstract Class Building
{
}
// Authentic
Public Abstract Class Tunnel
{
}
// Jungle
Public Abstract Class Jungle
{
}
/// <Summary>
/// Factory class, abstract methods can create multiple objects
/// </Summary>
Public Abstract Class Buildfactory
{
Public Abstract Road createroad ();
Public Abstract Building createbuilding ();
Public Abstract Tunnel createtunnel ();
Public Abstract Jungle createjungle ();
}
// -- ===-------------------------------------------- = ---
// Modern Style
// Road
Public Class Modernroad: Road
{
}
// House
Public Class Modernbuilding: Building
{
}
// Authentic
Public Class Moderntunnel: Tunnel
{
}
// Jungle
Public Class Modernjungle: Jungle
{
}
// -- ===-------------------------------------------- = ---
Class Modernbuildfactory: buildfactory
{
Public Override Road createroad (){
Return New Modernroad ();
}
Public Override Building createbuilding (){
Return New Modernbuilding ();
}
Public Override Tunnel createtunnel (){
Return New Moderntunnel ();
}
Public Override Jungle createjungle (){
Return New Modernjungle ();
}
}
/// <Summary>
/// ClientProgramDoes not depend on specific classes and does not need to be changed.
/// </Summary>
Class Gamemanager
{
Road;
Building building;
Tunnel tunnel;
Jungle jungle;
Buildfactory;
Public Gamemanager (buildfactory)
{
This . Buildfactory = Buildfactory;
}
Public Void Buildgamefacilities ()
{
Road = Buildfactory. createroad ();
Building = Buildfactory. createbuilding ();
Tunnel = Buildfactory. createtunnel ();
Jungle = Buildfactory. createjungle ();
}
Public Void Run ()
{
// Road. AAA ();
// Building. BBB (road );
// Tunnel. CCC ();
// Jungle. DDD (tunnel );
}
}
Class Program
{
Static Void Main ( String [] ARGs)
{
Gamemanager =
New Gamemanager ( New Modernbuildfactory ());
Gamemanager. buildgamefacilities ();
Gamemanager. Run ();
}
}
}