|
|
DESIGN PATTERNS RAGA GEETHIKA
CONTENTS What are Design Patterns Why do we need Design Patterns Classification of Design Patterns Implementation Conclusion
INTRODUCTION “Gang of four”: Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides Authors of Elements of Reusable Object-Oriented Software. Design patterns are recognized solutions to common problems. Design patterns are represented as relationships between classes and objects. The solution facilitates the development of highly cohesive modules with minimal coupling. Software professionals can immediately picture the high-level design in their heads when they refer the name of the pattern used to solve a particular issue when discussing system design.
DESIGN PATTERNS Creational Structural Behavioral Factory Abstract Factory Flyweight Singleton Builder Adapter Bridge Composite Decorator Facade Proxy Chain of responsibility Command Iterator Mediator Momento Observer State Strategy Visitor Template method
Factory method This pattern is used to create concrete class instances without specifying the exact class type Where its implemented. Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses. Factory methods encapsulate the creation of objects. This can be useful if the creation process is very complex, for example if it depends on settings in configuration files or on user input.
Abstract Factory Provide an interface for creating families of related or dependant objects without specifying their concrete classes. A framework is created for creating sub-components that inherit from a common component. Ex: Central Government Rules (Abstract factory Class) Methods should have policy. Should have Courts. State1 Rules State2 Rules. All the methods are inherited from Abstract Factory class.
Flyweight pattern The flyweight pattern is used whenever you have large amount of small objects that share common information. The flyweight distinguishes between inner state (sometime called intrinsic state) and outer state (sometime called extrinsic state) of the object. The example include 3 parts – The flyweight factory (NumberFactory) The flyweight itself (Number) Flyweight concretes (One, Two, Three). Whenever a number is needed and isn't in the factory I build it and then return it in the GetNumber method.(Lazy loading)
Singleton pattern The Singleton Design Pattern ensures that only a single instance of a given object can exist. It does this by making the class constructor private so that it [the singleton itself] has full control over when the class instance is created. Ex: All Banking related. Builder Pattern The builder pattern describe a way to separate an object from it's construction. The same construction method can create different representation of the object. EX: When have a house contractor that is responsible to build the house in the right order. We have a builder abstract class that defines the interface for the concrete builders.
Adapter pattern Convert the interface of a class into another interface clients expect. Adpater lets the classes work together that couldn't otherwise because of incompatible interfaces Ex: Third Party Payment System Bridge Pattern The bridge pattern decouples an abstraction from its implementation so the two can vary independently. The pattern helps us whenever we need to select or switch the implementation at runtime.
Composite pattern Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly". Ex : SiteMap Decorator Pattern The Decorator patterns allows an objects behavior to be dynamically altered at runtime. This change in behavior is accomplished by wrapping an object with a new object. This new wrapper object is the Decorator. Ex: A simple example of a Decorator can be demonstrated with a report class. Some reports may need a header and some may also need a footer.
Facade pattern The Facade Design Pattern provides a simple interface and controls access to a series of complicated interfaces and or sub systems. A facade can reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system Making the webservice acting as the Facade or the interface to many different dll's each representing a subsystem. Proxy Pattern Provides a surrogate or placeholder for another object to control access to it Ex: enable caching on an ASP.net page
Chain of Responsibility pattern You have more than one object that may handle a request. You have a scenario that you need to pass a request to one of several objects without specifying the receiver. You have handlers of a request that should be specified dynamically. We have three types of workers: worker, manager and a senior manager. Every worker type can handle request that are lower than the request limit they have. Whenever a request that is bigger than the request limit arrives the worker type deliver the request to the next handler in the chain until there is no one who can handle the request.
Command pattern Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. we can use the Command pattern to program buttons of the keyboard so that the keyboard need not know what application it is executing. Iterator Pattern Provide a way to access aggregate objects sequentially without the knowledge of the structure of the aggregate. The pattern is widely used in C# and in .Net framework we have the IEnumerator and IEnumerable interfaces to help us to implement iterators for aggregates. The LINQ extensions are built upon the IEnumerable interface which is a part of the iterator pattern implementation in .Net framework.
Mediator pattern Encapsulate the interaction between a set of objects and also encapsulate the protocol between those objects. Behavior that is distributed between some objects can be grouped or customized. The performance impact is caused because every communications pass through the mediator on the way to the communicated object A chat room application Memento Pattern You need to save object’s state and use the saved state later in order to restore the saved state. You don’t want to expose the internal state of your object. Scientific computing or in computer games
Observer pattern Objects need to be notified of a change in another objects You need that the object that notify its state change would not know about its subscribers. Ex: MVC framework State Pattern Allows an object to change it behavior when its internal state changes. pattern is used in programming to represent the state of an object. The main issues is to understand that every concrete state is responsible to change the current context behavior to the next context behavior in the chain of behaviors.
Strategy pattern Allows you to use multiple algorithms interchangeably. Breaking these complex algorithms down into smaller more manageable algorithms might make your code more readable and more easily maintained. Ex: A software system uses a single authorization checking class to determine access rights within the system. Access rights will be different for Anonymous users, Logged-In users and System Administrators. Visitor Pattern Enables us to create new operations to be performed on an existing structure. With the pattern you define two class hierarchies. The first for the elements which the operations will operate on. The second is for the visitors that define the operations on the elements.
Visitor pattern (contd..) The classes that defined in the object structure rarely change, but you need sometimes to define new operations on the object structure. You have a class hierarchy with many distinct operations. The pattern is used in order to extend elements of an object structure with new operations without the need to change the elements. Template method Which lets subclasses implement behaviour that can vary. Pattern is used in programming to represent the state of an object. The main issues is to understand that every concrete state is responsible to change the current context behavior to the next context behavior in the chain of behaviors.
Conclusion http://en.wikipedia.org/ http://www.asp.net/ http://blogs.msdn.com http://msdn.microsoft.com
| URL: |
No comments posted yet
Comments