Sunday, September 20, 2015

ABSTRACT FACTORY

ABSTRACT FACTORY

 

 

Abstract Factory

:: In Review

An Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.

1.                   

2.          

3.   

4.  using System;

5.   

6.  namespace DoFactory.GangOfFour.Abstract.Structural

7.  {

8.    /// <summary>

9.    /// MainApp startup class for Structural

10.  /// Abstract Factory Design Pattern.

11.  /// </summary>

12.  class MainApp

13.  {

14.    /// <summary>

15.    /// Entry point into console application.

16.    /// </summary>

17.    public static void Main()

18.    {

19.      // Abstract factory #1

20.      AbstractFactory factory1 = new ConcreteFactory1();

21.      Client client1 = new Client(factory1);

22.      client1.Run();

23. 

24.      // Abstract factory #2

25.      AbstractFactory factory2 = new ConcreteFactory2();

26.      Client client2 = new Client(factory2);

27.      client2.Run();

28. 

29.      // Wait for user input

30.      Console.ReadKey();

31.    }

32.  }

33. 

34.  /// <summary>

35.  /// The 'AbstractFactory' abstract class

36.  /// </summary>

37.  abstract class AbstractFactory

38.  {

39.    public abstract AbstractProductA CreateProductA();

40.    public abstract AbstractProductB CreateProductB();

41.  }

42. 

43. 

44.  /// <summary>

45.  /// The 'ConcreteFactory1' class

46.  /// </summary>

47.  class ConcreteFactory1 : AbstractFactory

48.  {

49.    public override AbstractProductA CreateProductA()

50.    {

51.      return new ProductA1();

52.    }

53.    public override AbstractProductB CreateProductB()

54.    {

55.      return new ProductB1();

56.    }

57.  }

58. 

59.  /// <summary>

60.  /// The 'ConcreteFactory2' class

61.  /// </summary>

62.  class ConcreteFactory2 : AbstractFactory

63.  {

64.    public override AbstractProductA CreateProductA()

65.    {

66.      return new ProductA2();

67.    }

68.    public override AbstractProductB CreateProductB()

69.    {

70.      return new ProductB2();

71.    }

72.  }

73. 

74.  /// <summary>

75.  /// The 'AbstractProductA' abstract class

76.  /// </summary>

77.  abstract class AbstractProductA

78.  {

79.  }

80. 

81.  /// <summary>

82.  /// The 'AbstractProductB' abstract class

83.  /// </summary>

84.  abstract class AbstractProductB

85.  {

86.    public abstract void Interact(AbstractProductA a);

87.  }

88. 

89. 

90.  /// <summary>

91.  /// The 'ProductA1' class

92.  /// </summary>

93.  class ProductA1 : AbstractProductA

94.  {

95.  }

96. 

97.  /// <summary>

98.  /// The 'ProductB1' class

99.  /// </summary>

100.   class ProductB1 : AbstractProductB

101.   {

102.     public override void Interact(AbstractProductA a)

103.     {

104.       Console.WriteLine(this.GetType().Name +

105.         " interacts with " + a.GetType().Name);

106.     }

107.   }

108.  

109.   /// <summary>

110.   /// The 'ProductA2' class

111.   /// </summary>

112.   class ProductA2 : AbstractProductA

113.   {

114.   }

115.  

116.   /// <summary>

117.   /// The 'ProductB2' class

118.   /// </summary>

119.   class ProductB2 : AbstractProductB

120.   {

121.     public override void Interact(AbstractProductA a)

122.     {

123.       Console.WriteLine(this.GetType().Name +

124.         " interacts with " + a.GetType().Name);

125.     }

126.   }

127.  

128.   /// <summary>

129.   /// The 'Client' class. Interaction environment for the products.

130.   /// </summary>

131.   class Client

132.   {

133.     private AbstractProductA _abstractProductA;

134.     private AbstractProductB _abstractProductB;

135.  

136.     // Constructor

137.     public Client(AbstractFactory factory)

138.     {

139.       _abstractProductB = factory.CreateProductB();

140.       _abstractProductA = factory.CreateProductA();

141.     }

142.  

143.     public void Run()

144.     {

145.       _abstractProductB.Interact(_abstractProductA);

146.     }

147.   }

148. }

149.  

150.  

151.           

152.             

=

=

=

=

=

 

No comments:

Post a Comment