Understanding Design Patterns

10
Definition: Design Patterns in object oriented world is reusable solution to common software design problems which occur again and again in real world application developmen t . In other words, it is a template or description for how to solve a problem which can be used in many different situations. Advantage:  DP provides reuse of solution for common s/w design. Once a design pattern has emerg ed to solve a particular problem, that design pattern can be applied countless times on future tasks where a similar problem may arise.  It helps to promote easier program changes as loosely coupled objects are easier to change. Keeping objects small and specialized promotes loose coupling.  They give us a common taxonomy allowing for better communication and understanding the situation.  This allows one to refer to a design concept without explaining the full detail of that concept.  They give us a higher level of abstraction in terms of analysis and design  This allows one to think of the larger design in terms of smaller component areas. Therefore, when one looks at the bigger picture, one can visualize it in terms of its constituent parts. DP  categories and types: There are 23 design patterns also known as Gang of four design patte rns (GoF)  Creational Design Pattern 1. Factory Method 2. Abstract Factory 3. Builder 4. Prototype 5. Singleton  Structural Design Patterns 1. Adapter 2. Bridge 3. Composite 4. Decorator 5. Façade 6. Flyweight 7. Proxy  Behavioral Design Patterns 1. Chain of Responsibility 2. Command 3. Interpreter 4. Iterator 5. Mediator 6. Memento 7. Observer 8. State 9. Strategy 10. Visitor 11. Template Method

description

Understanding Design Patterns

Transcript of Understanding Design Patterns

Definition: Design Patterns in object oriented world is reusable solution to common software design problems which occur again and again in real world application development. In other words, it is a template or description for how to solve a problem which can be used in many different situations.Advantage: DP provides reuse of solution for common s/w design. Once a design pattern has emerged to solve a particular problem, that design pattern can be applied countless times on future tasks where a similar problem may arise. It helps to promote easier program changes as loosely coupled objects are easier to change. Keeping objects small and specialized promotes loose coupling. They give us a common taxonomy allowing for better communication and understanding the situation. This allows one to refer to a design concept without explaining the full detail of that concept. They give us a higher level of abstraction in terms of analysis and design This allows one to think of the larger design in terms of smaller component areas. Therefore, when one looks at the bigger picture, one can visualize it in terms of its constituent parts.

DP categories and types:There are 23 design patterns also known as Gang of four design patterns (GoF) Creational Design Pattern1. Factory Method2. Abstract Factory3. Builder4. Prototype5. Singleton Structural Design Patterns1. Adapter2. Bridge3. Composite4. Decorator5. Faade6. Flyweight7. Proxy Behavioral Design Patterns1. Chain of Responsibility2. Command3. Interpreter4. Iterator5. Mediator6. Memento7. Observer8. State9. Strategy10. Visitor11. Template Method

Creational Design patterns These patterns deal with the process of object creation in such a way that they are separated from their implementing system. This way, it provides more flexibility in deciding which object needs to be created or instantiated for a given scenario. There are five such patterns:1) Abstract Factory It is used to create a set of related objects or dependent objects. The family of objects created by factory is determined at run-time according to the selection of concrete factory class. Abstract factory pattern acts as super factory which creates other factories. In abstract factory, interface is responsible for creating a set of related objects or dependent objects without specifying their concrete classes.

The classes, objects and interfaces used in the above UML diagram are described below:1. Client: This class is used Abstract Factory and Abstract Product interfaces to create family of related objects.2. Abstract Factory: This is an interface which is used to create abstract products.3. Abstract Product: This is an interface which is used to declare type of products.4. Concrete Factory: This is a class which implements the abstract factory interface to create concrete products.5. Concrete Product: This is a class which implements the abstract product interface to create products.

The following code shows the basic template code of the abstract factory design pattern implemented using C#.NET:

In the above abstract factory design pattern source code template client has twoprivatefields that hold the instances ofabstractproduct classes. These objects will be accessed by inheriting their base class interface. When a client is instantiated, a concrete factory object is passed to its constructor and populateprivatefields of client with appropriate data or value.TheAbstractfactoryis base class to concrete factory classes which generate or create set of related objects. This base class contains methods definition for each type of object that will be instantiated. The base class is declared asAbstractso that it can be inherited by other concrete factory subclasses.The concrete factory classes are inheriting fromAbstractfactoryclass and override the method of base class to generate a set of related objects required by client. There can be n number of concrete factory classes depending on software or application requirement.Abstractproductis the base class for the types of objects that factory class can create. There should be one base type for every distinct types of product required by client. The concrete product classes are inheriting fromAbstractproductclass. Each class contains specific functionality. Objects of these classes are generated byabstractfactoryclasses to populate client.Real World Example of Abstract Factory Design Pattern using C#.NETAs an example, consider a system that does the packaging and delivery of items for a web-based store. The company delivers two types of products. The first is a standard product that is placed in a box and delivered through the post with a simple label. The second is a delicate item that requires shockproof packaging and is delivered via a courier.In this situation, there are two types of objects required, a packaging object and a delivery documentation object. We could use two factories to generate these related objects. The one factory will be responsible for creating packaging and other delivery objects for standard parcels. The second will be responsible for creating packaging and delivery objects for delicate parcels.Class Client

AbstractFactory Patterns Form

OUTPUT

In the above example, code creates two client objects, each passing to different type of factory constructor. Types of generated objects are accessed through the client properties.NoteWhile studying abstract factory pattern, one question comes in my mind, i.e., what are concrete classes? So I Google searched the same and below is the answer to my question.Concrete class is nothing but normal class, which is having all basic class features, like variables, methods, constructors, etc.We can create an instance of the class in the other classes.2) SingletonSingleton design pattern is one of simplest design patterns. This pattern ensures that class has only one instance and provides global point of accessing it. The pattern ensures that only one object of a particular class is ever created. All further references to objects of the singleton class refer to the same underlying instance.There are situations in a project where we want only one instance of the object to be created and shared between the clients. No client can create an instance from outside. It is more appropriate than creating a global variable as this may be copied and leading to multiple access points.In the above singleton patterns UML diagram GetInstance method should be declared asstatic. This method returns single instance held inprivateinstance variable. In singleton pattern, define all the methods and instance asstatic. Thestatickeyword ensures that only one instance of object is created and you can call methods of class without creating object.The constructor of class is marked asprivate. This prevents any external classes from creating new instances. The class is also sealed to prevent inheritance, which could lead to sub classing that breaks the singleton rules.The following code shows the basic template code of the singleton design pattern implemented using C#.NET:The eager initialization of singleton pattern is as follows:

Lazy initialization of singleton pattern:

Thread-safe (Double-checked Locking) initialization of singleton pattern:

In the above code "lockThis" object and the use of locking within the "GetInstance" method. As programs can be multithreaded, it is possible that two threads could request the singleton before the instance variable is initialized. By locking the dummy "lockThis" variable, all other threads will be blocked. This means that two threads will not be able to simultaneously create their own copies of the object.Real World ExampleI am trying to apply this pattern in my application where I want to maintain application state for user login information and any other specific information which is required to be instantiated only once and held only one instance.Class ApplicationState

Singleton Pattern Form

OUTPUT

The above sample code creates two new variables and assigns the return value of theGetStatemethod to each. They are then compared to check that they both contain the same values and a reference to the same object.