Design Patterns An Introduction Mudasir Qazi - mudasirqazi00@gmail.com 19-Dec-14
Contents / Agenda • Definition • Categories • Creational Design Patterns • Structural Design Patterns • Behavioral Design Patterns • Advantages • When to use? • How to select a Design Patter? Mudasir Qazi - mudasirqazi00@gmail.com 29-Dec-14
Definition 1. A design pattern is a well-proved solution for solving the specific problem/task. 2. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code. • Christopher Alexander was the first person who invented all the following Design Patterns in 1977. But later the Gang of Four - Design patterns, elements of reusable object-oriented software book was written by a group of four persons named as Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides in 1995. That's why all the following Design Patterns are known as Gang of Four (GoF) Design Patterns. Link of this Book : https://books.google.com.pk/books?id=K4qv1D- LKhoC&printsec=frontcover&source=gbs_ge_summary_r&cad=0#v=onepage &q&f=false Mudasir Qazi - mudasirqazi00@gmail.com 39-Dec-14
Categories Following are the main categories of design patterns 1. Creational Design Patterns Creational design patterns are concerned with the way of creating objects. These design patterns are used when a decision must be made at the time of instantiation of a class (i.e. creating an object of a class). 2. Structural Design Patterns Structural design patterns are concerned with how classes and objects can be composed, to form larger structures. The structural design patterns simplifies the structure by identifying the relationships. These patterns focus on, how the classes inherit from each other and how they are composed from other classes. 3. Behavioral Design Patterns Behavioral design patterns are concerned with the interaction and responsibility of objects. In these design patterns, the interaction between the objects should be in such a way that they can easily talk to each other and still should be loosely coupled. That means the implementation and the client should be loosely coupled in order to avoid hard coding and dependencies. Mudasir Qazi - mudasirqazi00@gmail.com 49-Dec-14
Creational Design Patterns • There are following 6 types of creational design patterns. 1. Factory Method Pattern / Factory Pattern / Virtual Constructor Define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. 2. Abstract Factory Pattern / Kit Define an interface or abstract class for creating families of related (or dependent) objects but without specifying their concrete sub-classes. 3. Singleton Pattern Define a class that has only one instance and provides a global point of access to it. 4. Prototyping Pattern Cloning of an existing object instead of creating new one and can also be customized as per the requirement. 5. Builder Pattern Construct a complex object from simple objects using step-by-step approach. 6. Object Pool Pattern To reuse the object that are expensive to create. Mudasir Qazi - mudasirqazi00@gmail.com 59-Dec-14
Structural Design Patterns • There are following 7 types of structural design patterns. 1. Adapter Pattern Adapting an interface into other according to the client expectation. 2. Bridge Pattern Separating abstraction (interface) from implementation. 3. Composite Pattern Allowing client to operate on hierarchy of objects. 4. Decorator Pattern Adding functionality to an object dynamically. 5. Façade Pattern Providing an interface to a set of interfaces. 6. Flyweight Pattern Reusing an object by sharing it. 7. Proxy Pattern Representing another object. Mudasir Qazi - mudasirqazi00@gmail.com 69-Dec-14
Behavioral Design Patterns (1) • There are 12 type of behavioral design patterns 1. Chain of Responsibility Pattern Avoid coupling the sender of a request to its receiver by giving multiple objects a chance to handle the request. 2. Command Pattern / Action / Transaction Encapsulate a request under an object as a command and pass it to invoker object. Invoker object looks for the appropriate object which can handle this command and pass the command to the corresponding object and that object executes the command. 3. Interpreter Pattern To define a representation of grammar of a given language, along with an interpreter that uses this representation to interpret sentences in the language. 4. Iterator Pattern / Cursor To access the elements of an aggregate object sequentially without exposing its underlying implementation. 5. Mediator Pattern Defines an object that encapsulates how a set of objects interact. 6. Memento Pattern The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback). Mudasir Qazi - mudasirqazi00@gmail.com 79-Dec-14
Behavioral Design Patterns (2) • Behavioral design patterns continues to 7. Observer Pattern In Observer Pattern an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. 8. State Pattern Used to encapsulate varying behavior for the same routine based on an object's state object 9. Strategy Pattern / Policy Pattern Enables an algorithm’s behavior to be selected at runtime. 10. Template Pattern Defines the program skeleton of an algorithm in a method, called Template Method, which defers some steps to Sub Classes. It lets one redefine certain steps of an algorithm without changing the algorithm's structure. 11. Visitor Pattern Defines a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. 12. Null Object A Null Object is an object with defined neutral ("null") behavior. The Null Object Design Pattern describes the uses of such objects and their behavior (or lack thereof). Object references need to be checked to ensure they are not null before invoking any methods, because methods typically cannot be invoked on null references. Mudasir Qazi - mudasirqazi00@gmail.com 89-Dec-14
Advantages • They are reusable in multiple projects. • They provide the solutions that help to define the system architecture. • They capture the software engineering experiences. • They provide transparency to the design of an application. • They are well-proved and testified solutions since they have been built upon the knowledge and experience of expert software developers. • Design patterns don’t guarantee an absolute solution to a problem. They provide clarity to the system architecture and the possibility of building a better system. • Design patterns are programming language independent strategies for solving the common object-oriented design problems. That means, a design pattern represents an idea, not a particular implementation. Mudasir Qazi - mudasirqazi00@gmail.com 99-Dec-14
When to use? • We must use the design patterns during the analysis and requirement phase of SDLC(Software Development Life Cycle). • Design patterns ease the analysis and requirement phase of SDLC by providing information based on prior hands-on experiences. Mudasir Qazi - mudasirqazi00@gmail.com 109-Dec-14
How to Select? • Following points should be kept in mind when you choose a design pattern for your application. 1. Consider how design pattern solve the design problem. 2. Study how patterns interrelate. 3. Study patterns of like purpose. 4. Examine a cause of design. Mudasir Qazi - mudasirqazi00@gmail.com 119-Dec-14

