SDSU CS 696: Advanced OO
Spring Semester, 1997
Doc 21, Flyweight and Adapter

To Lecture Notes Index
San Diego State University -- This page last updated May 6, 1997

Contents of Doc 21, Flyweight and Adapter


Flyweight slide # 1
Adapter slide # 4
...The Adridecoxegate Pattern? slide # 7


Doc 21, Flyweight and Adapter Slide # 1

Flyweight


The flyweight pattern allows you to support sharing larger number of fine-grained object efficiently

The pattern can be used when all the following are true


Doc 21, Flyweight and Adapter Slide # 2
Example

State transitions

Start state comes first
When get user name transfer to HaveUserName state
When get correct password transfer to Authorized state

HaveUserName and Authorized states need the user name

In concurrent server we may have many (100's ) different users connected at the same time.

State creation can become expensive, so create each state once (Singleton pattern)

Each time use the state pass the user name to the state


Doc 21, Flyweight and Adapter Slide # 3
class HaveUserName extends SPopState
     {
     private HaveUserName() {};
     private static HaveUserName instance;
          
     public getInstance( )
          {
          if ( instance == null ) 
               instance = new HaveUserName();
          return instance;
          }
          
     public SPopState pass( String userName, String password )
          {
          if ( validateUser( userName, password )
               return Authorized.getInstance();
          else
               return Start.getInstance();
          }
     }


Doc 21, Flyweight and Adapter Slide # 4

Adapter


The adapter pattern converts the interface of a class into another interface.


Use the Adapter pattern when you want to use an existing class and its interface does not match the one you need


Class Adapter



Doc 21, Flyweight and Adapter Slide # 5
Class Adapter Example

class OldSquarePeg
     {
     public:
          void squarePegOperation()
               { do something }
     }

class RoundPeg
     {
     public:
          void virtual roundPegOperation = 0;
     }

class PegAdapter: private OldSquarePeg, public RoundPeg
     {
     public:
          void virtual roundPegOperation()
               {
               add some corners;
               squarePegOperation();
               }
     }
     
void clientMethod()
     {
     RoundPeg* aPeg = new PegAdapter();
     aPeg->roundPegOperation();
     }

Doc 21, Flyweight and Adapter Slide # 6
Adapter Object

class OldSquarePeg     {
     public:
          void squarePegOperation() { do something }
     }

class RoundPeg     {
     public:
          void virtual roundPegOperation = 0;
     }

class PegAdapter:  public RoundPeg     {
     private:
          OldSquarePeg* square;
          
     public:
          PegAdapter()     { square = new OldSquarePeg; }
               
          void virtual roundPegOperation()     {
               add some corners;
               square->squarePegOperation();
               }
     }
Doc 21, Flyweight and Adapter Slide # 7

The Adridecoxegate Pattern?


The structure may be similar, but the intent is different

This makes the details differ

Adapter uses an old class in a new situation
Changes the interface of an existing object

Bridge separates an interface from its implementation so that they can vary easily and independently

Decorator adds functionality to another object without changing its interface

Proxy just controls access to an object

Strategy encapsulates an algorithm

State encapsulates state dependent behavior