工厂和抽象工厂

  如何将实例化具体类的代码从应用中抽离,或者封装起来,使他们不会干扰到应用的其他部分?

静态工厂方法 

 1     interface IAnimal
 2     {
 3         void Shout();
 4     }
 5 
 6     class Dog:IAnimal
 7     {
 8         #region IAnimal 成员
 9 
10         public void Shout()
11         {
12             Console.WriteLine("汪汪汪...");
13         }
14 
15         #endregion
16     }
17 
18     class Duck:IAnimal
19     {
20         #region IAnimal 成员
21 
22         public void Shout()
23         {
24             Console.WriteLine("嘎嘎嘎...");
25         }
26 
27         #endregion
28     }
 1     class AnimalFactory
 2     {
 3         //静态工厂方法
 4         public static IAnimal Create(string category)
 5         {
 6             IAnimal animal = null;
 7             switch (category)
 8             {
 9                 case "Dog":
10                     animal = new Dog();
11                     break;
12                 case "Duck":
13                     animal = new Duck();
14                     break;
15                 default:
16                     break;
17             }
18             return animal;
19         }
20     }

  把具体实例化的过程封装了起来,这已经开始有了面向对象编程的意识了(虽然这里用的是静态方法)。

工厂方法模式

  接着上面的例子,我们把动物种类进行抽象,不再让他通同switch判断,下面我们创建动物类的工厂

 1     public interface AnimalFactory
 2     {
 3         IAnimal Create();
 4     }
 5 
 6     class DogFactory:AnimalFactory
 7     {
 8         #region AnimalFactory 成员
 9 
10         public IAnimal Create()
11         {
12             return new Dog();
13         }
14 
15         #endregion
16     }
17 
18     class DuckFactory:AnimalFactory
19     {
20         #region AnimalFactory 成员
21 
22         public IAnimal Create()
23         {
24             return new Duck();
25         }
26 
27         #endregion
28     }
 1    public interface IAnimal
 2     {
 3         void Shout();
 4     }
 5 
 6     class Duck:IAnimal
 7     {
 8         #region IAnimal 成员
 9 
10         public void Shout()
11         {
12             Console.WriteLine("嘎嘎嘎...");
13         }
14 
15         #endregion
16     }
17 
18     class Dog : IAnimal
19     {
20         #region IAnimal 成员
21 
22         public void Shout()
23         {
24             Console.WriteLine("汪汪汪...");
25         }
26 
27         #endregion
28     }

  如果我们这时候再添加各种类型的动物因为不用再去大肆修改原来的代码,直接再建立新的动物工厂就行,这不就是实现类设计原则中常说的开放-封闭原则么。

抽象工厂

  这个也是我们比较常见的一个模式,还记得Petshop这个案例么,它里面就有用到,在数据访问层就是用到的抽象工厂,创建了MSSQL和oracle数据库对数据操作的工厂。

通过工厂去创建对应数据类的处理。关于Petshop大家感兴趣的话可以自己去下载下来看,我们再写一个动物的例子好了。假设上面的动物分为家养和野生两种类型

 1    //家养
 2     public interface IKeepAnimal
 3     {
 4         void Shout();
 5     }
 6     //野生
 7     public interface IWildAnimal
 8     {
 9         void Shout();
10     }

  分别实现这两个接口

 1     class KeepDog:IKeepAnimal
 2     {
 3         #region IKeepAnimal 成员
 4 
 5         public void Shout()
 6         {
 7             Console.WriteLine("家养狗,汪!汪!汪!");
 8         }
 9 
10         #endregion
11     }
12 
13     class WildDog:IWildAnimal
14     {
15         #region IWildAnimal 成员
16 
17         public void Shout()
18         {
19             Console.WriteLine("野狗,汪~~~");
20         }
21 
22         #endregion
23     }

  创建抽象工厂;依赖抽象,而不是依赖具体。

 1     interface AnimalFactory
 2     {
 3         IKeepAnimal KeepFactory();
 4         IWildAnimal WildFactory();
 5     }
 6 
 7     class DogFacory:AnimalFactory
 8     {
 9         #region AnimalFactory 成员
10 
11         public IKeepAnimal KeepFactory()
12         {
13             return new KeepDog();
14         }
15 
16         public IWildAnimal WildFactory()
17         {
18             return new WildDog();
19         }
20 
21         #endregion
22     }

  鸭子的实现方式也和狗的一样,大家感兴趣的话可以试着自己去写下。

小结

  工厂方法模式通过让子类决定创建的对象是什么,来达到将对象创建的过程封装的目的。

  抽象工厂模式提供一个接口,用于创建相关或依赖的对象家族,而不需要明确指定哪个类。

posted @ 2015-05-25 15:19  高效养猪倌  阅读(662)  评论(2编辑  收藏  举报