Professional Documents
Culture Documents
Design Patterns Java 3
Design Patterns Java 3
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Design Patterns
• Material for this lecture is taken from
Design Patterns: Elements of Reusable Object-
Oriented Software
• by Gamma, Helm, Johnson, Vlissides
• ISBN: 0321700694, 9780321700698
Head First Design Patterns
• By Freeman, Bates, Sierra, Robson
• ISBN: 978-0596007126, 0596007124
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Structural Patterns
• Structural patterns are concerned with how classes and
objects are composed to form larger structures. Structural
class patterns use inheritance to compose interfaces or
implementations
Adapter
Bridge
Iterator
Composite
Decorator
Façade
Flyweight
Proxy
Model View Controller
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
• 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
• Applicability
you want to use an existing class, and its interface does not match the
one you need
you want to create a reusable class that cooperates with unrelated or
unforeseen classes, that is, classes that don't necessarily have
compatible interfaces
you need to use several existing subclasses, but it's impractical
to adapt their interface by sub-classing every one. An object
adapter can adapt the interface of its parent class
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
• Structure
Inheritance
Composition
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
• Consequences
Class adapter
• adapts Adaptee to Target by committing to a concrete Adapter class. A class
adapter won't work when we want to adapt a class and all its subclasses
• introduces only one object, and no additional pointer indirection is needed to get
to the adaptee
Object adapter
• lets a single Adapter work with many Adaptees—that is, the Adaptee itself and all
of its subclasses (if any). The Adapter can also add functionality to all Adaptees at
once
• makes it harder to override Adaptee behavior. It will require subclassing Adaptee
and making Adapter refer to the subclass rather than the Adaptee itself.
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Adapter
• Related Patterns
Bridge has a structure similar to an object adapter, but Bridge has a
different intent: It is meant to separate an interface from its
implementation so that they can be varied easily and independently.
An adapter is meant to change the interface of an existing object
Decorator enhances another object without changing its interface. A
decorator is thus more transparent to the application than an adapter
is. As a consequence, Decorator supports recursive composition,
which isn't possible with pure adapters
Proxy defines a representative or surrogate for another object and
does not change its interface
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
• Intent
Decouple the abstraction from its implementation so that the two can
vary independently
• Motivation
When an abstraction can have one of several possible
implementations, the usual way to accommodate them is to use
inheritance.
An abstract class defines the interface to the abstraction, and concrete
subclasses implement it in different ways.
Inheritance binds an implementation to the abstraction permanently,
which makes it difficult to modify, extend, and reuse abstractions and
implementations independently.
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
• Applicability
You want to avoid a permanent binding between an abstraction and its
implementation
Both the abstraction and their implementations should be extensible
by sub-classing
Changes in the implementation of an abstraction should have no
impact on clients
You have a proliferation of classes as shown earlier
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
• Structure
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
• Advantages
Decoupling interface and implementation
• An implementation is not bound permanently to an interface
Improve extensibility
• You can extend the Abstraction and Implementor hierarhies independently
Hiding implementation details from clients
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Bridge
• Related Patterns
An Abstract Factory can create and configure a particular Bridge
The Adapter pattern is oriented towards making unrelated classes
work together and it is usually applied to systems after they are
designed
Bridge is used up-front in a design to let abstractions and
implementations vary independently
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
• Print method:
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
• Intent
Provide a way to access the elements of an aggregate object
sequentially without exposing its underlying representation
• Applicability
To access an aggregate object’s contents without exposing its internal
representation
To support multiple traversals of aggregate objects
To provide a uniform interface for traversing different aggregate
structures
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
• Structure
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Iterator
• Advantages
It supports variations in the traversal of an aggregate
Iterators simplify the Aggregate interface
More than one traversal can be in progress at once
• Related Patterns
Composite: Iterators are often applied to recursive structures such as
Composites
Factory Method: Polymorphic iterators rely on factory methods to
instantiate the appropriate Iterator subclass
Memento is often used in conjunction with the Iterator pattern. An
iterator can use a memento to capture the state of an iteration. The
iterator stores the memento internally
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
• Intent
Compose objects into tree structures to represent part-whole
hierarchies. Composite lets clients treat individual objects and
composition of objects uniformly.
• Applicability
You want to represent part-whole hierarchies of objects
You want clients to be able to ignore the differences between
compositions of objects and individual objects.
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
• Structure
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
• Advantages
Defines hierarchies consisting of primitive objects and composite
objects
Makes the client simple. Clients can treat composite structures and
individual objects uniformly
Makes it easier to add new components. Clients don’t have to be
changed for new Component classes
• Disadvantages
It makes it harder to restrict the components of a composite.
Sometimes you want composite to have only certain components.
You’ll have to use run-time checks
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Composite
• Related patterns
Often the component-parent link is used for Chain of Responsibility
Decorator is often used with Composite. When decorator and
composite are used together, they will usually have a common parent
class. So decorators will have to support the Component interface with
operations like Add, Remove and GetChild
Flyweight let you share components, but they can no longer refer to
their parents
Iterator can be used to traverse composites
Visitor localizes operations and behavior that would otherwise be
distributed across Composite and Leaf classes
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
• Intent
Attach additional responsibilities to an object dynamically. Decorators
provide a flexible alternative to subclassing for extending functionality
• Applicability
To add responsibilities to individual objects dynamically and
transparently , that is, without affecting other objects
To add responsibilities that can be withdrawn
When extension by subclassing is impractical. Sometimes a large
number of independent extensions are possible and would produce an
explosion of subclasses to support every combination
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
• Structure
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
• Advantages
More flexibility than static inheritance.
• With decorators, responsibility can be added and removed at run-time simply by
attaching and detaching them. In contrast, inheritance requires creating a new class
for each additional responsibility
• Disadvantages
Lots of little objects
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Decorator
• Related patterns
Adapter: A decorator is different from an adapter in that a decorator
only changes an object responsibilities, not its interface; an adapter
will give an object a completely new interface
Composite: A decorator can be viewed as a degenerate composite
with only one component. However, a decorator adds additional
responsibilities, but it isn’t intended for object aggregation
Strategy: A decorator lets you change the skin of an object; a strategy
lets you change the guts. These are the two alternative ways of
changing an object
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
• Intent
Provide a unified interface to a set of interfaces in a subsystem. Façade
defines a high level interface that makes the subsystem easier to use
• Applicability
You want to provide a simple interface to a complex subsystem
• Subsystems often get more complex as they evolve
• A façade can provide a simple default view of the system that is good enough for
most clients
• Only clients that need more customizability will need to look beyond the façade
There a many dependencies between clients and implementation
classes of an abstraction
You want to layer your subsystems
• Use a façade to define an entry point to each subsystem level
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
• Structure
• Advantages
It shields clients from system components
It promotes weak coupling between the subsystem and its clients
It doesn’t prevent applications from using subsystem classes if they
need to
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Façade
• Related patterns
Abstract Factory can be used with Façade to provide an interface for
creating subsystem objects in a subsystem independent way
Mediator is similar to Façade in that it abstracts functionality of
existing classes. However, Mediator’s purpose is to abstract arbitrary
communication between colleague objects, often centralizing
functionality that doesn’t belong in any of them
Usually only one Façade object is required. Thus Façade objects are
often Singletons
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Flyweight
• Intent
Use sharing to support large number of fine-grained objects efficiently
• Motivation
Text editor offers facilities for formatting and editing documents
Objects are used to represent tables, figures and characters in
document
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Flyweight
• Motivation
A flyweight is a shared object that can be used in multiple contexts
simultaneously
Flyweight cannot make assumptions about the context in which they
operate
The key concept is the distinction between intrinsic and extrinsic state
A document editor can create a flyweight for each letter of the
alphabet
Intrinsic state – character code
Extrinsic state – coordinate position in the document, typographic and
formatting style
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Flyweight
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Flyweight
• Applicability
An application uses a large number of objects
Storage costs are high because of the sheer quantity of objects
Most object state can be made extrinsic
Many group of objects can be replaced by relatively few shared
objects once extrinsic state is removed
The application doesn’t depend on object identity. Since flyweight
objects may be shared, identity tests will return true for conceptually
distinct objects
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Flyweight
• Structure
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Flyweight
• Advantages
The more flyweights are shared, the greater the storage savings
The greatest savings occur when the objects use substantial quantities
of both intrinsic and extrinsic state and the extrinsic state can be
computed rather then stored
• Disadvantages
Flyweights may introduce run-time costs associated with transferring,
finding or computing extrinsic state
• Related patterns
The Flyweight pattern is often combined with the Composite pattern
to implement a logically hierarchical structure with shared leaf nodes
It’s often best to implement State and Strategy objects as flyweights
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Flyweight
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Flyweight
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Flyweight
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Remote Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Remote Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Remote Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Remote Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Remote Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Remote Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Remote Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Remote Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Remote Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Virtual Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Virtual Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Virtual Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Virtual Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Virtual Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Virtual Proxy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Proxy
• Intent
Provide a surrogate or placeholder for another object to control access
to it
• Applicability
When there is a need for a more versatile or sophisticated reference
to an object than a simple pointer
A remote proxy provides a local representative for an object in a
different address space
A virtual proxy creates a expensive objects on demand
A protection proxy controls the access to the original object
A smart reference is a replacement for a bare pointer that performs
additional actions when an object is accessed
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Proxy
• Structure
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Proxy
• Related patterns
An adapter provides a different interface to the object it adapts. In
contrast, a proxy provides the same interface as it’s subject
Although decorators can have similar implementations to proxies,
decorators have a different purpose. A decorator adds one or more
responsibilities to an object, whereas proxy controls access to an
object
Proxies vary in the degree to which they are implemented like a
decorator
A protection proxy might be implemented exactly like a decorator.
A remote proxy will not contain a direct reference to its real subject but only an
indirect reference like an ID
A virtual proxy will start off with an indirect reference such as a file name, but will
eventually obtain and use a direct reference
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
• Intent
Define a one-to-many dependency between objects so that when one
object changes state, all its dependents are notified and updated
automatically
• Applicability
When an abstraction has two aspects, one dependent on the other.
Encapsulating those aspects in separate objects let you vary and reuse
them independently
When a change to one object requires changing others and you don’t
know how many objects need to be changed
When one object should be able to notify another object without
making assumptions about who these objects are
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Observer
• Structure
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Strategy
• Intent
Define family of algorithms, encapsulate each one, and make them
interchangeable. Strategy lets the algorithm vary independently from
clients that use them
• Applicability
Many related classes differ only in their behavior. Strategy provide a
way to configure a class with one of many behaviors
You need different variants of an algorithm
An algorithm uses data that clients shouldn’t know about
A class defines many behaviors and these appear as multiple
conditional statements in its operations. Instead of many conditionals
move related conditionals branches into their own strategy class
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Strategy
• Structure
• Related patterns
Strategy objects often make good flyweights
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Strategy
• Shopping chart with two payment strategies: Credit Card or
PayPal
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Strategy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Strategy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Strategy
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Assignment
• SSE 3.0
• A strategic decision has been made to transform our simple spreadsheet engine
into a full-blown spreadsheet application. This transformation is to occur gradually,
in a series of increments.
• In this increment, you have two important goals:
• Perform a restructuring of the current code, as to facilitate the future adding of a
graphical user interface (GUI), by implementing the architectural pattern MVC. In
the context of the existing, context-based and output-only interface, the first step
should be a clear-cut separation between the model component on one side, and
the view+controller components on the other side.
• Implement a “Facade” for the Model component.
• Implement the automatic updates of cells as follows: any change performed by a
user in a given cell, should automatically trigger the update of all affected cells (i.e.
reference cells and formula cells). This update process should work like a chain
reaction, or an avalanche, in the sense that any updated cells should in turn trigger
the update of all cells that depend on them, and so forth. (Hint: use the design
pattern Observer to implement the notification system).
Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania