1 / 15

Design Patterns: MORE Examples

Design Patterns: MORE Examples. Based on Design Patterns: Elements of reusable Object-Oriented software by gamma, helm, johnson , and Vlissides , Addison- wesley , 1995. . Recall What a design pattern is.

lave
Download Presentation

Design Patterns: MORE Examples

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: MORE Examples Based on Design Patterns: Elements of reusable Object-Oriented software by gamma, helm, johnson, and Vlissides, Addison-wesley, 1995.

  2. Recall What a design pattern is “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” – Christopher Alexander, et al. A Pattern Language, Oxford Univ. Press, New York, 1977.

  3. What is a design pattern? An OO design pattern names, explains, and evaluates an important, recurring design in OO systems.

  4. Common Causes of Redesign Creating an object by specifying a class explicitly. Dependence on specific operations. Dependence on hardware and software platform. Dependence on object representations or implementations. Algorithmic dependencies. Tight coupling. Extending functionality by subclassing. Inability to alter classes conveniently.

  5. Frameworks and Patterns • Framework: • A set of cooperating classes that constitute a reusable design for a particular domain. • Example: A framework for financial modeling applications. • A basis for an application’s architecture. • Differs from design patterns in three ways: • Less abstract than design patterns. • More inclusive than design patterns. • A framework often contains several design patterns, but the converse is not true. • More specialized than design patterns.

  6. Creational Design Patterns • Creational design patterns • Abstract the instantiation process. • Help make a system independent of how its objects are created, composed, and represented. • Have two recurring themes: • They encapsulate knowledge about concrete classes the system uses. • They hide how instances of these classes are created and put together.

  7. An example design pattern: Factory Method • Intent • Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. • Motivation • Frameworks use abstract classes to define and maintain relationships between objects. Frameworks are often responsible for creating these objects as well. However, frameworks only know about abstract classes, which can’t be instantiated. • The Factory Method pattern encapsulates the knowledge of which subclass to create and moves this knowledge out of the framework.

  8. Factory Method design pattern (cont’d) • Applicability: Use the Factory Method pattern when • A class can’t predict the class of objects it must create. • A class wants its subclasses to specify the objects it creates. • Classes delegate responsibility to a selected helper subclass, and it’s desirable to localize the knowledge of which helper subclass is the delegate. • Structure (see diagram). • Some Consequences • Elimination of the need to bind application-specific classes into code. • Client may have to subclass the Creator class in order to create a ConcreteProduct object.

  9. Structural Design Patterns • Structural design patterns are concerned with composing classes and objects to form larger structures. • Structural class patterns • use inheritance to compose interfaces or implementations. • Structural object patterns • describe ways to compose objects to realize new functionality.

  10. Decorator- An example object structural pattern • Intent • Attach additional responsibilities to an object dynamically. This pattern provides an alternative to subclassing. • Motivation • Allows us to add responsibilities to an individual object rather than to an entire class, and it avoids the inflexibility of inheritance. • Applicability • When you want to add responsibilities to individual objects dynamically and transparently without affecting other objects. • When you want to add responsibilities that can be withdrawn. • When extension by subclassing is impractical.

  11. Decorator- An example structural pattern (cont’d) • Consequences • Benefits • More flexibility than static inheritance. • Avoids feature-laden classes high in the hierarchy. • Liabilities • A decorator and its component aren’t identical. • Lots of little objects.

  12. Behavioral design patterns Concerned with algorithms and the assignment of responsibilities between objects Describes the patterns of communication between objects. Characterizes complex control flow. Behavioral class patterns use inheritance to distribute behavior between classes. Behavioral object patterns use composition.

  13. Template Method-an example class behavioral design pattern • Intent • Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Allows subclasses to redefine certain steps of an algorithm without changing the algorithm’s structure. • Motivation • Allows the order of some algorithmic steps to be fixed, while allowing subclasses to vary those steps as needed. • Applicability • To implement the unchanging parts of an algorithm once and let subclasses implement behavior that is variant. • To factor common behavior among subclasses and localize it in a common class. • To control subclasses extensions.

  14. Template Method (cont’d)-an example object behavioral design pattern • Consequences • Provides a fundamental technique for code reuse. • Calls multiple kinds of operations: • Concrete operations. • Concrete AbstractClass operations. • Primitive operations. • Factory methods. • Hook operations (default behavior that may be extended if necessary).

  15. Summary • Design patterns are used to attack recurring problems that can cause redesign: • Creating an object by specifying a class explicitly. [Factory Method] • Dependence on specific operations. • Dependence on hardware and software platform. • Dependence on object representations or implementations. • Algorithmic dependencies. [Strategy, Template Method] • Tight coupling. • Extending functionality by subclassing. [Decorator, Strategy] • Inability to alter classes conveniently. [Adapter, Decorator]

More Related