1 / 64

Chapter 8 Object Design: Reuse and Patterns 2

This chapter explores the concepts of inheritance and delegation in object design patterns, comparing implementation inheritance with interface inheritance. It also introduces the use of the Composite design pattern for modeling software systems.

taylorjohn
Download Presentation

Chapter 8 Object Design: Reuse and Patterns 2

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. Chapter 8 Object Design: Reuse and Patterns 2

  2. Recall: Two Important Concepts in Design Patterns • Inheritance vs. delegation • Implementation inheritance vs. interface inheritance

  3. List Stack +Add() List +Remove() Remove() +Push() +Pop() +Top() Add() Stack +Push() +Pop() +Top() Delegation vs. Implementation Inheritance • Inheritance: Extending a Base class by a new operation or overwriting an operation. • Delegation: Catching an operation and sending it to another object. An example in the handout

  4. Comparison: Delegation vs Implementation Inheritance • Delegation • Pro: • Flexibility: Any object can be replaced at run time by another one (as long as it has the same type) • Con: • Inefficiency: Objects are encapsulated. • Inheritance • Pro: • Straightforward to use • Supported by many programming languages • Easy to implement new functionality • Con: • Inheritance exposes a subclass to the details of its parent class • Any change in the parent class implementation forces the subclass to change (which requires recompilation of both)

  5. Implementation Inheritance vs Interface Inheritance • Implementation inheritance • Also called class inheritance • Goal: Extend an applications’ functionality by reusing functionality in parent class • Inherit from an existing class with some or all operations already implemented • Interface inheritance • Also called subtyping • Inherit from an abstract class with all operations specified, but not yet implemented

  6. Interface inheritance • All subclasses respond to the requests in the interface of this abstract class, making them all subtype of the abstract class • Two benefit: • Clients remain unaware of the specific types of objects they use. • Clients remain unaware of the class that implement these objects. Clients only know about the abstract class defining the interface. • Reduce implementation dependencies between subsystems • Software Reuse -- Program to an interface, not an implementation.

  7. Some rules Program to an interface, not an implementation. Favor object composition over class inheritance Interface inheritance + delegation  reuse design pattern

  8. Design Patterns • What are Design Patterns? • A design pattern describes a problem which occurs over and over again in our environment • Then it describes the core of the solution to that problem, in such a way that you can use the solution a million times over, without ever doing it the same twice

  9. Towards a Pattern Taxonomy • Structural Patterns • Adapters, Bridges, Facades, and Proxies are variations on a single theme: • They reduce the coupling between two or more classes • They introduce an abstract class to enable future extensions • They encapsulate complex structures • Behavioral Patterns • Here we are concerned with algorithms and the assignment of responsibilies between objects: Who does what? • Behavorial patterns allow us to characterize complex control flows that are difficult to follow at runtime. • Creational Patterns • Here we our goal is to provide a simple abstraction for a complex instantiation process. • We want to make the system independent from the way its objects are created, composed and represented.

  10. Pattern Creational Pattern Structural Pattern Behavioral Pattern Abstract Factory Builder Pattern Command Observer Strategy Adapter Bridge Facade Proxy A Pattern Taxonomy Composite

  11. Design patterns

  12. The Composite Design pattern

  13. Definition of Software System A software system consists of subsystems which are either other subsystems or collection of classes Definition of Software Lifecycle: The software lifecycle consists of a set of development activities which are either other activities or collection of tasks What is common between these definitions?

  14. Introducing the Composite Pattern • Models tree structures that represent part-whole hierarchies with arbitrary depth and width. • The Composite Pattern lets client treat individual objects and compositions of these objects uniformly Component Client Leaf Operation() Composite Operation() AddComponent RemoveComponent() GetChild() Children

  15. What is common between these definitions? • Software System: • Definition: A software system consists of subsystems which are either other subsystems or collection of classes • Composite: Subsystem (A software system consists of subsystems which consists of subsystems , which consists of subsystems, which...) • Leaf node: Class • Software Lifecycle: • Definition: The software lifecycle consists of a set of development activities which are either other actitivies or collection of tasks • Composite: Activity (The software lifecycle consists of activities which consist of activities, which consist of activities, which....) • Leaf node: Task

  16. Modeling a Software System with a Composite Pattern Software System * User Class Subsystem Children

  17. Modeling the Software Lifecycle with a Composite Pattern Software Lifecycle * Manager Task Activity Children

  18. Battery Engine School Composite Pattern Dynamic tree (recursive aggregate): The Composite Patterns models dynamic aggregates Fixed Structure: Car * * Doors Wheels Organization Chart (variable aggregate): * * Department University Dynamic tree (recursive aggregate): Program * * Block Simple Compound Statement Statement

  19. Graphic Client Circle Draw() Picture Draw() Add(Graphic g) RemoveGraphic) GetChild(int) Line Draw() Children Graphic Applications also use Composite Patterns • The Graphic Class represents both primitives (Line, Circle) and their containers (Picture)

  20. Design Patterns reduce the Complexity of Models • To communicate a complex model we use navigation and reduction of complexity • We do not simply use a picture from the CASE tool and dump it in front of the user • The key is navigate through the model so the user can follow it. • We start with a very simple model and then decorate it incrementally • Start with key abstractions (use animation) • Then decorate the model with the additional classes • To reduce the complexity of the model even further, we • Apply the use of inheritance (for taxonomies, and for design patterns) • If the model is still too complex, we show the subclasses on a separate slide • Then identify (or introduced) patterns in the model • We make sure to use the name of the patterns

  21. Taxonomies Basic Abstractions Composite Patterns Example: A More Complex Model of a Software Project

  22. * Work Product Exercise • Redraw the complete model for Project from your memory using the following knowledge • The key abstractions are workproduct, task, schedule, and participant • Workproduct, Task and Participant are modeled with composite patterns, for example • There are taxonomies for each of the key abstractions You have 5 minutes!

  23. Graphics Component * getGraphics() Button Text Component Label Container add(Component c) paint(Graphics g) TextField TextArea Java‘s AWT library can be modeled with the composite pattern

  24. The Adapter Design pattern

  25. TextView getExtent() Shape BoundingBox() Createmanipulator() DrawingEditor text Line BoundingBox() Createmanipulator() PolygonShape BoundingBox() Createmanipulator() TextShape BoundingBox() Createmanipulator() Return text.getExtent() Return new TextManipulator Problem 1 Another way?

  26. TextView getExtent() Shape BoundingBox() Createmanipulator() DrawingEditor Line BoundingBox() Createmanipulator() PolygonShape BoundingBox() Createmanipulator() TextShape BoundingBox() Createmanipulator() Return getExtent() Return new TextManipulator Problem 1

  27. Adapter Pattern • “Convert the interface of a class into another interface clients expect.” • The adapter pattern lets classes work together that couldn’t otherwise because of incompatible interfaces • Used to provide a new interface to existing legacy components (Interface engineering, reengineering). • Also known as a wrapper

  28. Target Request() Client Adaptee ExistingRequest() adaptee Adapter Request() Adapter Pattern • Delegation is used tobind an Adapter and an Adaptee • Interface inheritance is use to specify the interface of the Adapter class. • Target and Adaptee (usually called legacy system) pre-exist the Adapter. • Target may be realized as an interface in Java.

  29. Adapter pattern • An example on textbook P320 • Question: Why does Array need to interact with Comparator, which is an abstract class?

  30. The Facade Design pattern

  31. Subsystem 1 can look into the Subsystem 2 (vehicle subsystem) and call on any component or class operation at will. Why is this good? Efficiency Why is this bad? Can’t expect the caller to understand how the subsystem works or the complex relationships within the subsystem. We can be assured that the subsystem will be misused, leading to non-portable code Design Example Subsystem 1 Subsystem 2 Seat Card AIM SA/RT A better design?

  32. The subsystem decides exactly how it is accessed. No need to worry about misuse by callers If a façade is used the subsystem can be used in an early integration test We need to write only a driver Realizing an Opaque Architecture with a Facade VIP Subsystem Vehicle Subsystem API Card Seat AIM SA/RT

  33. Facade Pattern • Provides a unified interface to a set of objects in a subsystem. • A facade defines a higher-level interface that makes the subsystem easier to use (i.e. it abstracts out the gory details) • Facades allow us to provide a closed architecture

  34. Subsystem Design using Façade, Adapter • The ideal structure of a subsystem consists of • an interface object • a set of application domain objects (entity objects) modeling real entities or existing systems • Some of the application domain objects are interfaces to existing systems • one or more control objects • We can use design patterns to realize this subsystem structure • Realization of the Interface Object: Facade • Provides the interface to the subsystem • Interface to existing systems: Adapter • Provides the interface to existing system (legacy system) • The existing system is not necessarily object-oriented!

  35. Question: Compare the Adapter design pattern and the Façade design pattern.

  36. The Strategy Design pattern

  37. Problem • Many different algorithms exists for the same task • Examples: • Breaking a stream of text into lines • Parsing a set of tokens into an abstract syntax tree • Sorting a list of customers • The different algorithms will be appropriate at different times • Rapid prototyping vs delivery of final product • We don’t want to support all the algorithms if we don’t need them • If we need a new algorithm, we want to add it easily without disturbing the application using the algorithm

  38. Composition&Compositor A big and hard-to-maintain client Composition Compositor Linebreaking algorithms Maintain and update linebreaks ? Problem • Maintain and update the linebreaks of text displayed in a test viewer using a linebreaking algorithm.

  39. Strategy pattern • Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. • Also known as Policy

  40. Applicability of Strategy Pattern • Many related classes differ only in their behavior. Strategy allows to configure a single class with one of many behaviors • Different variants of an algorithm are needed that trade-off space against time. All these variants can be implemented as a class hierarchy of algorithms

  41. Strategy AlgorithmInterface Structure Policy Context ContextInterface() * ConcreteStrategyC AlgorithmInterface() ConcreteStrategyB AlgorithmInterface() ConcreteStrategyA AlgorithmInterface() Policy decides which Strategy is best given the current Context

  42. Strategy Sort() MergeSort Sort() QuickSort Sort() BubbleSort Sort() Applying a Strategy Pattern in a Database Application Database Search() Sort() * Strategy

  43. Consequences • Families of related algorithms • Separating strategies out makes it easier to subclassing Context • Clients must be aware of different Strategies • Communication overhead between strategy and context

  44. Sample code P. 323

  45. The Proxy Design pattern

  46. Proxy Pattern: Motivation • It is 15:00pm. I am sitting at my 14.4 baud modem connection and retrieve a fancy web site from the US, This is prime web time all over the US. So I am getting 10 bits/sec. • What can I do?

  47. Proxy Pattern • What is expensive? • Object Creation • Object Initialization • Defer object creation and object initialization to the time you need the object • Proxy pattern: • Reduces the cost of accessing objects • Uses another object (“the proxy”) that acts as a stand-in for the real object • The proxy creates the real object only if the user asks for it

  48. Before

  49. Controlling Access

  50. After

More Related