SlideShare a Scribd company logo
Structural
Design Patterns
Michael Heron
Introduction
 Structural design patterns emphasize
relationships between entities.
 These can be classes or objects.
 They are designed to help deal with the
combinatorial complexity of large object
oriented programs.
 These often exhibit behaviours that are not
immediately intuitive.
Structural Patterns
 Common to the whole philosophy behind
structural patterns is the separation between
abstraction and implementation.
 This is a good guideline for extensible design.
 Structural patterns subdivide into two broad
subcategories.
 Class structural patterns
 Where the emphasis is on the relationship
between classes
 Object structural patterns
 The emphasis is on consistency of interaction
and realization of new functionality.
Façade
 When a model is especially complex, it can
be useful to add in an additional pattern to
help manage the external interface of that
model.
 That pattern is called a façade.
 A façade sits between the view/controller
and provides a stripped down or simplified
interface to complex functionality.
 There are costs to this though in terms of
coupling and cohesion.
 A façade is a structural pattern.
Façade
 A façade provides several benefits
 Makes software libraries easier to use by
providing helper methods
 Makes code more readable
 Can abstract away from the
implementation details of a complex library
or collection of classes.
 Can work as a wrapper for poorly designed
APIs, or for complex compound
relationships between objects.
Façade Example
public class FacadeExample {
SomeClass one;
SomeOtherClass two;
SomeKindOfConfigClass three;
public SomeOtherClass handleInput (String configInfo) {
three = SomeKindOfConfigClass (configInfo)
one = new SomeClass (configInfo);
two = one.getSomethingOut ();
return two;
}
}
Façade Example
public class FacadeExample {
public SomeOtherClass handleInput (String configInfo) {
return myFacade.doSomeMagic (configInfo);
}
}
public class Facade {
SomeClass one;
SomeOtherClass two;
SomeKindOfConfigClass three;
public SomeOtherClass doSomeMagic (String configInfo) {
three = SomeKindOfConfigClass (configInfo)
one = new SomeClass (configInfo);
two = one.getSomethingOut ();
return two;
}
}
Façade
 The more code that goes through the
façade, the more powerful it becomes.
 If just used in one place, it has limited benefit.
 Multiple objects can make use of the façade.
 Greatly increasing the easy of development
and reducing the impact of change.
 All the user has to know is what needs to go
in, and what comes out.
 The façade hides the rest
Downsides
 This comes with a necessary loss of control.
 You don’t really know what’s happening internally.
 Facades are by definition simplified interfaces.
 So you may not be able to do Clever Stuff when
blocked by one.
 Facades increase structural complexity.
 It’s a class that didn’t exist before.
 Facades increase coupling and reduce cohesion.
 They often have to link everywhere, and the set of
methods they expose often lack consistency
The Adapter
 The Adapter design pattern is used to
provide compatibility between
incompatible programming interfaces.
 This can be used to provide legacy support,
or consistency between different APIs.
 These are also sometimes called
wrappers.
 We have a class that wraps around another
class and presents an external interface.
The Adapter
 Internally, an adapter can be as simple as
a composite object and a method that
handles translations.
 We can combine this with other design
patterns to get more flexible solutions.
 For example, a factory for adapters
 Or adapters that work using the strategy
pattern.
 It is the combination of design patterns
that has the greatest potential in design.
Simple Example
abstract class Shape {
abstract void drawShape (Graphics g, int x1, int x2, int y1, int y2);
}
public class Adapter {
private Shape sh;
public void drawShape (int x, int y, int len, int ht, Graphics g) {
sh.drawShape (g, x, x+ht, y, y+len);
}
}
Adapters and Facades
 What’s the difference between a façade and
an adapter?
 A façade presents a new simplified API to
external objects.
 An adapter converts an existing API to a
common standard.
 The Façade creates the programming
interface for the specific combination of
objects.
 The adapter simply enforces consistency
between incompatible interfaces.
The Flyweight
 Object oriented programming languages
provide fine-grained control over data
and behaviours.
 But that flexibility comes at a cost.
 The Flyweight pattern is used to reduce
the memory and instantiation cost when
dealing with large numbers of finely-
grained objects.
 It does this by sharing state whenever
possible.
Scenario
 Imagine a word processor.
 They’re pretty flexible. You can store decoration
