.net design patterns - Abstract Factory

Abstract factory belongs to create design patterns,

Abstract factory capable of producing a series of relatively fixed product family products, to expand a plant is relatively easy, if you want to expand the number of products a product cluster, all of the factory will be expanded relatively trouble

Abstract Factory: factory + constraint
/// create products cluster, more than one object is a whole, indivisible

Abstract factory class

1  ///  <the Summary> 
2      /// a factory responsible for creating some of the products of
 3      /// Product Family
 4      /// single responsibility is to create a complete product cluster
 5      ///  
6      /// after inheriting an abstract class, you must the method of explicit override abstract parent class
 . 7      ///  </ Summary> 
. 8      public  abstract  class FactoryAbstract
 . 9      {
 10          public  abstract Irace CreateRace ();
 . 11          public  abstract IArmy CreateArmy ();
 12 is          public  abstract IHero CreateHero ();
 13 is          public  abstract IResource CreateResource();
14 
15         //public abstract ILuck CreateLuck();
16     }

Each implementation class

. 1  ///  <Summary> 
2      /// a factory is responsible for creating some products
 . 3      ///  </ Summary> 
. 4      public  class HumanFactory: FactoryAbstract
 . 5      {
 . 6          public  the override Irace CreateRace ()
 . 7          {
 . 8              return  new new Human ();
 . 9          }
 10  
. 11          public  the override IArmy CreateArmy ()
 12 is          {
 13 is              return  new new HumanArmy ();
 14          }
 15          public  the override IHero CreateHero()
16         {
17             return new HumanHero();
18         }
19         public override IResource CreateResource()
20         {
21             return new HumanResource();
22         }
23     }
. 1  ///  <Summary> 
2      /// a factory is responsible for creating some products
 . 3      ///  </ Summary> 
. 4      public  class ORCFactory: FactoryAbstract
 . 5      {
 . 6          public  the override Irace CreateRace ()
 . 7          {
 . 8              return  new new the ORC ();
 . 9          }
 10  
. 11          public  the override IArmy CreateArmy ()
 12 is          {
 13 is              return  new new ORCArmy ();
 14          }
 15          public  the override IHero CreateHero()
16         {
17             return new ORCHero();
18         }
19         public override IResource CreateResource()
20         {
21             return new ORCResource();
22         }
23     }

 

Guess you like

Origin www.cnblogs.com/Spinoza/p/11449569.html