Design-Pattern Text Book.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
An Introduction to Design Patterns An Introduction to Design Patterns (C#, WPF, ASP.NET, AJAX, PATTERNS) Formatted By- Ashish Tripathi Email: [email protected] Note: All the contents present into the book is extracted from the site http://www.dofactory.com/Patterns/Patterns.aspx without any textual change only formatting and presentation of the text is changed. For any genuine change in the book you can ask for the editable copy of the book by sending a mail to [email protected] with subject line ‘Design Patterns’ ****** 1 For more informations visit http://www.dofactory.com/Patterns/Patterns.aspx An Introduction to Design Patterns Design Patterns...........................................................................................................3 1. Introduction...................................................................................................3 2. Type of Design Patterns..................................................................................4 2.1 Creational Patterns .............................................................................4 2.2 Structural Patterns .............................................................................4 2.3 Behavioral Patterns.............................................................................4 3. Creational Design Patterns .............................................................................5 3.1 Abstract Factory Design Pattern ..........................................................5 3.2 Builder Design Pattern ......................................................................11 3.3 Factory Method Design Pattern..........................................................18 3.4 Prototype Design Pattern...................................................................23 3.5 Singleton Design Pattern ...................................................................28 4. Structural Design Patterns ...........................................................................32 4.1 Adapter Design Pattern .....................................................................32 4.2 Bridge Design Pattern .......................................................................37 4.3 Composite Design Pattern .................................................................43 4.4 Decorator Design Pattern ..................................................................49 4.5 Facade Design Pattern ......................................................................55 4.6 Flyweight Design Pattern...................................................................60 4.7 Proxy Design Pattern.........................................................................66 5. Behavioral Design Patterns...........................................................................70 5.1 Chain of Responsibility Design pattern ..............................................70 5.2 Command Design Pattern..................................................................76 5.3 Interpreter Design Pattern.................................................................82 5.4 Iterator Design pattern......................................................................87 5.5 Mediator Design Pattern....................................................................94 5.6 Memento Design Pattern .................................................................100 5.7 Observer Design Pattern..................................................................106 5.8 State Design Pattern .......................................................................112 5.9 Strategy Design Pattern...................................................................120 5.10 Template Method Design Pattern ...................................................125 5.11 Visitor Design Pattern ...................................................................130 ****** 2 For more informations visit http://www.dofactory.com/Patterns/Patterns.aspx An Introduction to Design Patterns Design Patterns 1. Introduction Design patterns are recurring solutions to software design problems you find again and again in real-world application development. Patterns are about design and interaction of objects, as well as providing a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges. The Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral. Here you will find information on these important patterns. To give you a head start, the C# source code is provided in 2 forms: 'structural' and 'real-world'. Structural code uses type names as defined in the pattern definition and UML diagrams. Real-world code provides real-world programming situations where you may use these patterns. A third form, '.NET optimized' demonstrates design patterns that exploit built-in .NET 2.0 features, such as, generics, attributes, delegates, and reflection. These and much more are available in our Design Pattern Framework 2.0TM. See our Singleton page for a .NET 2.0 Optimized code sample. ****** 3 For more informations visit http://www.dofactory.com/Patterns/Patterns.aspx An Introduction to Design Patterns 2. Type of Design Patterns 2.1 Creational Patterns Abstract Factory Creates an instance of several families of classes Builder Separates object construction from its representation Factory Method Creates an instance of several derived classes Prototype A fully initialized instance to be copied or cloned Singleton A class of which only a single instance can exist 2.2 Structural Patterns Adapter Match interfaces of different classes Bridge Separates an object’s interface from its implementation Composite A tree structure of simple and composite objects Decorator Add responsibilities to objects dynamically Facade A single class that represents an entire subsystem Flyweight A fine-grained instance used for efficient sharing Proxy An object representing another object 2.3 Behavioral Patterns Chain of Resp. A way of passing a request between a chain of objects Command Encapsulate a command request as an object Interpreter A way to include language elements in a program Iterator Sequentially access the elements of a collection Mediator Defines simplified communication between classes Memento Capture and restore an object's internal state Observer A way of notifying change to a number of classes State Alter an object's behavior when its state changes Strategy Encapsulates an algorithm inside a class Template Method Defer the exact steps of an algorithm to a subclass Visitor Defines a new operation to a class without change ****** 4 For more informations visit http://www.dofactory.com/Patterns/Patterns.aspx An Introduction to Design Patterns 3. Creational Design Patterns 3.1 Abstract Factory Design Pattern 3.1.1 Definition Provide an interface for creating families of related or dependent objects without specifying their concrete classes. 3.1.2 UML class diagram The classes and/or objects participating in this pattern are: AbstractFactory (ContinentFactory) Declares an interface for operations that create abstract products ConcreteFactory (AfricaFactory, AmericaFactory) Implements the operations to create concrete product objects AbstractProduct (Herbivore, Carnivore) Declares an interface for a type of product object Product (Wildebeest, Lion, Bison, Wolf) Defines a product object to be created by the corresponding concrete factory ****** 5 For more informations visit http://www.dofactory.com/Patterns/Patterns.aspx An Introduction to Design Patterns Implements the AbstractProduct interface Client (AnimalWorld) Uses interfaces declared by AbstractFactory and AbstractProduct classes 3.1.3 Sample code in C# This structural code demonstrates the Abstract Factory pattern creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. // Abstract Factory pattern -- Structural example using System; namespace DoFactory.GangOfFour.Abstract.Structural { // MainApp test application class MainApp { public static void Main() { // Abstract factory #1 AbstractFactory factory1 = new ConcreteFactory1(); Client c1 = new Client(factory1); c1.Run(); // Abstract factory #2 AbstractFactory factory2 = new ConcreteFactory2(); Client c2 = new Client(factory2); c2.Run(); // Wait for user input Console.Read(); } } // "AbstractFactory" abstract class AbstractFactory { public abstract AbstractProductA CreateProductA(); public abstract AbstractProductB CreateProductB(); } // "ConcreteFactory1" class ConcreteFactory1 : AbstractFactory { public override AbstractProductA CreateProductA() { return new ProductA1(); } public override AbstractProductB CreateProductB() ****** 6 For more informations visit http://www.dofactory.com/Patterns/Patterns.aspx An Introduction to Design Patterns { return new ProductB1(); } } // "ConcreteFactory2" class ConcreteFactory2 : AbstractFactory { public override AbstractProductA CreateProductA() { return new ProductA2(); } public override AbstractProductB CreateProductB() { return new ProductB2(); } } // "AbstractProductA" abstract class AbstractProductA { } // "AbstractProductB" abstract class AbstractProductB { public abstract void Interact(AbstractProductA a); } // "ProductA1" class ProductA1 : AbstractProductA { } // "ProductB1" class ProductB1 : AbstractProductB { public override void Interact(AbstractProductA a) { Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);