detail on any character in the text.
 How is this done?
 You could represent each character as an object.
 You could have each character contain its own
font object…
 … but that’s quite a memory overhead.
 It would be much better if instead of holding a
large font object, we held only a reference to a
font object.
The Flyweight
 The Flyweight pattern comes in to reduce the
state requirements here.
 It maintains a cache of previously utilised
configurations or styles.
 Each character is given a reference to a
configuration object.
 When a configuration is applied, we check the
cache to see if it exists.
 If it doesn’t, it creates one and add it to the cache.
 The Flyweight dramatically reduces the object
footprint.
 We have thousands of small objects rather than
thousands of large objects.
Before and After
public class MyCharacter {
char letter;
Font myFont;
void applyDecoration (string font, int size);
myFont = new Font (font, size);
}
}
public class MyCharacter {
char letter;
Font myFont;
void applyDecoration (string font, int size);
myFont = FlyweightCache.getFont (font, size);
}
}
Implementing a Flyweight
 The flyweight patterns makes no
implementation assumptions.
 A reasonably good way to do it is through a
hash map or other collection.
 Standard memoization techniques can be
used here.
 When a request is made, check the cache.
 If it’s there, return it.
 If it’s not, create it and put it in the cache and
return the new instance.
Limitations of the Flyweight
Pattern
 Flyweight is only an appropriate design
pattern when object references have no
context.
 As in, it doesn’t matter to what they are being
applied.
 A font object is a good example.
 It doesn’t matter if it’s being applied to a
number, a character, or a special symbol.
 A customer object is a bad example.
 Each customer is unique.
The Composite Pattern
 We often have to manipulate collections of
objects when programming.
 The composite pattern is designed to simplify
this.
 Internally, it represents data as a simple list or
other collection.
 Requires the use of polymorphism to assure
structural compatability.
 Externally it presents an API to add and
remove objects.
 And also to execute operations on the
collection as a whole.
The Composite Pattern
public class ShapeCollection implements Shape() {
ArrayList shapes = new ArrayList();
void addShape (Shape s) {
shapes.Add (s);
}
void removeShape (Shape s) {
shapes.Remove (s);
}
void draw() {
foreach (Shape s in shapes) {
s.draw();
}
}
void setColour (Colour c) {
foreach (Shape s in shapes) {
s.setColour (c);
}
}
}
The Composite Pattern
public MainProgram() {
Circle circle = new Circle();
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
ShapeCollection myCollection = new ShapeCollection();
ShapeCollection overallScene = new ShapeCollection();
myCollection.addShape (circle);
myCollection.addShape (rect);
overallScene.addShape (myCollection);
overallScene.addShape (tri);
myCollection.setColour (Colour.RED);
overallScene.draw();
}
Why Use Composite?
 Sometimes we need to be able to perform
operations on groups of objects as a whole.
 We may wish to move a group of shapes in a
graphics package as one example.
 These often exist side by side with more
primitive objects that get manipulated
individually.
 Having handling code for each of these
conditions is bad design.
The Composite
 The composite allows us to treat
collections and individual objects through
one consistent interface.
 We don’t need to worry about which we
are dealing with at any one time.
 It works by ensuring that the collection
implements the common interface shared
by all its constituent bits.
 The relationship is recursive if done
correctly.
Summary
 Structural patterns are the last of the families of
design patterns we are going to look at.
 We use an adapter to deal with incompatible
APIs.
 We use a bridge to decouple abstraction from
implementation.
 Implementation is very similar to strategy, only the
intent is unique.
 Flyweight patterns are used to reduce processing
and memory overheads.
 Composites are used to allow recursive and
flexible aggregate manipulation of objects.

More Related Content

What's hot (20)

PPTX
Fragment
nationalmobileapps
 
PPTX
Design Patterns - General Introduction
Asma CHERIF
 
PPTX
Introduction to spring boot
Santosh Kumar Kar
 
PPTX
Introduction to React JS
Arnold Asllani
 
PDF
Spring Boot
HongSeong Jeon
 
PPT
Builder pattern
Shakil Ahmed
 
PDF
WEB DEVELOPMENT USING REACT JS
MuthuKumaran Singaravelu
 
PPTX
REST & RESTful Web Services
Halil Burak Cetinkaya
 
