1 / 32

Design Patterns

Design Patterns. Design Patterns. Design patterns are solutions to problems that arise when developing software within a particular context Patterns capture the structure and collaboration among key participants in software designs

maire
Download Presentation

Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns

  2. Design Patterns • Design patterns are solutions to problems that arise when developing software within a particular context • Patterns capture the structure and collaboration among key participants in software designs • Patterns facilitate reuse of successful software architectures and designs

  3. Design Pattern Descriptions • Name • Problem • What is the problem and the context where we would use this pattern? • Under what specific conditions should this pattern be used? • Solution • A description of the elements that make up the design pattern • Emphasizes their relationships, responsibilities, and collaborations • Not a concrete design or implementation; rather an abstract description • Positive and negative consequences of use • The pros and cons of using the pattern • Includes impacts on reusability, portability, and extensibility

  4. Organizing Patterns • Purpose: What a pattern does • Creational: creating, initializing and configuring classes and objects • Structural patterns: composition of classes and objects • Behavioral patterns: dynamic interactions among classes and objects • Scope: what the pattern applies to • Class patterns: • Focus on the relationships between classes and their subclasses • Involve inheritance reuse • Object patterns: • Focus on relationships between objects • Involve composition reuse

  5. Patterns and Language • Most design patterns focus on OO • Assume inheritance, polymorphism, encapsulation, etc. • In procedural languages, might add OO features as “patterns” • Some languages provide or make it easier to implement some patterns

  6. Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes

  7. Abstract Factory

  8. Abstract Factory • Use the Abstract Factory pattern when • a system should be independent of how its products are created, composed, and represented. • a system should be configured with one of multiple families of products. • a family of related product objects is designed to be used together, and you need to enforce this constraint. • you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

  9. Abstract Factory • AbstractFactory (WidgetFactory) • declares an interface for operations that create abstract product objects. • ConcreteFactory (MacintoshWidgetFactory, LinuxWidgetFactory) • implements the operations to create concrete product objects. • AbstractProduct (Window, ScrollBar) • declares an interface for a type of product object. • ConcreteProduct (MacintoshWindow, MacintoshScrollBar) • defines a product object to be created by the corresponding concrete factory. • implements the AbstractProduct interface. • Client • uses only interfaces declared by AbstractFactory and AbstractProduct classes.

  10. Abstract Factory • Collaborations • Normally a single instance of a ConcreteFactory class is created at run-time. This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory. • AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

  11. Abstract Factory • Consequences • It isolates concrete classes • It makes exchanging product families easy • It promotes consistency among products • Supporting new kinds of products is difficult

  12. Factory Method • Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

  13. Factory Method

  14. Factory Method • Use the Factory Method pattern when • a class can't anticipate the class of objects it must create. • a class wants its subclasses to specify the objects it creates.

  15. Factory Method • Participants • Product (Document) • defines the interface of objects the factory method creates. • ConcreteProduct (MyDocument) • implements the Product interface. • Creator (Application) • declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. • may call the factory method to create a Product object. • ConcreteCreator (MyApplication) • overrides the factory method to return an instance of a ConcreteProduct.

  16. Singleton Ensure a class only has one instance, and provide a global point of access to it.

  17. Singleton • Use the Singleton pattern when • there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point. • when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

  18. Singleton • Consequences • Controlled access to sole instance • Permits refinement of operations • Permits a variable number of instances • More flexible than class operations

  19. Adapter • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

  20. Adapter

  21. Adapter

  22. Adapter

  23. Decorator • Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

  24. Decorator

  25. Decorator • Use Decorator • to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects. • for responsibilities that can be withdrawn. • when extension by subclassing is impractical.

  26. Decorator • Participants • Component (VisualComponent) • defines the interface for objects that can have responsibilities added to them dynamically. • ConcreteComponent (TextView) • defines an object to which additional responsibilities can be attached. • Decorator • maintains a reference to a Component object and defines an interface that conforms to Component's interface. • ConcreteDecorator (BorderDecorator, ScrollDecorator) • adds responsibilities to the component.

  27. Decorator • Consequences • More flexibility than static inheritance • Avoids feature-laden classes high up in the hierarchy • A decorator and its component aren't identical • Lots of little objects

  28. Facade • Toolkit vs. application • A facade is a class with a level of functionality that lies between a toolkit and a complete application • The intent of the facade pattern is to provide an interface that makes a subsystem easy to use

  29. Observer

  30. Observer • Use the Observer pattern in any of the following situations: • When a change to one object requires changing others, and you don't know how many objects need to be changed. • When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.

  31. Observer • Participants • Subject • knows its observers. Any number of Observer objects may observe a subject. • provides an interface for attaching and detaching Observer objects. • Observer • defines an updating interface for objects that should be notified of changes in a subject. • ConcreteSubject • stores state of interest to ConcreteObserver objects. • sends a notification to its observers when its state changes. • ConcreteObserver • maintains a reference to a ConcreteSubject object. • stores state that should stay consistent with the subject's. • implements the Observer updating interface to keep its state consistent with the subject's.

More Related