close
This tutorial is about factory pattern, which is my thoghts after watching the tutorial created by Christopher Okhravi.
Simple Factory
Factory Method
Abstract Factory
IAnimal portion:
1: public interface IAnimal
2: {
3: void makeSound();
4: }
Dog portion:
1: public class Dog : IAnimal
2: {
3: public void makeSound()
4: {
5: Console.WriteLine("Dog is barking!");
6: }
7: }
Cat portion:
1: public class Cat : IAnimal
2: {
3: public void makeSound()
4: {
5: Console.WriteLine("Cat can meow!");
6: }
7: }
Duck portion:
1: public class Duck : IAnimal
2: {
3: public void makeSound()
4: {
5: Console.WriteLine("Duck just quack!");
6: }
7: }
SimpleAnimalFactory portion:
1: public static class SimpleAnimalFactory
2: {
3: private static Dictionary<string, IAnimal> animals = new Dictionary<string, IAnimal>();
4: public static IAnimal CreateAnimal(string Name)
5: {
6: if (animals.Count == 0)
7: {
8: animals.Add("Cat", new Cat());
9: animals.Add("Dog", new Dog());
10: animals.Add("Duck", new Duck());
11: }
12: return animals[Name];
13: }
14: }
Client portion:
1: static void Main(string[] args)
2: {
3: SimpleAnimalFactory.CreateAnimal("Cat").makeSound();
4: SimpleAnimalFactory.CreateAnimal("Dog").makeSound();
5: SimpleAnimalFactory.CreateAnimal("Duck").makeSound();
6: Console.ReadKey();
7: }
Testing Result:
Factory Method
Update SimpleAnimalFactory portion:
1: public static class SimpleAnimalFactory
2: {
3: private static Dictionary<string, IAnimal> animals = new Dictionary<string, IAnimal>();
4: private static IList<string> names = new List<string>();
5: public static IAnimal CreateAnimal(string Name)
6: {
7: if (animals.Count == 0)
8: {
9: animals.Add("Cat", new Cat());
10: animals.Add("Dog", new Dog());
11: animals.Add("Duck", new Duck());
12: }
13: return animals[Name];
14: }
15: public static IAnimal CreateAnimal(int index)
16: {
17: if (names.Count == 0)
18: {
19: names.Add("Cat");
20: names.Add("Dog");
21: names.Add("Duck");
22: }
23: string name = names[index % animals.Count];
24: return CreateAnimal(name);
25: }
26: }
RandomFactory portion:
1: public class RandomFactory : IAnimalFactory
2: {
3: public IAnimal factoryMethod()
4: {
5: Random rnd = new Random(Guid.NewGuid().GetHashCode());
6: int index = rnd.Next(0, 1000);
7: return SimpleAnimalFactory.CreateAnimal(index);
8: }
9: }
BalanceFactory portion:
1: public class BalanceFactory : IAnimalFactory
2: {
3: int index = 0;
4: public IAnimal factoryMethod()
5: {
6: return SimpleAnimalFactory.CreateAnimal(index++);
7: }
8: }
Client portion:
1: static void Main(string[] args)
2: {
3: SimpleAnimalFactory.CreateAnimal("Cat").makeSound();
4: SimpleAnimalFactory.CreateAnimal("Dog").makeSound();
5: SimpleAnimalFactory.CreateAnimal("Duck").makeSound();
6: Console.WriteLine("-------------------------");
7: Console.ReadKey();
8: //-----------------------------------------
9: Console.WriteLine("RandomFactory Example:");
10: RandomFactory randFactory = new RandomFactory();
11: for(int i=0; i<9;i++)
12: {
13: Console.Write("#{0}:", i);
14: randFactory.factoryMethod().makeSound();
15: }
16: Console.WriteLine("-------------------------");
17: Console.ReadKey();
18: //-----------------------------------------
19: Console.WriteLine("BalanceFactory Example:");
20: BalanceFactory balanceFactory = new BalanceFactory();
21: for (int i = 0; i < 9; i++)
22: {
23: Console.Write("#{0}:", i);
24: balanceFactory.factoryMethod().makeSound();
25: }
26:
27: Console.ReadKey();
28: }
Testing Result:
Abstract Factory
References:
1. [Design Pattern] 簡單工廠模式 (Simple Factory Pattern) 不怕飲料有幾種
2. http://www.dofactory.com/net/abstract-factory-design-pattern
全站熱搜
留言列表