-
Notifications
You must be signed in to change notification settings - Fork 49
/
Program.cs
93 lines (90 loc) · 2.77 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
//Modifying the IAnimalFactory class and calling methods as per the design in Client.
namespace FactoryMethodPatternBeautification
{
public interface IAnimal
{
void Speak();
void Action();
}
public class Dog : IAnimal
{
public void Speak()
{
Console.WriteLine("Dog says: Bow-Wow.");
}
public void Action()
{
Console.WriteLine("Dogs prefer barking...\n");
}
}
public class Tiger : IAnimal
{
public void Speak()
{
Console.WriteLine("Tiger says: Halum.");
}
public void Action()
{
Console.WriteLine("Tigers prefer hunting...\n");
}
}
//Modifying the IAnimalFactory class.
public abstract class IAnimalFactory
{
public IAnimal MakeAnimal()
{
Console.WriteLine("AnimalFactory.MakeAnimal()-You cannot ignore parent rules.");
/*
At this point,it doesn't know whether it will get a Dog or a Tiger.
It will be decided by the subclasses i.e.DogFactory or TigerFactory.
But it knows that it will Speak and it will have a preferred way of Action.
*/
IAnimal animal = CreateAnimal();
animal.Speak();
animal.Action();
return animal;
}
//So, the following method is acting like a factory (of creation).
//protected abstract IAnimal CreateAnimal();
public abstract IAnimal CreateAnimal();
}
public class DogFactory : IAnimalFactory
{
//protected override IAnimal CreateAnimal()
public override IAnimal CreateAnimal()
{
//Creating a Dog
return new Dog();
}
}
public class TigerFactory : IAnimalFactory
{
//protected override IAnimal CreateAnimal()
public override IAnimal CreateAnimal()
{
//Creating a Tiger
return new Tiger();
}
}
class Client
{
static void Main(string[] args)
{
Console.WriteLine("***Beautification to Factory Pattern Demo***\n");
// Creating a tiger using the Factory Method
IAnimalFactory tigerFactory = new TigerFactory();
IAnimal aTiger = tigerFactory.MakeAnimal();
//IAnimal aTiger = tigerFactory.CreateAnimal();
//aTiger.Speak();
//aTiger.Action();
// Creating a dog using the Factory Method
IAnimalFactory dogFactory = new DogFactory();
IAnimal aDog = dogFactory.MakeAnimal();
//IAnimal aDog = dogFactory.CreateAnimal();
//aDog.Speak();
//aDog.Action();
Console.ReadKey();
}
}
}