Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

bridge structural pattern


// Purpose.  Bridge design pattern demo
// 
// Discussion.  The motivation is to decouple the Time interface from the
// Time implementation, while still allowing the abstraction and the
// realization to each be modelled with their own inheritance hierarchy.
// The implementation classes below are straight-forward.  The interface
// classes are a little more subtle.  Routinely, a Bridge pattern
// interface hierarchy "hasa" implementation class.  Here the interface
// base class "hasa" a pointer to the implementation base class, and each
// class in the interface hierarchy is responsible for populating the base
// class pointer with the correct concrete implementation class.  Then all
// requests from the client are simply delegated by the interface class to
// the encapsulated implementation class.

#include <iostream.h>
#include <iomanip.h>
#include <string.h>

class TimeImp {
public:
   TimeImp( int hr, int min ) {
      hr_ = hr;  min_ = min; }
   virtual void tell() {
      cout << "time is " << setw(2) << setfill(48) << hr_ << min_ << endl; }
protected:
   int hr_, min_;
};

class CivilianTimeImp : public TimeImp {
public:
   CivilianTimeImp( int hr, int min, int pm ) : TimeImp( hr, min ) {
      if (pm)
         strcpy( whichM_, " PM" );
      else
         strcpy( whichM_, " AM" ); }
   /* virtual */ void tell() {
      cout << "time is " << hr_ << ":" << min_ << whichM_ << endl; }
protected:
   char  whichM_[4];
};

class ZuluTimeImp : public TimeImp {
public:
   ZuluTimeImp( int hr, int min, int zone ) : TimeImp( hr, min ) {
      if (zone == 5)
         strcpy( zone_, " Eastern Standard Time" );
      else if (zone == 6)
         strcpy( zone_, " Central Standard Time" ); }
   /* virtual */ void tell() {
      cout << "time is " << setw(2) << setfill(48) << hr_ << min_ 
         << zone_ << endl; }
protected:
   char  zone_[30];
};

class Time {
public:
   Time() { }
   Time( int hr, int min ) {
      imp_ = new TimeImp( hr, min ); }
   virtual void tell() {
      imp_->tell(); }
protected:
   TimeImp*  imp_;
};

class CivilianTime : public Time {
public:
   CivilianTime( int hr, int min, int pm ) {
      imp_ = new CivilianTimeImp( hr, min, pm ); }
};

class ZuluTime : public Time {
public:
   ZuluTime( int hr, int min, int zone ) {
      imp_ = new ZuluTimeImp( hr, min, zone ); }
};


void main() {
   Time*  times[3];
   times[0] = new Time( 14, 30 );
   times[1] = new CivilianTime( 2, 30, 1 );
   times[2] = new ZuluTime( 14, 30, 6 );
   for (int i=0; i < 3; i++)
      times[i]->tell();
}

// time is 1430
// time is 2:30 PM
// time is 1430 Central Standard Time

Proxy Structural Pattern


// Purpose.  Direct coupling, lots of start-up and shut-down overhead

#include <iostream>
#include <string>
using namespace std;

class Image {
   int        id;
   static int next;
public:
   Image() { id = next++;  cout << "   $$ ctor: "<< id << '\n'; }
   ~Image()              { cout << "   dtor: " << id << '\n'; }
   void draw()           { cout << "   drawing image " << id << '\n'; }
};
int Image::next = 1;

void main( void ) {
   Image images[5];
   int   i;

   cout << "Exit[0], Image[1-5]: ";
   cin >> i;
   while (i) {
      images[i-1].draw();
      cout << "Exit[0], Image[1-5]: ";
      cin >> i;
}  }

//    $$ ctor: 1
//    $$ ctor: 2
//    $$ ctor: 3
//    $$ ctor: 4
//    $$ ctor: 5
// Exit[0], Image[1-5]: 2
//    drawing image 2
// Exit[0], Image[1-5]: 4
//    drawing image 4
// Exit[0], Image[1-5]: 2
//    drawing image 2
// Exit[0], Image[1-5]: 0
//    dtor: 5
//    dtor: 4
//    dtor: 3
//    dtor: 2
//    dtor: 1




// Purpose.  Proxy design pattern

// 1. Design an "extra level of indirection" wrapper class
// 2. The wrapper class holds a pointer to the real class
// 3. The pointer is initialized to null
// 4. When a request comes in, the real object is created "on first use"
//    (aka lazy intialization)
// 5. The request is always delegated

class RealImage {
   int  id;
public:
   RealImage( int i ) { id = i;  cout << "   $$ ctor: "<< id << '\n'; }
   ~RealImage()                { cout << "   dtor: " << id << '\n'; }
   void draw()                 { cout << "   drawing image " << id << '\n'; }
};