PDF
Java Design Patterns Tutorial | Edureka
Edureka!
 
PPT
Developing an ASP.NET Web Application
Rishi Kothari
 
PPT
TypeScript Presentation
Patrick John Pacaña
 
PPT
Adapter pattern
Shakil Ahmed
 
PPTX
[Final] ReactJS presentation
洪 鹏发
 
PPTX
Flutter presentation.pptx
FalgunSorathiya
 
PPT
Jsp ppt
Vikas Jagtap
 
PPT
Java basic
Sonam Sharma
 
PPT
Tomcat
Venkat Pinagadi
 
Design Patterns - General Introduction
Asma CHERIF
 
Introduction to spring boot
Santosh Kumar Kar
 
Introduction to React JS
Arnold Asllani
 
Spring Boot
HongSeong Jeon
 
Builder pattern
Shakil Ahmed
 
WEB DEVELOPMENT USING REACT JS
MuthuKumaran Singaravelu
 
REST & RESTful Web Services
Halil Burak Cetinkaya
 
Java Design Patterns Tutorial | Edureka
Edureka!
 
Developing an ASP.NET Web Application
Rishi Kothari
 
TypeScript Presentation
Patrick John Pacaña
 
Adapter pattern
Shakil Ahmed
 
[Final] ReactJS presentation
洪 鹏发
 
Flutter presentation.pptx
FalgunSorathiya
 
Jsp ppt
Vikas Jagtap
 
Java basic
Sonam Sharma
 

Viewers also liked (20)

PDF
Software Design Patterns. Part I :: Structural Patterns
Sergey Aganezov
 
PPTX
Structural Design pattern - Adapter
Manoj Kumar
 
PPTX
PATTERNS03 - Behavioural Design Patterns
Michael Heron
 
PPT
Design Patterns
soms_1
 
PPTX
PATTERNS02 - Creational Design Patterns
Michael Heron
 
PPTX
Musings on misconduct
Michael Heron
 
PPT
Singleton Object Management
ppd1961
 
PPT
Introduction to Design Patterns and Singleton
Jonathan Simon
 
PPTX
Singleton class in Java
Rahul Sharma
 
PPTX
Construction Management in Developing Countries, Lecture 8
Hari Krishna Shrestha
 
PDF
Code & Cannoli - Domain Driven Design
Frank Levering
 
ZIP
Adapter Design Pattern
guy_davis
 
PDF
Domain Driven Design Development Spring Portfolio
Srini Penchikala
 
