You are on page 1of 45

Structural Pattern part-I introduction

S. No TOPIC 1 2 3 4 PPT Slides L1 L2 L3 L4 3 18 19 31 32 37 38 40

Adaptor Bridge Composite Repeated key points for Structural Patterns (Intent, Motivation, Also Known As) Code Examples Reference

5
6 7

L5
L6 L7

41 42
43 44 45 45

UNIT-IV

Agenda
Intent & Motivation Structure Applicability Consequences Known Uses Related Patterns References
UNIT-IV 2

L1

What is Adapter?
Intent: Change the interface of a class into another interface which is expected by the client. Also Know As: Wrapper
UNIT-IV 3

Motivation

L1

UNIT-IV

L1

Structure (Class)

UNIT-IV

L1

Structure (Object)

UNIT-IV

L1

Applicability
Use an existing class whose interface does not match the requirement Create a reusable class though the interfaces are not necessary compatible with callers Want to use several existing subclasses, but it is impractical to subclass everyone. (Object Adapter Only)
UNIT-IV 7

L1

Class Adapter Pattern

Pros

Only 1 new object, no additional indirection Less code required than the object Adapter Can override Adaptee's behaviour as required Requires sub-classing (tough for single inheritance) Less flexible than object Adapter

Cons

UNIT-IV

L1

Object Adapter Pattern


Pros
More flexible than class Adapter Doesn't require sub-classing to work Adapter works with Adaptee and all of its subclasses

Cons
Harder to override Adaptee behavior Requires more code to implement properly
UNIT-IV 9

L1

Pluggable Adapters

implemented with abstract operations


UNIT-IV 10

L1

Pluggable Adapters

implemented with UNIT-IV delegate objects

11

L1

