Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Design
Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented by Ian Davis Winter 2016 © Spiros Mancoridis
2
Terminology and Motivation
Winter 2016 © Spiros Mancoridis
3
Design Patterns Good designers know not to solve every problem from first principles. They reuse solutions. Practitioners do not do a good job of recording experience in software design for others to use. Winter 2016 © Spiros Mancoridis
4
Design Patterns (Cont’d)
A Design Pattern systematically names, explains, and evaluates an important and recurring design. We describe a set of well-engineered design patterns that practitioners can apply when crafting their applications. Winter 2016 © Spiros Mancoridis
5
Becoming a Master Designer
First, One Must Learn the Rules: Algorithms Data Structures Languages Later, One Must Learn the Principles: Structured Programming Modular Programming OO Programming Winter 2016 © Spiros Mancoridis
6
Becoming a Master Designer (Cont’d)
Finally, One Must Study the Designs of Other Masters: Design patterns must be understood, memorized, and applied. There are thousands of existing design patterns. Winter 2016 © Spiros Mancoridis
7
Reusable OO Design Patterns
Winter 2016 © Spiros Mancoridis
8
OOD Patterns Topics Terminology and Motivation
Reusable OO Design Patterns: Creational Patterns Structural Patterns Behaviour Patterns Winter 2016 © Spiros Mancoridis
9
Creational patterns Winter 2016 © Spiros Mancoridis
10
Factory Pattern (Intent)
Introduces a layer of software which allows run time creation of objects, identified by parametric value rather than by compile time name. May use polymorphism to create different objects in distinct subclasses sharing common factory method. Winter 2016 © Spiros Mancoridis
11
Factory Pattern (Motivation)
May wish to restrict conditions under which objects returned by factory may be created. The classes employed within software may not be known until run-time. eg. OLE pictures etc. may be inserted into documents. May wish to identify classes by GUID’s, and register them. Winter 2016 © Spiros Mancoridis
12
The Abstract Factory Pattern (Intent)
Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Allows entire suites of classes to optionally be interchanged easily at run time. Winter 2016 © Spiros Mancoridis
13
The Abstract Factory Pattern (Behavior)
Sometimes we have systems that support different representations depending on external factors. There is an Abstract Factory that provides an interface for the client. In this way the client can obtain a specific object through this abstract interface. Winter 2016 © Spiros Mancoridis
14
Example of the Abstract Factory Pattern
Winter 2016 © Spiros Mancoridis
15
Structure of the Abstract Factory Pattern
Winter 2016 © Spiros Mancoridis
16
Participants of the Abstract Factory Pattern
declares an interface for operations that create abstract product objects. Concrete Factory: implements the operations to create concrete product objects. Winter 2016 © Spiros Mancoridis
17
Participants of the Abstract Factory Pattern (Cont’d)
Abstract Product: Declares an interface for a type of product object. Concrete Product: Defines a product object to be declared by the corresponding concrete factory. (Implements the Abstract Product interface). Client: Uses only interfaces declared by Abstract Factory and Abstract Product classes. Winter 2016 © Spiros Mancoridis
18
Builder pattern Methods of a builder class provide an interface which allows the builder class to receive in a pre-determined manner the material from which something is to be built, produced or used. The builder also provides a method to return the result of construction if appropriate. Winter 2016 © Spiros Mancoridis
19
Builder pattern motivation
Want to divorce the issue of what is used to build a result from how the result is built. May want to build many different things from the same input. Easy to create and introduce into system new builder classes, if they all have the same external behaviour. Essentially builder pattern reinforces the importance of polymorphism. Winter 2016 © Spiros Mancoridis
20
Builder pattern example
SGML parser identifies types of data token traversed within document. Want to tell application what has been seen while traversing document. Don’t want to tell application what to do with this data. Application may format this data, build electronic indices from it, etc. May want to view document in many concurrent windows and styles (eg. Lector) Winter 2016 © Spiros Mancoridis
21
Prototype pattern Don’t want to explicitly remember the knowledge of exactly what class, and with what parameters a class should be created when requested. Instead encapsulate this information by holding a dummy instance of the class to be created following a given request. Clone copies of this dummy instance as needed. Winter 2016 © Spiros Mancoridis
22
Prototype pattern (example)
Visio has vast numbers of visual symbols stored in template sheets. The symbols can be identified by GUID. The GUID allows the class supporting the representation of that symbol to be loaded. Once loaded the visual symbol can be cloned multiple times. It may appear multiple times in a drawing. Winter 2016 © Spiros Mancoridis
23
Singleton pattern Provides global access to single instances of a class transparently. Centralizes access to this single instance. Embeds appropriate concurrency control within the singleton pattern. Avoids potential misuse of global objects. Winter 2016 © Spiros Mancoridis
24
Singleton pattern example
Threads may wish to emit error messages. How error messages are handled is not the threads problem. May want to direct error messages to a single shared error message area. Need to avoid conflict when multiple threads concurrently report errors. Easy to change so that each thread has their own error message area if desired. Winter 2016 © Spiros Mancoridis
25
Structural patterns Winter 2016 © Spiros Mancoridis
26
The Adapter Pattern Intent: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. Motivation: When we want to reuse classes in an application that expects classes with a different interface, we do not want (and often cannot) to change the reusable classes to suit our application. Winter 2016 © Spiros Mancoridis
27
Example of the Adapter Pattern
Winter 2016 © Spiros Mancoridis
28
Structure of the Adapter Pattern Using Multiple Inheritance
Winter 2016 © Spiros Mancoridis
29
Structure of the Adapter Pattern Using Object Composition
Winter 2016 © Spiros Mancoridis
30
Participants of the Adapter Pattern
Target: Defines the application-specific interface that clients use. Client: Collaborates with objects conforming to the target interface. Adaptee: Defines an existing interface that needs adapting. Adapter: Adapts the interface of the adaptee to the target interface. Winter 2016 © Spiros Mancoridis
31
The Facade Pattern (Intent)
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. Winter 2016 © Spiros Mancoridis
32
The Facade Pattern (Motivation)
Structuring a system into subsystems helps reduce complexity. A common design goal is to minimize the communication and dependencies between subsystems. Use a facade object to provide a single, simplified interface to the more general facilities of a subsystem. Winter 2016 © Spiros Mancoridis
33
Example of the Facade Pattern
Winter 2016 © Spiros Mancoridis
34
Structure of the Facade Pattern
Winter 2016 © Spiros Mancoridis
35
Participants of the Facade Pattern
Knows which subsystem classes are responsible for a request. Delegates client requests to appropriate subsystem objects. Subsystem Classes: Implement subsystem functionality. Handle work assigned by the facade object. Have no knowledge of the facade; that is, they keep no references to it. Winter 2016 © Spiros Mancoridis
36
The Composite Pattern (Intent)
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Winter 2016 © Spiros Mancoridis
37
The Composite Pattern (Motivation)
If the composite pattern is not used, client code must treat primitive and container classes differently, making the application more complex than is necessary. Winter 2016 © Spiros Mancoridis
38
Example of the Composite Pattern
Winter 2016 © Spiros Mancoridis
39
Structure of the Composite Pattern
Winter 2016 © Spiros Mancoridis
40
Participants of Composite Pattern
Component: Declares the interface for objects in the composition. Implements default behavior for the interface common to all classes. Declares an interface for accessing and managing its child components. Defines an interface for accessing a component's parent in the recursive structure (optional). Winter 2016 © Spiros Mancoridis
41
Participants of Composite Pattern (Cont’d)
Leaf: Represents leaf objects in the composition. A leaf has no children. Defines behavior for primitive objects in the composition. Composite: Defines behavior for components having children. Stores child components. Implements child-related operations in the component interface. Winter 2016 © Spiros Mancoridis
42
Participants of Composite Pattern (Cont’d)
Client: Manipulates objects in the composition through the component interface. Winter 2016 © Spiros Mancoridis
43
Behavioral Patterns Winter 2016 © Spiros Mancoridis
44
The Command Pattern (Intent)
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log request, and support undoable operations. Winter 2016 © Spiros Mancoridis
45
The Command Pattern (Motivation)
Sometimes it's necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request. Winter 2016 © Spiros Mancoridis
46
Example of the Command Pattern
Winter 2016 © Spiros Mancoridis
47
Structure of the Command Pattern
Winter 2016 © Spiros Mancoridis
48
Structure of the Command Pattern
Winter 2016 © Spiros Mancoridis
49
Participants of the Command Pattern
Declares an interface for executing an operation. Concrete Command: Defines a binding between a Receiver object and an action. Implements Execute by invoking the corresponding operations(s) on Receiver. Winter 2016 © Spiros Mancoridis
50
Participants of the Command Pattern (Cont’d)
Client: Creates a Concrete Command object and sets its receiver. Invoker: Asks the command to carry out the request. Receiver: Knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver. Winter 2016 © Spiros Mancoridis
51
Interpreter pattern Want to compute something capable of being expressed in a language. The computation is performed on structural representation of the language operations to be performed. Structural objects representing operations have built in knowledge of how to perform their own operation. Winter 2016 © Spiros Mancoridis
52
Interpreter pattern example
A language statement may be converted to an expression tree of operations to be performed. Each operation takes as its input the results returned by it’s child expression trees. Such an expression tree is often called a plan. Winter 2016 © Spiros Mancoridis
53
The Iterator Pattern (Intent)
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Move the responsibility for access and traversal from the aggregate object to the iterator object. Potentially preserves state information which speeds traversal. Winter 2016 © Spiros Mancoridis
54
The Iterator Pattern (Motivation)
One might want to traverse an aggregate object in different ways. One might want to have more than one traversal pending on the same aggregate object. Not all types of traversals can be anticipated a priori. One should not bloat the interface of the aggregate object with all these traversals. Winter 2016 © Spiros Mancoridis
55
Example of the Iterator Pattern
Winter 2016 © Spiros Mancoridis
56
Structure of the Iterator Pattern
Winter 2016 © Spiros Mancoridis
57
Participants of the Iterator Pattern
Iterator: Defines an interface for accessing and traversing elements. Concrete Iterator: Implements an iterator interface and keeps track of the current position in the traversal of the aggregate. Aggregate: Defines an interface for creating an iterator object. Concrete Aggregate: Implements the iterator creation interface to return an instance of the proper concrete iterator. Winter 2016 © Spiros Mancoridis
58
The Template Pattern (Intent)
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. The Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. Winter 2016 © Spiros Mancoridis
59
The Template Pattern (Motivation)
By defining some of the steps of an algorithm, using abstract operations, the template method fixes their ordering. Winter 2016 © Spiros Mancoridis
60
Structure of the Template Pattern
Winter 2016 © Spiros Mancoridis
61
Structure of the Template Pattern
Abstract Class: Defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm. Implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in Abstract Class or those of other objects. Winter 2016 © Spiros Mancoridis
62
Structure of the Template Pattern (Cont’d)
Concrete Class: Implements the primitive operations to carry out subclass-specific steps to the algorithm. Winter 2016 © Spiros Mancoridis
63
The Observer Pattern (Intent)
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Winter 2016 © Spiros Mancoridis
64
The Observer Pattern (Motivation)
A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. You don't want to achieve consistency by making the classes tightly coupled, because that reduces their reusability. Winter 2016 © Spiros Mancoridis
65
Example of the Observer Pattern
Winter 2016 © Spiros Mancoridis
66
Structure of the Observer Pattern
Winter 2016 © Spiros Mancoridis
67
Structure of the Observer Pattern
The key objects in this pattern are subject and observer. A subject may have any number of dependent observers. All observers are notified whenever the subject undergoes a change in state. Winter 2016 © Spiros Mancoridis
68
Participants of the Observer Pattern
Subject: Knows its numerous observers. Provides an interface for attaching and detaching observer objects. Sends a notification to its observers when its state changes. Observer: Defines an updating interface for concrete observers. Winter 2016 © Spiros Mancoridis
69
Participants of the Observer Pattern (Cont’d)
Concrete Subject: Stores state of interest to concrete observers. Concrete Observer: Maintains a reference to a concrete subject object. Stores state that should stay consistent with the subject's. Implements the updating interface. Winter 2016 © Spiros Mancoridis
70
The Master-Slave Pattern (Intent)
Handles the computation of replicated services within a software system to achieve fault tolerance and robustness. Independent components providing the same service (slaves) are separated from a component (master) responsible for invoking them and for selecting a particular result from the results returned by the slaves. Winter 2016 © Spiros Mancoridis
71
The Master-Slave Pattern (Motivation)
Fault tolerance is a critical factor in many systems. Replication of services and delegation of the same task to several independent suppliers is a common strategy to handle such cases. Winter 2016 © Spiros Mancoridis
72
Example of the M/S Pattern
Winter 2016 © Spiros Mancoridis
73
Structure of the M/S Pattern
Winter 2016 © Spiros Mancoridis
74
Participants of the M/S Pattern
Slave: Implements a service. Master: Organizes the invocation of replicated services. Decides which of the results returned by its slaves is to be passed to its clients. Client: Requires a certain service in order to solve its own task. Winter 2016 © Spiros Mancoridis
75
Smart operator pattern
Need to extend behaviour of standard operations, to handle reference counting, etc. Embed extended behaviour inside a revised definition of the underlying operation. Ensures that operators enforce desired behaviour.. Handles cross-cutting concerns cleanly Winter 2016 © Spiros Mancoridis
76
Smart operator example
class Cdstring : public Cdata { private: Cstring *m_cstringP; void bind(Cstring *cstringP) { if (cstringP) cstringP->AddRef(); if (m_cstringP) m_cstringP->Release(); m_cstringP = cstringP; } public: Cdstring& operator=(const Cdstring& cdata) // Assignment operator m_got = cdata.m_got; bind(cdata.m_cstringP); return *this; Winter 2016 © Spiros Mancoridis
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.