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.
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.
Creational Patterns
Builder Pattern
Another Real world Dialog without Builder pattern style
Another Real world Dialog with Builder pattern style
In this example, we provides only data to the process.
Problem 1 – Huge constructor parameter
Solution for this problem
We can introduce properties for these class creation by remove the Constructor method from the class.
Problem 2 – We cannot control the process and process steps either
Solution for this problem
Here, we created separate class to build this sandwich. In this way, we could make sure that sandwich constructed properly.
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.
Solution to this problem
Builder
Concrete Builder
Director
Client code which calls director
Sample code for Builder pattern
Real world example for Builder pattern
What we built
The Prototype Pattern
Note: In this pattern, We are going to copying object instead of Newing object.
Problem 1
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
Problem 2
Solution to this problem
Sample code for Prototype Pattern
Real world Example of Prototype Pattern
Language Examples
Singleton Pattern
Sample code for Singleton Pattern
Real world example of Singleton pattern
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.
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.
Factory Patterns or Factory Method
Demo of example without Factory pattern
Simple Factory Pattern
Demo of Simple Factory Pattern
Factory Method Pattern
Demo of Factory Method Pattern
Sample code for Factory method
Real world Example of Factory Method
Abstract Factory Pattern
Sample code of Abstract Factory
Real world Example of Abstract Factory
Structural Patterns
Adapter Pattern
Demo of Adapter Pattern
Example existing .NET FCL Adapter Pattern
Sample code Example
Real world Example
Bridge Pattern
Initial Problem
Abstraction from the implementation
Demo of Initial Problem with first step of Abstraction
Refactoring to bridge pattern
Sample Code of Bridge pattern
Real world Example
What we did
Note: Left side Abstraction is Our ManuScript base class, implementor is IFormatter Interface and Concreate Implementor is StandardFormatter class.
Summary
Behavioural Patterns
Chain of Responsibility Pattern
Demo of Expense Report without Chain of Responsibility pattern
Expense Report with Chain of Responsibility
Sample code for Chain of Responsibility
Real world example of Chain of Responsibility
Command Pattern
Demo of Example of Command pattern without Command Pattern
Example of Command pattern
Sample code for Command pattern
Real world example for Command Pattern
Observer Pattern
No comments:
Post a Comment