PDF
003 obf600105 gpon ma5608 t basic operation and maintenance v8r15 issue1.02 (...
Cavanghetboi Cavangboihet
 
PPT
Design patterns ppt
Aman Jain
 
PPTX
A Practical Guide to Domain Driven Design: Presentation Slides
thinkddd
 
PDF
Structure as Architecture
Hashim k abdul azeez
 
PDF
Rem Koolhaas –designing the design process
Sjors Timmer
 
PPTX
Domain Driven Design 101
Richard Dingwall
 
PDF
Design patterns
abhisheksagi
 
Software Design Patterns. Part I :: Structural Patterns
Sergey Aganezov
 
Structural Design pattern - Adapter
Manoj Kumar
 
PATTERNS03 - Behavioural Design Patterns
Michael Heron
 
Design Patterns
soms_1
 
PATTERNS02 - Creational Design Patterns
Michael Heron
 
Musings on misconduct
Michael Heron
 
Singleton Object Management
ppd1961
 
Introduction to Design Patterns and Singleton
Jonathan Simon
 
Singleton class in Java
Rahul Sharma
 
Construction Management in Developing Countries, Lecture 8
Hari Krishna Shrestha
 
Code & Cannoli - Domain Driven Design
Frank Levering
 
Adapter Design Pattern
guy_davis
 
Domain Driven Design Development Spring Portfolio
Srini Penchikala
 
003 obf600105 gpon ma5608 t basic operation and maintenance v8r15 issue1.02 (...
Cavanghetboi Cavangboihet
 
Design patterns ppt
Aman Jain
 
A Practical Guide to Domain Driven Design: Presentation Slides
thinkddd
 
Structure as Architecture
Hashim k abdul azeez
 
Rem Koolhaas –designing the design process
Sjors Timmer
 
Domain Driven Design 101
Richard Dingwall
 
Design patterns
abhisheksagi
 
Ad

Similar to PATTERNS04 - Structural Design Patterns (20)

PPT
M04 Design Patterns
Dang Tuan
 
PPT
M04_DesignPatterns software engineering.ppt
ssuser2d043c
 
PPT
Design Patterns
Rafael Coutinho
 
PDF
Module 4: UML In Action - Design Patterns
jaden65832
 
PPTX
Structural pattern 3
Naga Muruga
 
PDF
Design patterns
Anas Alpure
 
PPTX
Software System Architecture-Lecture 6.pptx
ssuser9a23691
 
PPTX
Advance oops concepts
Sangharsh agarwal
 
PDF
Javascript Design Patterns
Lilia Sfaxi
 
PDF
Design Patterns
adil raja
 
PPTX
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Luis Valencia
 
PPT
Design Patterns By Sisimon Soman
Sisimon Soman
 
PPTX
Design Patterns - Part 1 of 2
Savio Sebastian
 
PPT
Composite Design Pattern
Ferdous Mahmud Shaon
 
PPTX
Design patterns in brief
DUONG Trong Tan
 
PPT
Software Design Patterns
Satheesh Sukumaran
 
PPT
Software Design Patterns
Satheesh Sukumaran
 
PPTX
Sda 9
AmberMughal5
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
ODP
Patterns in Python
dn
 
M04 Design Patterns
Dang Tuan
 
M04_DesignPatterns software engineering.ppt
ssuser2d043c
 
Design Patterns
Rafael Coutinho
 
Module 4: UML In Action - Design Patterns
jaden65832
 
Structural pattern 3
Naga Muruga
 
Design patterns
Anas Alpure
 
Software System Architecture-Lecture 6.pptx
ssuser9a23691
 
Advance oops concepts
Sangharsh agarwal
 
Javascript Design Patterns
Lilia Sfaxi
 
Design Patterns
adil raja
 
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Luis Valencia
 
Design Patterns By Sisimon Soman
Sisimon Soman
 
Design Patterns - Part 1 of 2
Savio Sebastian
 
Composite Design Pattern
Ferdous Mahmud Shaon
 
Design patterns in brief
DUONG Trong Tan
 
Software Design Patterns
Satheesh Sukumaran
 
Software Design Patterns
Satheesh Sukumaran
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Patterns in Python
dn
 
Ad

More from Michael Heron (20)

PPTX
Meeple centred design - Board Game Accessibility
Michael Heron
 
PDF
Accessibility Support with the ACCESS Framework
Michael Heron
 
PDF
ACCESS: A Technical Framework for Adaptive Accessibility Support
Michael Heron
 
PPTX
Authorship and Autership
Michael Heron
 
PDF
Text parser based interaction
Michael Heron
 
PPTX
SAD04 - Inheritance
Michael Heron
 
PPT
GRPHICS08 - Raytracing and Radiosity
Michael Heron
 
PPT
GRPHICS07 - Textures
Michael Heron
 
PPT
GRPHICS06 - Shading
Michael Heron
 
PPT
GRPHICS05 - Rendering (2)
Michael Heron
 
PPT
GRPHICS04 - Rendering (1)
Michael Heron
 
PPTX
GRPHICS03 - Graphical Representation
Michael Heron
 
PPTX
GRPHICS02 - Creating 3D Graphics
Michael Heron
 
PPTX
GRPHICS01 - Introduction to 3D Graphics
Michael Heron
 
PPT
GRPHICS09 - Art Appreciation
Michael Heron
 
PPTX
2CPP18 - Modifiers
Michael Heron
 
PPTX
2CPP17 - File IO
Michael Heron
 
PPT
2CPP16 - STL
Michael Heron
 
PPT
2CPP15 - Templates
Michael Heron
 
PPTX
2CPP14 - Abstraction
Michael Heron
 
Meeple centred design - Board Game Accessibility
Michael Heron
 
Accessibility Support with the ACCESS Framework
Michael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
Michael Heron
 
Authorship and Autership
Michael Heron
 
Text parser based interaction
Michael Heron
 
SAD04 - Inheritance
Michael Heron
 
GRPHICS08 - Raytracing and Radiosity
Michael Heron
 
GRPHICS07 - Textures
Michael Heron
 
GRPHICS06 - Shading
Michael Heron
 
GRPHICS05 - Rendering (2)
Michael Heron
 
GRPHICS04 - Rendering (1)
Michael Heron
 
GRPHICS03 - Graphical Representation
Michael Heron
 
GRPHICS02 - Creating 3D Graphics
Michael Heron
 
GRPHICS01 - Introduction to 3D Graphics
Michael Heron
 
GRPHICS09 - Art Appreciation
Michael Heron
 
2CPP18 - Modifiers
Michael Heron
 
2CPP17 - File IO
Michael Heron
 
2CPP16 - STL
Michael Heron
 
2CPP15 - Templates
Michael Heron
 
2CPP14 - Abstraction
Michael Heron
 

Recently uploaded (20)

PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 

PATTERNS04 - Structural Design Patterns

  • 2. Introduction  Structural design patterns emphasize relationships between entities.  These can be classes or objects.  They are designed to help deal with the combinatorial complexity of large object oriented programs.  These often exhibit behaviours that are not immediately intuitive.
  • 3. Structural Patterns  Common to the whole philosophy behind structural patterns is the separation between abstraction and implementation.  This is a good guideline for extensible design.  Structural patterns subdivide into two broad subcategories.  Class structural patterns  Where the emphasis is on the relationship between classes  Object structural patterns  The emphasis is on consistency of interaction and realization of new functionality.
  • 4. Façade  When a model is especially complex, it can be useful to add in an additional pattern to help manage the external interface of that model.  That pattern is called a façade.  A façade sits between the view/controller and provides a stripped down or simplified interface to complex functionality.  There are costs to this though in terms of coupling and cohesion.  A façade is a structural pattern.
  • 5. Façade  A façade provides several benefits  Makes software libraries easier to use by providing helper methods  Makes code more readable  Can abstract away from the implementation details of a complex library or collection of classes.  Can work as a wrapper for poorly designed APIs, or for complex compound relationships between objects.
  • 6. Façade Example public class FacadeExample { SomeClass one; SomeOtherClass two; SomeKindOfConfigClass three; public SomeOtherClass handleInput (String configInfo) { three = SomeKindOfConfigClass (configInfo) one = new SomeClass (configInfo); two = one.getSomethingOut (); return two; } }
  • 7. Façade Example public class FacadeExample { public SomeOtherClass handleInput (String configInfo) { return myFacade.doSomeMagic (configInfo); } } public class Facade { SomeClass one; SomeOtherClass two; SomeKindOfConfigClass three; public SomeOtherClass doSomeMagic (String configInfo) { three = SomeKindOfConfigClass (configInfo) one = new SomeClass (configInfo); two = one.getSomethingOut (); return two; } }
  • 8. Façade  The more code that goes through the façade, the more powerful it becomes.  If just used in one place, it has limited benefit.  Multiple objects can make use of the façade.  Greatly increasing the easy of development and reducing the impact of change.  All the user has to know is what needs to go in, and what comes out.  The façade hides the rest
  • 9. Downsides  This comes with a necessary loss of control.  You don’t really know what’s happening internally.  Facades are by definition simplified interfaces.  So you may not be able to do Clever Stuff when blocked by one.  Facades increase structural complexity.  It’s a class that didn’t exist before.  Facades increase coupling and reduce cohesion.  They often have to link everywhere, and the set of methods they expose often lack consistency
  • 10. The Adapter  The Adapter design pattern is used to provide compatibility between incompatible programming interfaces.  This can be used to provide legacy support, or consistency between different APIs.  These are also sometimes called wrappers.  We have a class that wraps around another class and presents an external interface.
  • 11. The Adapter  Internally, an adapter can be as simple as a composite object and a method that handles translations.  We can combine this with other design patterns to get more flexible solutions.  For example, a factory for adapters  Or adapters that work using the strategy pattern.  It is the combination of design patterns that has the greatest potential in design.
  • 12. Simple Example abstract class Shape { abstract void drawShape (Graphics g, int x1, int x2, int y1, int y2); } public class Adapter { private Shape sh; public void drawShape (int x, int y, int len, int ht, Graphics g) { sh.drawShape (g, x, x+ht, y, y+len); } }
  • 13. Adapters and Facades  What’s the difference between a façade and an adapter?  A façade presents a new simplified API to external objects.  An adapter converts an existing API to a common standard.  The Façade creates the programming interface for the specific combination of objects.  The adapter simply enforces consistency between incompatible interfaces.
  • 14. The Flyweight  Object oriented programming languages provide fine-grained control over data and behaviours.  But that flexibility comes at a cost.  The Flyweight pattern is used to reduce the memory and instantiation cost when dealing with large numbers of finely- grained objects.  It does this by sharing state whenever possible.
  • 15. Scenario  Imagine a word processor.  They’re pretty flexible. You can store decoration detail on any character in the text.  How is this done?  You could represent each character as an object.  You could have each character contain its own font object…  … but that’s quite a memory overhead.  It would be much better if instead of holding a large font object, we held only a reference to a font object.
  • 16. The Flyweight  The Flyweight pattern comes in to reduce the state requirements here.  It maintains a cache of previously utilised configurations or styles.  Each character is given a reference to a configuration object.  When a configuration is applied, we check the cache to see if it exists.  If it doesn’t, it creates one and add it to the cache.  The Flyweight dramatically reduces the object footprint.  We have thousands of small objects rather than thousands of large objects.
  • 17. Before and After public class MyCharacter { char letter; Font myFont; void applyDecoration (string font, int size); myFont = new Font (font, size); } } public class MyCharacter { char letter; Font myFont; void applyDecoration (string font, int size); myFont = FlyweightCache.getFont (font, size); } }
  • 18. Implementing a Flyweight  The flyweight patterns makes no implementation assumptions.  A reasonably good way to do it is through a hash map or other collection.  Standard memoization techniques can be used here.  When a request is made, check the cache.  If it’s there, return it.  If it’s not, create it and put it in the cache and return the new instance.
  • 19. Limitations of the Flyweight Pattern  Flyweight is only an appropriate design pattern when object references have no context.  As in, it doesn’t matter to what they are being applied.  A font object is a good example.  It doesn’t matter if it’s being applied to a number, a character, or a special symbol.  A customer object is a bad example.  Each customer is unique.
  • 20. The Composite Pattern  We often have to manipulate collections of objects when programming.  The composite pattern is designed to simplify this.  Internally, it represents data as a simple list or other collection.  Requires the use of polymorphism to assure structural compatability.  Externally it presents an API to add and remove objects.  And also to execute operations on the collection as a whole.
  • 21. The Composite Pattern public class ShapeCollection implements Shape() { ArrayList shapes = new ArrayList(); void addShape (Shape s) { shapes.Add (s); } void removeShape (Shape s) { shapes.Remove (s); } void draw() { foreach (Shape s in shapes) { s.draw(); } } void setColour (Colour c) { foreach (Shape s in shapes) { s.setColour (c); } } }
  • 22. The Composite Pattern public MainProgram() { Circle circle = new Circle(); Rectangle rect = new Rectangle(); Triangle tri = new Triangle(); ShapeCollection myCollection = new ShapeCollection(); ShapeCollection overallScene = new ShapeCollection(); myCollection.addShape (circle); myCollection.addShape (rect); overallScene.addShape (myCollection); overallScene.addShape (tri); myCollection.setColour (Colour.RED); overallScene.draw(); }
  • 23. Why Use Composite?  Sometimes we need to be able to perform operations on groups of objects as a whole.  We may wish to move a group of shapes in a graphics package as one example.  These often exist side by side with more primitive objects that get manipulated individually.  Having handling code for each of these conditions is bad design.
  • 24. The Composite  The composite allows us to treat collections and individual objects through one consistent interface.  We don’t need to worry about which we are dealing with at any one time.  It works by ensuring that the collection implements the common interface shared by all its constituent bits.  The relationship is recursive if done correctly.
  • 25. Summary  Structural patterns are the last of the families of design patterns we are going to look at.  We use an adapter to deal with incompatible APIs.  We use a bridge to decouple abstraction from implementation.  Implementation is very similar to strategy, only the intent is unique.  Flyweight patterns are used to reduce processing and memory overheads.  Composites are used to allow recursive and flexible aggregate manipulation of objects.