There are 3 major categories of design patterns
- Creational Pattern - designed to solve problems related to the creational process of objects and classes.
- Structural pattern - designed to solve problems related to the composition of an object or class.
- Behavioral pattern - designed to solve problems related to the behaviour and interaction between objects or classes.
1. Abstract Factory - provides a method for creating objects or classes that are related or dependent, without specifying the concrete class.
Example -
Factory.CreateProductA();
Factory.CreateProductB();
2. Factory Method - defines an interface for creating an object, but lets subclasses decide which class to instantiate.
Example -
FactoryA.Create();
FactoryB.Create();
3. Builder - Separates the construction of a complex object from its representation so that the same construction process can create different representations.
Example -
Builder.BuildPartA();
Builder.BuildPartB();
Build.GetFinalProduct();
4. Prototype - specifies the kind of objects to create using a prototypical instance, and creates new objects by copying this prototype.
Example -
Product = Prototype.Clone();
5. Singleton - Ensures a class has only one instance and provides global point of access to it.
Example -
Singletion.DoSomething();
.
1. Adapter - Converts the inerface of a class into another interface that client expect.
Example -
Target obj = new Adapter();
obj.DoSomething();
2. Bridge - decouples an abstraction from its implementation so that the two can vary independently.
Example -
Var obj = new ConcreteA();
obj.DoSomething();
obj = new ConcreteB();
obj.DoSomething();
3. Composition - Composes objects into tree structure to represent part-whole hierarchies.
Example -
Composite.Add(objA);
Composite.Add(objB);
4. Decorator - Attaches additional responsibilities to an object dynamically.
Example -
obj.SetDecorator(decA);
obj.Dodecorator();
obj.SetDecorator(decB);
obj.Dodecorator();
5. Facade - provides a unified interface to a set of interfaces in a subsystem.
Example -
Facade.MethodFromObjA();
Facade.MethodfromObjB();
6. Flyweight - Uses sharing to support large numbers of fine-grained object efficiently.
Example -
A = FWFactory.GetFW("A");
B = FWFactory.GetFW("B");
7. Proxy - Provides a surrogate or placeholder for another object to control access to it.
Example -
var proxy = new Proxy();
proxy.RequestChannel();
.
1. Chain of Response - Avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
Example -
Employee.SetSupervisor(Manager);
Employee.SetSupervisor(Director);
Employee.Execute();
2. Command - Encapsulates a request as an object and supports undoable operations.
Example -
Command.DoSomething();
Command.Redo();
Command.Undo();
3. Interpreter - Given a language, defines a representation for its grammer.
Example -
Vocabulary.Add(expressionA);
Vocabulary.Add(expressionB);
Vocabulary.Translate();
4. Mediator - defines an object that encapsulates how a set of objects interact.
Example -
Mediator.Add(objA);
Mediator.Add(objB);
ObjA.Send("ObjB", Message);
5. Memento - Without violating encapulation, captures and externalizes an object's internal state so that the object can be restored to this state later.
Example -
ObjA.Name = "ObjA";
Memento.Save(ObjA);
Memento.Restore(ObjA);
6. Observer - defines a one-to-many dependency between objects so that when one object changes state, all its dependencies are notified and updated automatically.
Example -
Observer.Attach(ObjA);
Observer.Attach(ObjB);
Observer.ChangeSomething();
Observer.Notify();
7. State - Allows an object to alter its behaviour when its internal state changes. The object will appear to change its class.
Example -
Context.Add(ObjA);
ObjA.ChangeState("A");
ObjA.ChangeState("B");
8. Strategy - defines a family of algorithms, encapsulates each one, and makes them interchangeable. Using Strategy, the algorithms can vary independently from clients that use it.
Example -
List.Add(ObjA);
List.Add(objB);
List.SortStrategy(Ascending);
List.SortStrategy(Descending);
9. Templete Method - defines the skeleton of an algorithm in an operation, deferring some steps to subclasses.
Example -
Template A = new Student();
Template B = new Teacher();
A.write();
B.Write();
10. Visitor - represents an operation to be preformed on the elements of an object structure.
Example -
List.Add(Student("A"));
List.Add(Student("B"));
List.Visit(new VoteVisitor());