Design Pattern - Introduction

  • 1.
    Design Patterns An Introduction MudasirQazi - mudasirqazi00@gmail.com 19-Dec-14
  • 2.
    Contents / Agenda •Definition • Categories • Creational Design Patterns • Structural Design Patterns • Behavioral Design Patterns • Advantages • When to use? • How to select a Design Patter? Mudasir Qazi - mudasirqazi00@gmail.com 29-Dec-14
  • 3.
    Definition 1. A designpattern is a well-proved solution for solving the specific problem/task. 2. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code. • Christopher Alexander was the first person who invented all the following Design Patterns in 1977. But later the Gang of Four - Design patterns, elements of reusable object-oriented software book was written by a group of four persons named as Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides in 1995. That's why all the following Design Patterns are known as Gang of Four (GoF) Design Patterns. Link of this Book : https://books.google.com.pk/books?id=K4qv1D- LKhoC&printsec=frontcover&source=gbs_ge_summary_r&cad=0#v=onepage &q&f=false Mudasir Qazi - mudasirqazi00@gmail.com 39-Dec-14
  • 4.
    Categories Following are themain categories of design patterns 1. Creational Design Patterns Creational design patterns are concerned with the way of creating objects. These design patterns are used when a decision must be made at the time of instantiation of a class (i.e. creating an object of a class). 2. Structural Design Patterns Structural design patterns are concerned with how classes and objects can be composed, to form larger structures. The structural design patterns simplifies the structure by identifying the relationships. These patterns focus on, how the classes inherit from each other and how they are composed from other classes. 3. Behavioral Design Patterns Behavioral design patterns are concerned with the interaction and responsibility of objects. In these design patterns, the interaction between the objects should be in such a way that they can easily talk to each other and still should be loosely coupled. That means the implementation and the client should be loosely coupled in order to avoid hard coding and dependencies. Mudasir Qazi - mudasirqazi00@gmail.com 49-Dec-14
  • 5.
    Creational Design Patterns •There are following 6 types of creational design patterns. 1. Factory Method Pattern / Factory Pattern / Virtual Constructor Define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. 2. Abstract Factory Pattern / Kit Define an interface or abstract class for creating families of related (or dependent) objects but without specifying their concrete sub-classes. 3. Singleton Pattern Define a class that has only one instance and provides a global point of access to it. 4. Prototyping Pattern Cloning of an existing object instead of creating new one and can also be customized as per the requirement. 5. Builder Pattern Construct a complex object from simple objects using step-by-step approach. 6. Object Pool Pattern To reuse the object that are expensive to create. Mudasir Qazi - mudasirqazi00@gmail.com 59-Dec-14
  • 6.
    Structural Design Patterns •There are following 7 types of structural design patterns. 1. Adapter Pattern Adapting an interface into other according to the client expectation. 2. Bridge Pattern Separating abstraction (interface) from implementation. 3. Composite Pattern Allowing client to operate on hierarchy of objects. 4. Decorator Pattern Adding functionality to an object dynamically. 5. Façade Pattern Providing an interface to a set of interfaces. 6. Flyweight Pattern Reusing an object by sharing it. 7. Proxy Pattern Representing another object. Mudasir Qazi - mudasirqazi00@gmail.com 69-Dec-14
  • 7.
    Behavioral Design Patterns(1) • There are 12 type of behavioral design patterns 1. Chain of Responsibility Pattern Avoid coupling the sender of a request to its receiver by giving multiple objects a chance to handle the request. 2. Command Pattern / Action / Transaction Encapsulate a request under an object as a command and pass it to invoker object. Invoker object looks for the appropriate object which can handle this command and pass the command to the corresponding object and that object executes the command. 3. Interpreter Pattern To define a representation of grammar of a given language, along with an interpreter that uses this representation to interpret sentences in the language. 4. Iterator Pattern / Cursor To access the elements of an aggregate object sequentially without exposing its underlying implementation. 5. Mediator Pattern Defines an object that encapsulates how a set of objects interact. 6. Memento Pattern The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback). Mudasir Qazi - mudasirqazi00@gmail.com 79-Dec-14
  • 8.
    Behavioral Design Patterns(2) • Behavioral design patterns continues to 7. Observer Pattern In Observer Pattern an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. 8. State Pattern Used to encapsulate varying behavior for the same routine based on an object's state object 9. Strategy Pattern / Policy Pattern Enables an algorithm’s behavior to be selected at runtime. 10. Template Pattern Defines the program skeleton of an algorithm in a method, called Template Method, which defers some steps to Sub Classes. It lets one redefine certain steps of an algorithm without changing the algorithm's structure. 11. Visitor Pattern Defines a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. 12. Null Object A Null Object is an object with defined neutral ("null") behavior. The Null Object Design Pattern describes the uses of such objects and their behavior (or lack thereof). Object references need to be checked to ensure they are not null before invoking any methods, because methods typically cannot be invoked on null references. Mudasir Qazi - mudasirqazi00@gmail.com 89-Dec-14
  • 9.
    Advantages • They arereusable in multiple projects. • They provide the solutions that help to define the system architecture. • They capture the software engineering experiences. • They provide transparency to the design of an application. • They are well-proved and testified solutions since they have been built upon the knowledge and experience of expert software developers. • Design patterns don’t guarantee an absolute solution to a problem. They provide clarity to the system architecture and the possibility of building a better system. • Design patterns are programming language independent strategies for solving the common object-oriented design problems. That means, a design pattern represents an idea, not a particular implementation. Mudasir Qazi - mudasirqazi00@gmail.com 99-Dec-14
  • 10.
    When to use? •We must use the design patterns during the analysis and requirement phase of SDLC(Software Development Life Cycle). • Design patterns ease the analysis and requirement phase of SDLC by providing information based on prior hands-on experiences. Mudasir Qazi - mudasirqazi00@gmail.com 109-Dec-14
  • 11.
    How to Select? •Following points should be kept in mind when you choose a design pattern for your application. 1. Consider how design pattern solve the design problem. 2. Study how patterns interrelate. 3. Study patterns of like purpose. 4. Examine a cause of design. Mudasir Qazi - mudasirqazi00@gmail.com 119-Dec-14