Sunday 9 March 2014

Design Patterns Library

 

 

Introduction to Design Patterns

 

Design patterns are solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects.

 

 

 

image

 

 

 

image

 

 

 

image

 

 

image

 

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

The 23 Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral.

image 

 

 

 

Creational Patterns

 

Builder Pattern

image

 

 

image

 

 

image

 

 

image

 

 

Another Real world Dialog without Builder pattern style

 

image

 

 

 

Another Real world Dialog with Builder pattern style

 

In this example, we provides only data to the process.

image

 

 

 

Problem 1 – Huge constructor parameter

 

image

 

 

image

 

 

image

 

 

Solution for this problem

We can introduce properties for these class creation by remove the Constructor method from the class.

 

image

 

image

 

 

 

Problem 2 – We cannot control the process and process steps either

 

image

 

 

Solution for this problem

 

Here, we created separate class to build this sandwich.  In this way, we could make sure that sandwich constructed properly.

 

image

 

 

image

 

 

image

 

 

 

Problem 3 – When we create more different type of sandwich

 

We have to create new builder classes if you want to introduce new type of sandwiches.

image

 

 

 

Solution to this problem

 

Builder

image

 

 

Concrete Builder

 

image

 

 

Director

image

 

 

image

 

 

image

 

 

 

Client code which calls director

 

image

 

 

Sample code for Builder pattern

image

 

 

image

 

image

 

image

 

 

image

 

 

 

 

Real world example for Builder pattern

image

 

image

 

image

 

 

image

 

 

image

 

 

image

 

image

 

 

What we built

image

 

 

image

 

 

 

image

 

 

 

image

 

 

image

 

 

image

 

 

 

The Prototype Pattern

image

 

 

image

 

 

image

 

 

image

 

Note:  In this pattern, We are going to copying object instead of Newing object.

 

 

 

image

 

 

 

image

 

 

 

Problem 1

image

 

 

image

 

Note:  In this above example, when we run this program it will take time to fetch some data from web.  This is right situation to use proto type instance in our application.

 

 

Solution for this Problem

image

 

 

image

 

 

image

 

 

 

Problem 2

image

 

 

image

 

 

Solution to this problem

 

image

 

 

image

 

 

image

 

 

Sample code for Prototype Pattern

image

 

image

 

 

image

 

image

 

 

Real world Example of Prototype Pattern

image

 

 

image

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

Language Examples

 

image

 

 

image

 

 

 

Singleton Pattern

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

image

 

 

image

 

 

image

 

 

 

Sample code for Singleton Pattern

image

 

image

 

 

 

Real world example of Singleton pattern

 

image

 

image

 

image

 

 

The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. The following example includes a lock statement.

image

 

The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.

The section Threading (C# and Visual Basic) discusses threading.

The lock keyword calls Enter at the start of the block and Exit at the end of the block. A ThreadInterruptedException is thrown if Interrupt interrupts a thread that is waiting to enter a lock statement.

In general, avoid locking on a public type, or instances beyond your code's control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:

  • lock (this) is a problem if the instance can be accessed publicly.

  • lock (typeof (MyType)) is a problem if MyType is publicly accessible.

  • lock("myLock") is a problem because any other code in the process using the same string, will share the same lock.

Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.

You can't use the await keyword in the body of a lock statement.

 

image

 

 

image

 

 

image

 

 

image

 

 

 

Factory Patterns or Factory Method

 

image

 

image

 

 

image

 

 

image

 

 

 

Demo of example without Factory pattern

 

image

 

 

image

 

 

 

Simple Factory Pattern

 

image

 

 

Demo of Simple Factory Pattern

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

 

Factory Method Pattern

 

image

 

 

 

Demo of Factory Method Pattern

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

Sample code for Factory method

image

 

image

 

 

image

 

 

 

Real world Example of Factory Method

image

 

image

 

 

image

 

 

image

 

image

 

 

image

 

 

 

Abstract Factory Pattern

 

image

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

Sample code of Abstract Factory

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

Real world Example of Abstract Factory

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

image

 

 

image

 

 

image

 

 

 

 

 

Structural Patterns

 

Adapter Pattern

image

 

image

 

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

image

 

 

image

 

 

Demo of Adapter Pattern

Example existing .NET FCL Adapter Pattern

 

image

 

 

image

 

 

image

 

 

image

 

 

Sample code Example

image

 

image

 

 

 

Real world Example

 

image

 

 

image

 

 

image

 

 

image

 

image

 

image

 

 

image

 

 

image

 

 

 

 

Bridge Pattern

 

image

 

image

 

 

image

 

 

 

Initial Problem

 

image

 

 

 

Abstraction from the implementation

 

image

 

 

Demo of Initial Problem with first step of Abstraction

image

image

 

image

 

image

 

 

 

Refactoring to bridge pattern

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

Sample Code of Bridge pattern

image

 

image

 

image

 

 

Real world Example

image

 

image

 

image

 

image

 

 

image

 

image

 

 

 

What we did

image

 

Note:  Left side Abstraction is Our ManuScript base class, implementor is IFormatter Interface and Concreate Implementor is StandardFormatter class.

 

 

image

 

 

Summary

 

image

 

 

 

Behavioural Patterns

 

Chain of Responsibility Pattern

 

image

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

image

 

 

image

 

 

image

 

 

Demo of Expense Report without Chain of Responsibility pattern

image

 

 

image

 

 

image

 

 

image

 

image

 

 

 

Expense Report with Chain of Responsibility

image

 

image

image

image

 

image

 

 

Sample code for Chain of Responsibility

image

 

 

image

 

image

 

 

image

 

 

 

Real world example of Chain of Responsibility

image

 

 

image

 

image

 

image

 

image

 

image

 

 

image

 

 

 

Command Pattern

image

 

image

 

 

 

Demo of Example of Command pattern without Command Pattern

 

image

 

 

image

 

image

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

Example of Command pattern

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

image

 

image

 

 

image

 

 

image

 

 

 

Sample code for Command pattern

image

 

image

 

image

 

 

 

Real world example for Command Pattern

image

 

image

 

image

 

image

 

 

image

 

image

 

 

image

 

 

image

 

 

 

Observer Pattern

image

 

image

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

image

 

 

 

Demo of Traditional Implementation

No comments:

Post a Comment