You are on page 1of 32

# Structure Patters: Bridge and Flyweight

CSCI 3132 Summer 2011

1

Introducing  the  Bridge  Pa1ern
•  Intent
– To  decouple  an  abstrac7on  from  its   implementa7on  so  that  the  two  can  vary   independently.   – What  does  it  mean?
•  Decouple:  have  things  behave  independently  from  each   other.   •  Abstrac7on:  how  things  are  related  to  each  other   conceptually.

2

An  Example
•  A  simple  problem
–  Drawing  shapes
•  Rectangles

–  Two  diﬀerent  drawing  programs

3

double y2){! DP1::draw_a_line( x1.! }. x2.   •  V1Rectangle  uses  DP1   •  V2Rectangle  uses  DP2   class V1Rectangle : public Rectangle! {! public:! V1Rectangle(double x1. x2. y2).! ! 4 .Example  (Cont’d) •  Proper  use  of  inheritance   –  We  don’t  want  the  code  that  draws  the  rectangles  to   worry  about  what  type  of  drawing  program  it  should  use. double y1. y1. y1. double y1. y2){} void drawLine( double x1. double x2. double x2.! }! public:! ~V1Rectangle(void). double y2) : ! Rectangle(x1.

A  Design  using  Inheritance   Changing requirements: add a new shape .circle 5 .

Sequence  Diagram   6 .

Any  Problem? •  Combinatorial  explosion   – Another  new  drawing  program   •  Six  diﬀerent  kinds  of  Shapes     –  2  Shape  concepts  7mes  three  drawing  programs   – Another  type  of  Shape   •  Nine  diﬀerent  kinds  of  Shapes     –  3  Shape  concepts  7mes  three  drawing  programs   7 .

Learning  the  Bridge  Pa1ern:   An  Example •  Reason   –  Tightly  coupled   •  The  abstrac7on  (  the  kinds  of  Shapes  )  and  the  implementa7on   (  the  drawing  programs)   •  The  Bridge  pa1ern   –  To  decouple  an  abstrac7on  from  its  implementa7on  so   that  the  two  can  vary  independently   8 .

An  Observa7on •  The  way  to  look  at  design  pa1erns   –  Focus  on  the  context  of  the  pa1ern  –  the  problem  it  is   trying  to  solve   •  Not  on  the  solu7ons  the  pa1erns  oﬀer   •  The  bridge  pa1ern   –  An  abstrac7on  that  has  diﬀerent  implementa7ons   –  Allow  the  abstrac7on  and  the  implementa7on  to  vary   independently  of  each  other   9 .

Learning  the  Bridge  Pa1ern:  Deriving  It •  Step  1  :  ﬁnd  out  what  varies  and  encapsulate  it.       10 .   •  Step  2:  Represent  the  varia7ons.

Tie  the  classes  together   –  What  uses  what?   11 .Learning  the  Bridge  Pa1ern:  Deriving  It •  Step  3.

Learning  the  Bridge  Pa1ern:  Deriving  It •  Expanding  the  design   12 .

Separa7on  of     abstrac7on  and  implementa7on Separate the abstraction class hierarchy from the implementation class hierarchy and use delegation as a bridge between the two. 13 .

Class  Diagram •  Generic  structure  of  the  Bridge  pa1ern   14 .

Typically. can be different from Abstraction interface.The  Par7cipants   •  Abstraction – defines the abstraction’s interface RefinedAbstraction – extends the interface defined by Abstraction Implementor – defines the interface for implementation classes. Implementor provides only primitive operations. ConcreteImplementor – implements the Implementor interface and defines its concrete implementation. and Abstraction defines higher-level operations based on these primitives. 15 •  •  •  .

Using  the  Bridge  Pa1ern •  The  Bridge  pa1ern  oZen  incorporates  the  Adapter   pa1ern   –  Compound  design  pa1erns   •  Instan7a7ng  the  objects  of  the  Bridge  pa1ern   –  The  objects  represen7ng  the  abstrac7on  were  given  their   implementa7on  while  being  instan7ated.   •  Basic  strategies  for  dealing  with  varia7on:   –  Find  what  varies  and  encapsulate  it.   –  Favor  aggrega7on  over  inheritance.   16 .

–  You  want  to  share  an  implementa7on  among  mul7ple   objects.e.   –  Changes  in  the  implementa7on  of  an  abstrac7on   should  have  no  impact  on  clients  (i.Applicability   •  Use  the  bridge  when:   –  You  want  to  avoid  a  permanent  binding  between  an   abstrac7on  and  its  implementa7on.   17 .  code  should  not   have  to  be  recompiled).  and  want  to  hide  this  from  the  client.   –  Both  the  abstrac7ons  and  their  implementa7ons   should  be  extensible  by  subclassing.

Interface  and  implementa7on  can  be  varied   independently.Beneﬁts  in  using  Bridge  Pa1ern     Decoupling  abstrac7on  from  implementa7on     Reduc7on  in  the  number  of  sub  classes.   •  Loosely  coupled  client  code.   •  Abstrac7on  and  implementa7on  can  be  extended   independently.   Cleaner  code  and  Reduc7on  in  executable  size.     •  •  •  •  18 .

19 .   •  Bridge  is  designed  up-­‐front  to  let  the  abstrac7on  and  the   implementa7on  vary  independently.  so  that  you  can  vary  or  replace  the   implementa7on  without  changing  the  client  code.Bridge  and  Adapter   •  The  Bridge  pa1ern  is  designed  to  separate  a  class’s  interface   from  its  implementa7on.   •  Adapter  makes  things  work  aZer  they’re  designed.  Bridge   makes  them  work  before  they  are.  Adapter  is  retroﬁ1ed  to   make  unrelated  classes  work  together.

whereas  Bridge  decouples  the   abstrac7on  from  the  implementa7on.  they  are  trying  to  solve  two  diﬀerent   design  problems.  to  provide   diﬀerent  implementa7on  for  the  same  abstrac7on.   •  Strategy  is  mainly  concerned  in  encapsula7ng   algorithms.Bridge  and  Strategy     •  OZen.  these  two  pa1erns  are  similar  in   structure.  the  Strategy  Pa1ern  is  confused  with  the   Bridge  Pa1ern.     •  Even  though.     20 .

Bridge  and  Abstract  Factory     The  Abstract  Factory  pa1ern  can  be  used  by   the  Bridge  pa1ern  to  decide  which   implementa7on  class  to  instan7ate  for  an   abstrac7on  object.     21 .

Flyweight   22 .

txt")   23 .txt")   .txt")   •  new  File("notes..Problem  of  redundant  objects   •  Flyweight:  a  class  that  has  only  one  instance  for  each   unique  state   •  problem:  existence  of  redundant  objects  can  bog   down  system   –  many  objects  have  same  state   –  example:  File  objects  that  represent  the  same  ﬁle  on  disk   •  new  File("mobydick.txt")   •  new  File("mobydick.   •  new  File("notes.txt")   •  new  File("mobydick..

but  has  many  instances.Flyweight  pa1ern   •  ﬂyweight:  an  assurance  that  no  more  than  one  instance   of  a  class  will  have  iden7cal  state   •  achieved  by  caching  iden7cal  instances  of  objects  to   reduce  object  construc7on   –  similar  to  singleton.  one  for   each  unique-­‐state  object   •  useful  for  cases  when  there  are  many  instances  of  a  type   but  many  are  the  same   •  can  be  used  in  conjunc7on  with  Factory  pa1ern  to  create   a  very  eﬃcient  object-­‐builder   24 .

// true } } 25 .out.println(fly == fly2).out.println(weight == weight2). System. // true // true String distinctString = fly + weight.println(flyweight == "flyweight"). weight = "weight".println(distinctString == "flyweight"). System.intern().out. String fly2 = "fly".Flyweight  and  Strings   •  Flyweighted  strings   –  Java  Strings  are  ﬂyweighted  by  the  compiler  wherever   possible   –  can  be  ﬂyweighted  at  run7me  with  the  intern  method   public class StringTest { public static void main(String[] args) { String fly = "fly". weight2 = "weight".out. System. System. // false String flyweight = (fly + weight).

Implemen7ng  a  Flyweight   •  ﬂyweigh7ng  works  best  on  immutable  objects     –  immutable:  cannot  be  changed  once  constructed   class  pseudo-­‐code  sketch:   public  class  Flyweighted  {   sta7c  map  or  table  of  instances   private  constructor   sta7c  method  to  get  an  instance   if  we  have  created  this  type  of  instance  before.  make  the  new  instance.  store  and  return  it   }   26 .  get  it  from  map   and  return  it   otherwise.

Flyweight  sequence  diagram   27 27 .

y = y.y + ")".x + ". } public int getX() { return this. " + this. } public String toString() { return "(" + this.y. } } 28 . this.x.Class  before  ﬂyweigh7ng   •  A  class  to  be  ﬂyweighted   public class Point { private int x. int y) { this. } public int getY() { return this. public Point(int x.x = x. y.

" + y. if (instances.get(key). instances.. y. p). int y) { String key = x + ". } private final int x. 29 . y). // immutable private Point(int x. int y) { .put(key.containsKey(key)) // re-use existing pt return (Point)instances. Point p = new Point(x.Class  aZer  ﬂyweigh7ng   •  A  class  that  has  been  ﬂyweighted!   public class Point { private static Map instances = new HashMap(). return p. public static Point getInstance(int x..

Behavioral   pa1erns  describe  not  just  pa1erns  of  objects  or  classes  but   also  the  pa1erns  of  communica7on  between  them.GoF  Pa1erns  Summary   •  Crea3onal  Pa7erns  abstract  the  instan7a7on  process.     •  Behavioral  Pa7erns  are  concerned  with  algorithms  and  the   assignment  of  responsibili7es  between  objects.  They   help  make  a  system  independent  of  how  its  objects  are   created.  and  represented.  composed.       30 .   •  Structural  Pa7erns  are  concerned  with  how  classes  and   objects  are  composed  to  form  larger  structures.

Design  Space  for  GoF  Pa1erns   Scope: domain over which a pattern applies Purpose: reflects what a pattern does 31 .

GoF  Pa1ern  Rela7onships   32 .