// 1. Design an "extra level of indirection" wrapper class
class Image {
   // 2. The wrapper class holds a pointer to the real class
   RealImage* theRealThing;
   int        id;
   static int next;
public:
   Image()  { id = next++;  theRealThing = 0; }  // 3. Initialized to null
   ~Image() { delete theRealThing; }
   void draw() {
      // 4. When a request comes in, the real object is created "on first use"
      if ( ! theRealThing) theRealThing = new RealImage( id );
      // 5. The request is always delegated
      theRealThing->draw();
}  };
int Image::next = 1;

void main( void ) {
   Image images[5];
   int   i;

   cout << "Exit[0], Image[1-5]: ";
   cin >> i;
   while (i) {
      images[i-1].draw();
      cout << "Exit[0], Image[1-5]: ";
      cin >> i;
}  }

// Exit[0], Image[1-5]: 2
//    $$ ctor: 2
//    drawing image 2
// Exit[0], Image[1-5]: 4
//    $$ ctor: 4
//    drawing image 4
// Exit[0], Image[1-5]: 2
//    drawing image 2
// Exit[0], Image[1-5]: 4
//    drawing image 4
// Exit[0], Image[1-5]: 0
//    dtor: 4
//    dtor: 2

Flyweight Structural Pattern

// Purpose.  Flyweight                  #include <iostream.h>
//
// Discussion.  Trying to use objects   const int X = 6;
// at very low levels of granularity    const int Y = 10;
// is nice, but the overhead may be
// prohibitive.  Flyweight suggests     class Gazillion {
// removing the non-shareable state     public:
// from the class, and having the cli-     Gazillion( int in ) {
// ent supply it when methods are             val1_ = in;
// called.  This places more respon-          cout << "ctor: "<< val1_<<endl; }
// sibility on the client, but, con-       ~Gazillion() {
// siderably fewer instances of the           cout << val1_ << ' '; }
// Flyweight class are now created.        void report( int in ) {
// Sharing of these instances is fa-          cout << val1_ << in << ' '; }
// cilitated by introducing a Factory   private:
// class that maintains a "cache" of       int  val1_;
// existing Flyweights.                 };
//
// In this example, the "X" state is    class Factory {
// considered shareable (within each    public:
// row anyways), and the "Y" state has     static Gazillion* getFly(int in) {
// been externalized (it is supplied          if ( ! pool_[in])
// by the client when report() is                pool_[in] =
// called).                                              new Gazillion( in );
                                              return pool_[in];
#include <iostream.h>                      }
                                           static void cleanUp() {
const int X = 6;                              cout << "dtors: ";
const int Y = 10;                             for (int i=0; i < X; i++)
                                                 if (pool_[i])
class Gazillion {                                   delete pool_[i];
public:                                       cout << endl;
   Gazillion() {                           }
      val1_ = num_ / Y;                 private:
      val2_ = num_ % Y;                    static Gazillion*  pool_[X];
      num_++;                           };
   }
   void report() {                      Gazillion*  Factory::pool_[]  = {
      cout << val1_ << val2_ << ' ';                             0,0,0,0,0,0 };
   }
private:                                void main( void )
   int    val1_;                        {
   int    val2_;                           for (int i=0; i < X; i++)
   static int num_;                        {
};                                            for (int j=0; j < Y; j++)
                                                 Factory::getFly(i)->report(j);
int Gazillion::num_ = 0;                      cout << endl;
                                           }
void main( void )                          Factory::cleanUp();
{                                       }
   Gazillion  matrix[X][Y];
   for (int i=0; i < X; i++)            // ctor: 0
   {                                    // 00 01 02 03 04 05 06 07 08 09
      for (int j=0; j < Y; j++)         // ctor: 1
         matrix[i][j].report();         // 10 11 12 13 14 15 16 17 18 19
      cout << endl;                     // ctor: 2
   }                                    // 20 21 22 23 24 25 26 27 28 29
}                                       // ctor: 3
                                        // 30 31 32 33 34 35 36 37 38 39
// 00 01 02 03 04 05 06 07 08 09        // ctor: 4
// 10 11 12 13 14 15 16 17 18 19        // 40 41 42 43 44 45 46 47 48 49
// 20 21 22 23 24 25 26 27 28 29        // ctor: 5
// 30 31 32 33 34 35 36 37 38 39        // 50 51 52 53 54 55 56 57 58 59
// 40 41 42 43 44 45 46 47 48 49        // dtors: 0 1 2 3 4 5
// 50 51 52 53 54 55 56 57 58 59