Skip to main content

Design Patterns

Design Pattern - Design pattern to build high quality robust applications.

            Developer have to take care to select the right pattern for the right problem. Improper usage lead to unmaintainable, complex and inefficient code.
      
      We shall take a look at how to use the well known Gang of Four (GoF) Design Patterns in C# 4.0 code. Those patterns will work well in any project that uses C# but especially in WPF, WCF, WinForms, ASP.NET projects. The GoF Design Patterns are divided into 3 categories : Creational Patterns, Structural Patterns and Behavioral Patterns. I am going to explain each GoF Design Pattern in detail and will show you examples of how to write good C# 4.0 code that implement those patterns.

Creational Pattern
  1. Abstract Factory Pattern: Create instances of classes belonging to different families
  2. Builder Pattern: Separate representation and object construction
  3. Factory Method Pattern: Create instances of derived classes
  4. Prototype Pattern: Clone or copy initialized instances
  5. Singleton Pattern: Class with only one single possible instance
Structural Pattern
  1. Adapter Pattern: Match interfaces of classes with different interfaces
  2. Bridge Pattern:: Separate implementation and object interfaces
  3. Composite: Simple and composite objects tree
  4. Decorator: Dynamically add responsibilities to objects
  5. Facade: Class that represents sub classes and subsystems
  6. Flyweight: Minimize memory usage by sharing as much data as possible with similar objects
  7. Proxy: Object that represents another object
Behavioural Pattern
  1. Chain of Responsibility: Pass requests between command and processing objects within a chain of objects
  2. Command: Encapsulate a method call as an object containing all necessary information
  3. Interpreter: Include language elements and evaluate sentences in a given language
  4. Iterator: Give sequential access to elements in a collection
  5. Mediator: Encapsulates and simplifies communication between objects
  6. Memento: Undo modifications and restore an object to its initial state
  7. Observer: Notify dependent objects of state changes
  8. State: Change object behavior depending on its state
  9. Strategy: Encapsulate algorithms within a class and make them interchangeable
  10. Template Method: Define an algorithm skeleton and delegate algorithm steps to sub classes so that they may be overridden
  11. Visitor: Add new operations to classes without modifying them
There are several other patterns such as Parallel Patterns, SOA Patterns, Enterprise Architecture Patterns, etc… So if we work in the respective area don’t hesitate to look up patterns that may help you to be more efficient and build better applications.

Comments

Popular posts from this blog

C# IEnumerable and IQueryable

The first important point to remember is IQueryable interface inherits from IEnumerable, so whatever IEnumerable can do, IQueryable can also do.   There are many differences but let us discuss about the one big difference which makes the biggest difference. IEnumerable interface is useful when your collection is loaded using LINQ or Entity framework and you want to apply filter on the collection. Consider the below simple code which uses IEnumerable with entity framework. It’s using a Wherefilter to get records whose EmpId is 2. EmpEntities ent = new EmpEntities(); IEnumerable<Employee> emp = ent.Employees;  IEnumerable<Employee> temp = emp.Where(x => x.Empid == 2).ToList<Employee>(); This where filter is executed on the client side where the IEnumerable code is. In other words all the data is fetched from the database and then at the client its scans and gets the record with EmpId is 2.   But now see the below code we have...