Beruflich Dokumente
Kultur Dokumente
Design Patterns
There are three most popular MV-* design patterns: MVC, MVP and MVVM. These are
widely used by the various technologies. In this article, I will provide my opinion on
these three.
MVC Pattern
MVC design pattern splits an application into three main aspects: Model, View and
Controller
1. Model
The Model represents a set of classes that describe the business logic i.e. business
model as well as data access operations i.e. data model. It also defines business rules
for data means how the data can be changed and manipulated.
2. View
The View represents the UI components like CSS, jQuery, html etc. It is only responsible
for displaying the data that is received from the controller as the result. This also
transforms the model(s) into UI.
3. Controller
The Controller is responsible to process incoming requests. It receives input from users
via the View, then process the user's data with the help of Model and passing the
results back to the View. Typically, it acts as the coordinator between the View and the
Model.
Today, this pattern is used by many popular framework like as Ruby on Rails, Spring
Framework, Apple iOS Development and ASP.NET MVC.
MVP pattern
This pattern is similar to MVC pattern in which controller has been replaced by the
presenter. This design pattern splits an application into three main aspects: Model, View
and Presenter.
1. Model
The Model represents a set of classes that describes the business logic and data. It also
defines business rules for data means how the data can be changed and manipulated.
2. View
The View represents the UI components like CSS, jQuery, html etc. It is only responsible
for displaying the data that is received from the presenter as the result. This also
transforms the model(s) into UI.
3. Presenter
The Presenter is responsible for handling all UI events on behalf of the view. This receive
input from users via the View, then process the user's data with the help of Model and
passing the results back to the View. Unlike view and controller, view and presenter are
completely decoupled from each others and communicate to each others by an
interface.
Also, presenter does not manage the incoming request traffic as controller.
This pattern is commonly used with ASP.NET Web Forms applications which require to
create automated unit tests for their code-behind pages. This is also used with windows
forms.
2. There is one-to-one relationship between View and Presenter means one View is
mapped to only one Presenter.
3. View has a reference to Presenter but View has not reference to Model.
MVVM stands for Model-View-View Model. This pattern supports two-way data binding
between view and View model. This enables automatic propagation of changes, within
the state of view model to the View. Typically, the view model uses the observer pattern
to notify changes in the view model to model.
1. Model
The Model represents a set of classes that describes the business logic and data. It also
defines business rules for data means how the data can be changed and manipulated.
2. View
The View represents the UI components like CSS, jQuery, html etc. It is only responsible
for displaying the data that is received from the controller as the result. This also
transforms the model(s) into UI..
3. View Model
The View Model is responsible for exposing methods, commands, and other properties
that helps to maintain the state of the view, manipulate the model as the result of
actions on the view, and trigger events in the view itself.
This pattern is commonly used by the WPF, Silverlight, Caliburn, nRoute etc.
Key Points about MVVM Pattern:
1. User interacts with the View.
3. View has a reference to ViewModel but View Model has no information about the
View.
Singleton pattern is one of the simplest design patterns. This pattern ensures that a
class has only one instance and provides a global point of access to it.
The UML class diagram for the implementation of the Singleton design pattern is given
below:
The classes, and objects in the above UML class diagram are as follows:
1. Singleton
This is a class which is responsible for creating and maintaining its own unique instance.
C# - Implementation Code
1. //eager initialization of singleton
3. {
5. private Singleton() { }
6.
8. {
9. get
10. {
12. }
13. }
14.}
15.
18.{
21.
22. public static Singleton GetInstance
23. {
24. get
25. {
28.
30. }
31. }
32.}
33.
36.{
40.
42. {
43. get
44. {
46. {
47. if (instance == null)
49.
51. }
52. }
53. }
54.}
Who is what?
The classes and objects in the above class diagram can be identified as follows:
C# - Sample Code
1. /// <summary>
5. {
11. {
14.
16. IP = "192.168.1.23";
17. }
20.
22. {
23. get
24. {
30. {
33.
35. }
36. }
37. }
38.
40. {
42. }
43.
44.}
45.
46./// <summary>
48./// </summary>
49.///
50.class Program
51.{
54. Singleton.Instance.Show();
55. Singleton.Instance.Show();
56.
57. Console.ReadKey();
58. }
59.}
Prototype pattern is used to create a duplicate object or clone of the current object to
enhance performance. This pattern is used when creation of object is costly or complex.
The UML class diagram for the implementation of the Prototype design pattern is given
below:
The classes, interfaces and objects in the above UML class diagram are as follows:
1. Prototype
This is an interface which is used for the types of object that can be cloned itself.
2. ConcretePrototype
This is a class which implements the Prototype interface for cloning itself.
C# - Implementation Code
1. public interface Prototype
2. {
3. Prototype Clone();
4. }
5.
6. public class ConcretePrototypeA : Prototype
7. {
9. {
12.
15. }
16.}
17.
19.{
21. {
24.
27. }
28.}
Prototype Pattern - Example
Who is what?
The classes, interfaces and objects in the above class diagram can be identified as
follows:
C# - Sample Code
1. /// <summary>
3. /// </summary>
5. {
6. IEmployee Clone();
7. string GetDetails();
8. }
9.
10./// <summary>
12./// </summary>
14.{
19.
21. {
24.
27. }
28.
30. {
31. return string.Format("{0} - {1} - {2}", Name, Role, PreferredLanguage);
32. }
33.}
34.
35./// <summary>
37./// </summary>
39.{
43.
45. {
48.
51. }
52.
54. {
57.}
58.
59./// <summary>
61./// </summary>
62.
63.class Program
64.{
66. {
71.
74.
75. Console.WriteLine(dev.GetDetails());
76. Console.WriteLine(devCopy.GetDetails());
77.
82.
86.
87. Console.WriteLine(typist.GetDetails());
88. Console.WriteLine(typistCopy.GetDetails());
89.
90. Console.ReadKey();
91.
92. }
93.}
Builder pattern builds a complex object by using a step by step approach. Builder
interface defines the steps to build the final object. This builder is independent from the
objects creation process. A class that is known as Director, controls the object creation
process.
Moreover, builder pattern describes a way to separate an object from its construction.
The same construction method can create different representation of the object.
The UML class diagram for the implementation of the builder design pattern is given
below:
The classes, interfaces and objects in the above UML class diagram are as follows:
1. Builder
This is an interface which is used to define all the steps to create a product
2. ConcreteBuilder
This is a class which implements the Builder interface to create complex product.
3. Product
This is a class which defines the parts of the complex object which are to be generated
by the builder pattern.
4. Director
This is a class which is used to construct an object using the Builder interface.
C# - Implementation Code
1. public interface IBuilder
2. {
3. void BuildPart1();
4. void BuildPart2();
5. void BuildPart3();
6. Product GetProduct();
7. }
8.
10.{
12.
14. {
16. }
17.
18. public void BuildPart2()
19. {
21. }
22.
24. {
26. }
27.
29. {
31. }
32.}
33.
35.{
39.}
40.
42.{
43. public void Construct(IBuilder IBuilder)
44. {
45. IBuilder.BuildPart1();
46. IBuilder.BuildPart2();
47. IBuilder.BuildPart3();
48. }
49.}
Who is what?
The classes, interfaces and objects in the above class diagram can be identified as
follows:
1. IVehicleBuilder - Builder interface
3. Vehicle- Product
C# - Sample Code
1. /// <summary>
3. /// </summary>
5. {
6. void SetModel();
7. void SetEngine();
8. void SetTransmission();
9. void SetBody();
11.
13.}
14.
15./// <summary>
17./// </summary>
19.{
20. Vehicle objVehicle = new Vehicle();
22. {
24. }
25.
27. {
29. }
30.
32. {
34. }
35.
37. {
39. }
40.
42. {
46.
48. {
50. }
51.}
52.
53./// <summary>
55./// </summary>
57.{
60. {
62. }
63.
65. {
67. }
68.
72. }
73.
75. {
77. }
78.
80. {
83. objVehicle.Accessories.Add("Helmet");
84. }
85.
87. {
89. }
90.}
91.
92./// <summary>
94./// </summary>
95.public class Vehicle
96.{
102.
104. {
106. }
107.
109. {
114. Console.WriteLine("Accessories:");
116. {
118. }
119. }
120. }
121.
126. {
128.
130. {
132. }
133.
135. {
136. objBuilder.SetModel();
137. objBuilder.SetEngine();
138. objBuilder.SetBody();
139. objBuilder.SetTransmission();
140. objBuilder.SetAccessories();
141. }
142.
144. {
145. return objBuilder.GetVehicle();
146. }
147. }
148.
153. {
155. {
157. vehicleCreator.CreateVehicle();
159. vehicle.ShowInfo();
160.
161. Console.WriteLine("---------------------------------------------");
162.
164. vehicleCreator.CreateVehicle();
166. vehicle.ShowInfo();
167.
168. Console.ReadKey();
169. }
170. }
2. The creation of objects should be independent from the way the object's parts
are assembled.
Abstract Factory patterns acts a super-factory which creates other factories. This pattern
is also called as Factory of factories. In Abstract Factory pattern an interface is
responsible for creating a set of related objects, or dependent objects without specifying
their concrete classes.
Abstract Factory Pattern - UML Diagram & Implementation
The UML class diagram for the implementation of the abstract factory design pattern is
given below:
The classes, interfaces and objects in the above UML class diagram are as follows:
1. AbstractFactory
2. ConcreteFactory
3. AbstractProduct
This is an interface which declares a type of product.
4. ConcreteProduct
5. Client
C# - Implementation Code
1. public interface AbstractFactory
2. {
3. AbstractProductA CreateProductA();
4.
5. AbstractProductB CreateProductB();
6. }
7.
9. {
11. {
13. }
14.
16. {
19.}
20.
22.{
24. {
26. }
27.
29. {
31. }
32.}
33.
35.
37.
39.
41.
45.
47.{
50.
52. {
55. }
56.}
Abstract Factory Pattern - Example
Who is what?
The classes, interfaces and objects in the above class diagram can be identified as
follows:
5. VehicleClient - Client
C# - Sample Code
1. /// <summary>
3. /// </summary>
4. interface VehicleFactory
5. {
8. }
9.
10./// <summary>
12./// </summary>
14.{
16. {
18. {
23. default:
25. }
26.
27. }
28.
30. {
32. {
37. default:
39. }
40.
41. }
42.}
43.
44./// <summary>
48.{
50. {
52. {
57. default:
59. }
60.
61. }
62.
64. {
66. {
73. }
74.
75. }
76.}
77.
78./// <summary>
80./// </summary>
81.interface Bike
82.{
84.}
85.
86./// <summary>
88./// </summary>
89.interface Scooter
90.{
92.}
93.
94./// <summary>
98.{
100. {
102. }
103. }
104.
109. {
111. {
113. }
114. }
115.
120. {
121. public string Name()
122. {
124. }
125. }
126.
131. {
133. {
135. }
136. }
137.
142. {
145.
146. public VehicleClient(VehicleFactory factory, string type)
147. {
150. }
151.
153. {
155. }
156.
158. {
160. }
161.
162. }
163.
168. {
170. {
171. VehicleFactory honda = new HondaFactory();
173.
175. Console.WriteLine(hondaclient.GetBikeName());
176. Console.WriteLine(hondaclient.GetScooterName());
177.
179. Console.WriteLine(hondaclient.GetBikeName());
180. Console.WriteLine(hondaclient.GetScooterName());
181.
184.
186. Console.WriteLine(heroclient.GetBikeName());
187. Console.WriteLine(heroclient.GetScooterName());
188.
190. Console.WriteLine(heroclient.GetBikeName());
191. Console.WriteLine(heroclient.GetScooterName());
192.
193. Console.ReadKey();
194. }
195. }
Abstract Factory Pattern Demo - Output
Note
1. Internally, Abstract Factory use Factory design pattern for creating objects. But it
can also use Builder design pattern and prototype design pattern for creating objects. It
completely depends upon your implementation for creating objects.
3. When Abstract Factory, Builder, and Prototype define a factory for creating the
objects, we should consider the following points :
1. Abstract Factory use the factory for creating objects of several classes.
2. Builder use the factory for creating a complex object by using simple
objects and a step by step approach.
In Factory pattern, we create object without exposing the creation logic. In this pattern,
an interface is used for creating an object, but let subclass decide which class to
instantiate. The creation of object is done when it is required. The Factory method
allows a class later instantiation to subclasses.
The classes, interfaces and objects in the above UML class diagram are as follows:
1. Product
This is an interface for creating the objects.
2. ConcreteProduct
3. Creator
This is an abstract class and declares the factory method, which returns an object of
type Product.
4. ConcreteCreator
This is a class which implements the Creator class and overrides the factory method to
return an instance of a ConcreteProduct.
C# - Implementation Code
1. interface Product
2. {
3.
4. }
5.
7. {
8. }
9.
11.{
12.}
13.
14.abstract class Creator
15.{
17.}
18.
20.{
22. {
24. {
28. }
29. }
30.}
Factory Method Pattern - Example
Who is what?
The classes, interfaces and objects in the above class diagram can be identified as
follows:
1. IFactory - Interface
3. VehicleFactory - Creator
C# - Sample Code
1. using System;
2. namespace Factory
3. {
4. /// <summary>
5. /// The 'Product' interface
6. /// </summary>
8. {
10. }
11.
16. {
18. {
20. }
21. }
22.
27. {
29. {
30. Console.WriteLine("Drive the Bike : " + miles.ToString() + "km");
31. }
32. }
33.
38. {
40.
41. }
42.
47. {
49. {
51. {
56. default:
58. }
59. }
60.
61. }
62.
67. {
69. {
71.
73. scooter.Drive(10);
74.
76. bike.Drive(20);
77.
78. Console.ReadKey();
79.
80. }
81. }
82.}
2. Parent class allows later instantiation to subclasses means the creation of object
is done when it is required.
4. A class (creator) will not know what classes it will be required to create.
Bridge pattern is used to separate an abstraction from its implementation so that both
can be modified independently.
This pattern involves an interface which acts as a bridge between the abstraction class
and implementer classes and also makes the functionality of implementer class
independent from the abstraction class. Both types of classes can be modified without
affecting to each other.
The UML class diagram for the implementation of the bridge design pattern is given
below:
The classes, interfaces and objects in the above UML class diagram are as follows:
1. Abstraction
This is an abstract class and containing members that define an abstract business
object and its functionality. It contains a reference to an object of type Bridge. It can
also acts as the base class for other abstractions.
2. Redefined Abstraction
This is a class which inherits from the Abstraction class. It extends the interface defined
by Abstraction class.
3. Bridge
This is an interface which acts as a bridge between the abstraction class and
implementer classes and also makes the functionality of implementer class
independent from the abstraction class.
These are classes which implement the Bridge interface and also provide the
implementation details for the associated Abstraction class.
C# - Implementation Code
1. public abstract class Abstraction
2. {
4.
6. {
7. Console.WriteLine("ImplementationBase:Operation()");
8. Implementer.OperationImplementation();
9. }
10.}
11.
13.{
15. {
16. Console.WriteLine("RefinedAbstraction:Operation()");
17. Implementer.OperationImplementation();
18. }
19.}
20.
22.{
24.}
25.
27.{
29. {
30. Console.WriteLine("ImplementationA:OperationImplementation()");
31. }
32.}
33.
35.{
37. {
38. Console.WriteLine("ImplementationB:OperationImplementation()");
39. }
40.}
Bridge Pattern - Example
Who is what?
The classes, interfaces and objects in the above class diagram can be identified as
follows:
C# - Sample Code
1. /// <summary>
5. {
10.}
11.
12./// <summary>
14./// </summary>
16.{
18. {
20. }
21.}
22.
23./// <summary>
25./// </summary>
27.{
28. public string UserComments { get; set; }
29.
31. {
34. }
35.}
36.
37./// <summary>
39./// </summary>
41.{
43.}
44.
45./// <summary>
47./// </summary>
49.{
51. {
54.}
55.
56./// <summary>
58./// </summary>
60.{
62. {
64. }
65.}
66.
67./// <summary>
69./// </summary>
71.{
73. {
75. }
76.}
77.
78./// <summary>
80./// </summary>
81.class Program
82.{
84. {
88.
92.
94. message.Send();
95.
97. message.Send();
98.
100. message.Send();
101.
106.
108. usermsg.Send();
109.
110. Console.ReadKey();
111. }
112. }
3. The Bridge pattern is used when a new version of a software or system is brought
out, but the older version of the software still running for its existing client. There is no
need to change the client code, but the client need to choose which version he wants to
use.
Note
Bridge pattern has nearly the same structure as the Adapter Pattern. But it is used when
designing new systems instead of the Adapter pattern which is used with already
existing systems.
Adapter pattern acts as a bridge between two incompatible interfaces. This pattern
involves a single class called adapter which is responsible for communication between
two independent or incompatible interfaces.
For Example: A card reader acts as an adapter between memory card and a laptop.
You plugins the memory card into card reader and card reader into t he laptop so that
memory card can be read via laptop.
The UML class diagram for the implementation of the Adapter design pattern is given
below:
The classes, interfaces and objects in the above UML class diagram are as follows:
1. ITarget
2. Adapter
This is a class which implements the ITarget interface and inherits the Adaptee class. It
is responsible for communication between Client and Adaptee.
3. Adaptee
This is a class which have the functionality, required by the client. However, its
interface is not compatible with the client.
4. Client
This is a class which interact with a type that implements the ITarget interface.
However, the communication class called adaptee, is not compatible with the client
C# - Implementation Code
1. public class Client
2. {
3. private ITarget target;
4.
6. {
7. this.target = target;
8. }
9.
11. {
12. target.MethodA();
13. }
14.}
15.
17.{
19.}
20.
22.{
24. {
25. MethodB();
26. }
27.}
28.
30.{
32. {
34. }
35.}
Who is what?
The classes, interfaces and objects in the above class diagram can be identified as
follows:
4. ThirdPartyBillingSystem - Client
C# - Sample Code
1. /// <summary>
3. /// </summary>
5. {
7.
9. {
11. }
12.
14. {
17.
20. {
21. Console.Write(item);
22. }
23.
24. }
25.}
26.
27./// <summary>
29./// </summary>
31.{
33.}
34.
35./// <summary>
37./// </summary>
39.{
41. {
43.
48.
51.}
52.
53./// <summary>
55./// </summary>
57.{
59. {
63. {
64. employeeList.Add(employee[0]);
65. employeeList.Add(",");
66. employeeList.Add(employee[1]);
67. employeeList.Add(",");
68. employeeList.Add(employee[2]);
69. employeeList.Add("\n");
70. }
71.
73. }
74.}
75.
76.///
78.///
79.class Program
80.{
82. {
85. client.ShowEmployeeList();
86.
87. Console.ReadKey();
88.
89. }
90.}
1. Allow a system to use classes of another system that is incompatible with it.
2. Allow communication between new and already existing system which are
independent to each other
3. Ado.Net SqlAdapter, OracleAdapter, MySqlAdapter are best example of Adapter
Pattern.
Note
1. Internally, Adapter use Factory design pattern for creating objects. But it can also
use Builder design pattern and prototype design pattern for creating product. It
completely depends upon your implementation for creating products.
3. When Adapter, Builder, and Prototype define a factory for creating the products,
we should consider the following points :
2. Builder use the factory for creating a complex product by using simple
objects and a step by step approach.