Two-way Adapters
class SquarePeg { public: void virtual squarePegOperation() { blah } } class PegAdapter: public SquarePeg, RoundPeg {
public: void virtual roundPegOperation() {

add some corners; class RoundPeg {


public: void virtual roundPegOperation() { blah } } void virtual squarePegOperation() { add some corners; roundPegOperation(); }
UNIT-IV 12

squarePegOperation();

L1

Adapting Local Classes to RMI


Comparison: Increases reusability of local class Improves performance of local class Doesn't use Java single parent by subclassing (uses composition)
UNIT-IV 13

L1

Related Patterns
Adapter can be similar to the remote form of Proxy. However, Proxy doesn't change interfaces. Decorator enhances another object without changing its interface. Bridge similar structure to Adapter, but different intent. Separates interface from implementation.
UNIT-IV 14

L1

Conclusions
Allows collaboration between classes with incompatible interfaces Implemented in either class-based (inheritance) or object-based (composition & delegation) manner Useful pattern which promotes reuse and allows integration of diverse software components
UNIT-IV 15

L1

Adapter
You have
legacy code current client

Adapter changes interface of legacy code so client can use it Adapter fills the gap b/w two interfaces No changes needed for either
legacy code, or client
UNIT-IV 16

L1

Adapter (cont.)
class NewTime { public: int GetTime() { return m_oldtime.get_time() * 1000 + 8; } private: OldTime m_oldtime; };

UNIT-IV

17

L2

The Bridge Pattern

UNIT-IV

18

Overview
Intent Also Known As Motivation Participants Structure Applicability Benefits Drawbacks Related Pattern I
UNIT-IV 19

L2

L2

BRIDGE (Object Structural)


Intent: Decouple as abstraction from its implementation so that the two can vary independently. Also Known As: Handle/Body

UNIT-IV

20

L2

Motivation
Entry

oracleDBEntry

FilesysEntry

UNIT-IV

21

L2

Motivation
Entry

Appointment

Task

OracleDBApp.

FilesysApp.

OracleDBTask

FilesysTask

UNIT-IV

22

L2

Motivation
Entry
getText() setText() Destroy()

PersistentImp.

impl

Bridge
Appointment
getAlarm() setAlarm() getText() setText() Destroy()UNIT-IV

initialize() store() load() Destroy()

Task
getPriority() setPriority() getText() setText() Destroy()

OraclePImp AccessPImp
initialize() store() load() destroy() initialize() store() load() destroy()
23

L2

Participants
Abstraction (Entry): - define the abstractions interface - maintains a reference to an object of type Implementor Refined Abstraction (Task): - extends the interface defined by Abstraction

UNIT-IV

24

L2

Participants (continue)
Implementor (persistentImp): - defines an interface for implementation classes. Typically, this Implementors interface provides only primitive menthod ConcreteImplementor (oraclePImp,
AccessPImp):

- implements the Implementors interface


UNIT-IV 25

L2

Structure
Client

Abstraction

Implementer

impl

OperationImp()

RefinedAbstraction

ConcreteImplementerA

ConcreteImplementerB

UNIT-IV

26

L2

Applicability
Want to avoid a permanent binding between an abstraction and implementation. When abstractions and implementations should be extensible through subclassing. When implementation changes should not impact clients.
UNIT-IV 27

L2

Applicability (continue)
When the implementation should be completely hidden from the client. (C++) When you have a proliferation of classes. When, unknown to the client, implementations are shared among objects.
UNIT-IV 28

L2

Benefits
Avoid permanent binding between an abstraction and its implementation Avoid nested generalizations Ease adding new implementations Reduce code repetition Allow runtime switching of behavior
UNIT-IV 29

L2

Drawbacks
Double indirection - entry operation are implemented by
subclasses of PersistentImp class. Entry class must delegate the message to a PersistentImp subclass which implements the appropriate method. This will have a slight impact on performance.
UNIT-IV 30

L2

Composite Pattern
Intent : Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

UNIT-IV

31

L2

Consequences
decoupling interface & implementation
- implementation of abstraction - can be configured at run-time - eliminate compile time dependencies on implementation - encourages layering

improved extensibility
- Abstraction & Implementer - can be extended

independently

hiding implementation details from clients


UNIT-IV 32

L3

Composite Pattern
Facilitates the composition of objects into tree structures that represent partwhole hierarchies. These hierarchies consist of both primitive and composite objects.

UNIT-IV

33

L3

UNIT-IV

34

L3

Observations
The Component (Graphic) is an abstract class that declares the interface for the objects in the pattern. As the interface, it declares methods (such as Draw) that are specific to the graphical objects. Line, Rectangle, and Text are so-called Leafs, which are subclasses that implement Draw to draw lines, rectangles, and text, respectively.
UNIT-IV 35

L3

Observations (Continued)
The Picture class represents a number of graphics objects. It can call Draw on its children and also uses children to compose pictures using primitive objects.

UNIT-IV

36

L3

Concluding Considerations
The Composite Pattern is used to represent part-whole object hierarchies. Clients interact with objects through the component class. It enables clients to to ignore the specifics of which leaf or composite class they use. Can be used recursively, so that Display can show both flares and stars. New components can easily be added to a design.
UNIT-IV 37

L4

BRIDGE (Object Structural)


Intent: Decouple as abstraction from its implementation so that the two can vary independently. Also Known As: Handle/Body

UNIT-IV

38

L4

Motivation
Entry

oracleDBEntry

FilesysEntry

UNIT-IV

39

L4

Motivation
Entry

Appointment

Task

OracleDBApp.

FilesysApp.

OracleDBTask

FilesysTask

UNIT-IV

40

L5

Two-way Adapters
class SquarePeg { public: void virtual squarePegOperation() { blah } } class PegAdapter: public SquarePeg, RoundPeg {
public: void virtual roundPegOperation() {

add some corners; class RoundPeg {


public: void virtual roundPegOperation() { blah } } void virtual squarePegOperation() { add some corners; roundPegOperation(); }
UNIT-IV 41

squarePegOperation();

L5

Adapting Local Classes to RMI


Comparison: Increases reusability of local class Improves performance of local class Doesn't use Java single parent by subclassing (uses composition)
UNIT-IV 42

L6

Motivation
Entry
getText() setText() Destroy()

PersistentImp.

impl

Bridge
Appointment
getAlarm() setAlarm() getText() setText() Destroy()UNIT-IV

initialize() store() load() Destroy()

Task
getPriority() setPriority() getText() setText() Destroy()

OraclePImp AccessPImp
initialize() store() load() destroy() initialize() store() load() destroy()
43

L6

Participants
Abstraction (Entry): - define the abstractions interface - maintains a reference to an object of type Implementor Refined Abstraction (Task): - extends the interface defined by Abstraction

UNIT-IV

44

L7

References

Becker, Dan. Design networked applications in RMI using the Adapter design pattern. JavaWorld Magazine, May 1999. http://www.javaworld.com/javaworld/jw05-1999/jw-05-networked.html Buschmann et al. A System of Patterns: Pattern-Oriented Software Architecture. John Wiley and Sons. Chichester. 1996 Gamma et al. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. Boston. 1995 Nguyen, D.X. Tutorial 10: Stacks and Queues: The Adapter Pattern. Rice University. 1999. http://www.owlnet.rice.edu/~comp212/99fall/tutorials/10/tutorial10.html Whitney, Roger. CS 635 Advanced Object-Oriented Design & Programming. San Diego State University. 2001. http://www.eli.sdsu.edu/courses/spring01/cs635/notes/proxy/proxy.html#Heading1 0 Shalloway, Alan., and Trott, James R., Design Patterns Explained: A New Perspective on Object-Oriented Design, Addison-Wesley, 2002. Rising, Linda., The Patterns Handbook: Techniques, Strategies, and Applications, Cambridge university Press, 1998.
UNIT-IV 45

You might also like