Design Patterns Explained C++ Code Examples – Chapter 4: A Standard Object-Oriented Solution

Example 4-1: Instantiating the V1 Features

TOC

Example 4-2: Implementation of V1 Methods

TOC

Example 4-3: Instantiating the V2 Features

TOC

Example 4-4: Implementation of V2 Methods

TOC

Example 4-1: Instantiating the V1 Features


Model

TOP
Model.h

#pragma once
#include "Feature.h"

class Model
{
public:
    Model(void);
protected:
    Feature **features;
    int nElements;

public:
    virtual ~Model(void);
};

Model.cpp

#include "Model.h"

Model::Model(void)
{
}

Model::~Model(void)
{
}

V1Model

TOP
V1Model.h

#pragma once
#include "model.h"
#include <string>
using namespace std;

class V1Model : public Model
{
public:
Model(void);
static int openModel( string ModelName);
static Model* buildV1Model(string);
void buildModel();


private:
    void buildV1Model();
    int nElements;
    int myModelNumber;
    int getNumberOfElements();

public:
    ~V1Model(void);
};

V1Model.cpp

#include "V1Model.h"
#include "Feature.h"
#include "V1.h"
#include "V1Slot.h"
#include "V1Hole.h"

V1Model::V1Model(void)
{
}

V1Model::~V1Model(void)
{
}

// segment of code that instantiates the features
// no error checking provided--for illustration 
// purposes only

Model* V1Model::buildV1Model (string modelName) 
{
    int modNum;
    modNum= V1Model::openModel( modelName);
    if (modNum <= 0) return 0l;
	
    V1Model *myModel= new V1Model;
    myModel->myModelNumber= modNum;
    myModel->buildModel();
    return myModel;
}

void V1Model::buildModel()
{
    // each feature object needs to know the model number 
    // and feature ID it corresponds to in order to
    // retrieve information when requested. Note how 
    // this information is passed to each object’s
    // constructor
	
    nElements= getNumberOfElements();
    features= new Feature*[ nElements];
	
    // do for each feature in the model
    int i;
    int ID;
    for (i= 0; i < nElements; i++) 
    {
        // determine feature present and create 
        // appropriate feature object
        ID= V1::getFeatureID( myModelNumber, i);
        switch( V1::getFeatureType( myModelNumber, ID)) 
        {
            case FEATURE_SLOT:
                features[i]= new V1Slot( myModelNumber, ID);
	break;
	
            case FEATURE_HOLE:
                features[i]= new V1Hole( myModelNumber, ID);
                break;
             // Other cases
                }
        }
}

int V1Model::getNumberOfElements ()
{
    // get the number of Elements from V1
    // for code example, just return 1;
    return 1;
}

Feature

TOP
Feature.h

#pragma once
#include "definitions.h"
#include <string>
using namespace std;

class Feature
{
public:
    Feature(void);
public:
    virtual ~Feature(void);
};

Feature.cpp

#include "Feature.h"

Feature::Feature(void)
{
}

Feature::~Feature(void)
{
}

V1

TOP
V1.h

#pragma once
#include "definitions.h"

class V1
{
public:
    V1(void);		
    static FEATURE_TYPES getFeatureType( int, int);
    static int getFeatureID( int, int );
    static double getXforHole( int, int);
    static double getX1forSlot( int, int);
    static double getX2forSlot( int, int);

public:
    ~V1(void);
};

V1.cpp

#include "V1.h"

V1::V1(void)
{
}

V1::~V1(void)
{
}

double V1::getXforHole( int, int)
{ 
    return 0;
}
double V1::getX1forSlot( int, int)
{
    return 0;
}
double V1::getX2forSlot( int, int)
{
    return 0;
}

Example 4-2: Implementation of V1 Methods


CutoutFeature

TOP
CutoutFeature.h

#pragma once
#include "feature.h"

class CutoutFeature :
    public Feature
{
public:
    CutoutFeature(void);
public:
    ~CutoutFeature(void);
};

CutoutFeature.cpp

#include "CutoutFeature.h"

CutoutFeature::CutoutFeature(void)
{
}

CutoutFeature::~CutoutFeature(void)
{
}

HoleFeature

TOP
HoleFeature.h

#pragma once
#include "feature.h"

class HoleFeature :
    public Feature
{
public:
    HoleFeature(void);
public:
    ~HoleFeature(void);
};

HoleFeature.cpp

#include "HoleFeature.h"

HoleFeature::HoleFeature(void)
{
}

HoleFeature::~HoleFeature(void)
{
}

IrregularFeature

TOP
IrregularFeature.h

#pragma once

class IrregularFeature
{
public:
    IrregularFeature(void);
public:
    virtual ~IrregularFeature(void);
};

IrregularFeature.cpp

#include "IrregularFeature.h"

IrregularFeature::IrregularFeature(void)
{
}

IrregularFeature::~IrregularFeature(void)
{
}

SlotFeature

TOP
SlotFeature.h

#pragma once
#include "feature.h"

class SlotFeature :
    public Feature
{
public:
    SlotFeature(void);
public:
    ~SlotFeature(void);
};

SlotFeature.cpp

#include "SlotFeature.h"

SlotFeature::SlotFeature(void)
{
}

SlotFeature::~SlotFeature(void)
{
}

SpecialFeature

TOP
SpecialFeature.h

#pragma once
#include "feature.h"

class SpecialFeature :
    public Feature
{
public:
    SpecialFeature(void);
public:
    ~SpecialFeature(void);
};

SpecialFeature.cpp

#include "SpecialFeature.h"

SpecialFeature::SpecialFeature(void)
{
}

SpecialFeature::~SpecialFeature(void)
{
}

V1Cutout

TOP
V1Cutout.h

#pragma once
#include "cutoutfeature.h"

class V1Cutout :
    public CutoutFeature
{
public:
    V1Cutout(int, int);
public:
    ~V1Cutout(void);
private:
    int myModelNumber;
    int myID;
};

V1Cutout.cpp

#include "V1Cutout.h"

V1Cutout::V1Cutout(int modelNumber, int ID)
{
    myModelNumber = modelNumber;
    myID = ID;
}

V1Cutout::~V1Cutout(void)
{
}

V1Hole

TOP
V1Hole.h

#pragma once
#include "holefeature.h"

class V1Hole :
    public HoleFeature
{
public:
    V1Hole(int, int);
    double getX(); 
    
public:
    ~V1Hole(void);
private:
    int myModelNumber;
    int myID;
};

V1Hole.cpp

#include "V1Hole.h"
#include "V1.h"

V1Hole::V1Hole(int modelNumber, int ID)
{
    myModelNumber = modelNumber;
    myID = ID;
}
V1Hole::~V1Hole(void)
{
}
// myModelNumber and myID are private members containing
// information about the model and feature (in V1) this 
// feature corresponds to

double V1Hole::getX()
{
    return V1::getXforHole( myModelNumber, myID);
}

V1Irregular

TOP
V1Irregular.h

#pragma once
#include "irregularfeature.h"

class V1Irregular :
    public IrregularFeature
{
public:
    V1Irregular(int, int);
public:
    ~V1Irregular(void);
private:
    int myModelNumber;
    int myID;
};

V1Irregular.cpp

#include "V1Irregular.h"

V1Irregular::V1Irregular(int modelNumber, int ID)
{
    myModelNumber = modelNumber;
    myID = ID;
}

V1Irregular::~V1Irregular(void)
{
}

V1Slot

TOP
V1Slot.h

#pragma once
#include "slotfeature.h"

class V1Slot :
    public SlotFeature
{
public:
    V1Slot(int, int);
    double getX1();
    double getX2();
public:
    ~V1Slot(void);
private:
    int myModelNumber;
    int myID;
};

V1Slot.cpp

#include "V1Slot.h"
#include "V1.h"

V1Slot::V1Slot(int modelNumber, int ID)
{
    myModelNumber = modelNumber;
    myID = ID;
}

V1Slot::~V1Slot(void)
{
}

// myModelNumber and myID are private members containing
// information about the model and feature (in V1) this 
// feature corresponds to
double V1Slot::getX1 () 
{
    return V1::getX1forSlot( myModelNumber, myID);
}
double V1Slot::getX2 () 
{
    return V1::getX2forSlot( myModelNumber, myID);
}

V1Special

TOP
V1Special.h

#pragma once
#include "specialfeature.h"

class V1Special :
    public SpecialFeature
{
public:
    V1Special(int, int);
public:
    ~V1Special(void);
private:
    int myModelNumber;
    int myID;
};

V1Special.cpp

#include "V1Special.h"

V1Special::V1Special(int modelNumber, int ID)
{
    myModelNumber = modelNumber;
    myID = ID;
}

V1Special::~V1Special(void)
{
}

Example 4-3: Instantiating the V2 Features


V2Model

TOP
V2Model.h

#pragma once
#include "model.h"
#include <string>
using namespace std;
#include "OOGFeature.h"
#include "definitions.h"

class V2Model :
    public Model
{
public:
    V2Model(void);
    static int openModel( string ModelName);
    static Model* buildV2Model(string);
    void buildModel();
    int getNumberOfElements();
    OOGFeature *getElement(int);

public:
    ~V2Model(void);

};

V2Model.cpp

#include "V2Model.h"
#include "V2Slot.h"
#include "V2Hole.h"

V2Model::V2Model(void)
{
}

V2Model::~V2Model(void)
{
}
// segment of code that instantiates the features
// no error checking provided--for illustration 
// purposes only

Model* V2Model::buildV2Model (string modelName) 
{
    // open model
    int modNum;
    modNum= V2Model::openModel( modelName);
    if (modNum <= 0) return 0l;
    
    V2Model *myModel= new V2Model;
    if (!myModel->openModel( modelName)) return 0l;
    
    myModel->buildModel();
    return myModel;
}


void V2Model::buildModel()
{
    // each feature object needs to know the feature in the 
    // V2 system it corresponds to in order to retrieve 
    // information when requested. Note how this information
    // is passed into each object’s constructor
    
    nElements= getNumberOfElements();
    
    OOGFeature *oogF= new OOGFeature;
    
    // do for each feature in the model
    int i;
    for (i= 0; i < nElements; i++) {
        // determine feature present and create 
        // appropriate feature object
        oogF= getElement(i);
        switch( oogF->getType()) 
        {
            case OOGFEATURE_SLOT:
                features[i]= new V2Slot( oogF);
                break;
    
            case OOGFEATURE_HOLE:
                features[i]= new V2Hole( oogF);
                break;
            // other cases
        }
    }
}

int V2Model::getNumberOfElements ()
{
    // get the number of Elements from V1
    // for code example, just return 1;
    return 1;
}

V2Cutout

TOP
V2Cutout.h

#pragma once
#include "cutoutfeature.h"
#include "OOGFeature.h"

class V2Cutout :
    public CutoutFeature
{
public:
    V2Cutout(OOGFeature *feature);
public:
    ~V2Cutout(void);
private:
    OOGFeature *myOOGFeature;
};

V2Cutout.cpp

#include "V2Cutout.h"
#include "OOGFeature.h"

V2Cutout::V2Cutout(OOGFeature *feature)
{
    myOOGFeature= feature;
}

V2Cutout::~V2Cutout(void)
{
}

V2Hole

TOP
V2Hole.h

#pragma once
#include "holefeature.h"
#include "OOGFeature.h"

class V2Hole :
    public HoleFeature
{
public:
    V2Hole(OOGFeature *feature);
    double getX();
public:
    ~V2Hole(void);
private:
    OOGFeature *myOOGFeature;
};

V2Hole.cpp

#include "V2Hole.h"
#include "OOGFeature.h"

V2Hole::V2Hole(OOGFeature *feature)
{
    myOOGFeature= feature;
}

V2Hole::~V2Hole(void)
{
}
double V2Hole::getX () 
{
    return myOOGFeature->getX1();
}

V2Irregular

TOP
V2Irregular.h

#pragma once
#include "irregularfeature.h"
#include "OOGFeature.h"

class V2Irregular :
    public IrregularFeature
{
public:
    V2Irregular(OOGFeature *feature);
public:
    ~V2Irregular(void);
private:
    OOGFeature *myOOGFeature;
};

V2Irregular.cpp

#include "V2Irregular.h"
#include "OOGFeature.h"

V2Irregular::V2Irregular(OOGFeature *feature)
{
    myOOGFeature= feature;
}

V2Irregular::~V2Irregular(void)
{
}

V2Special

TOP
V2Special.h

#pragma once
#include "specialfeature.h"
#include "OOGFeature.h"

class V2Special :
    public SpecialFeature
{
public:
    V2Special(OOGFeature *feature);
public:
    ~V2Special(void);
private:
    OOGFeature *myOOGFeature;
};

V2Special.cpp

#include "V2Special.h"
#include "OOGFeature.h"

V2Special::V2Special(OOGFeature *feature)
{
    myOOGFeature= feature;
}

V2Special::~V2Special(void)

OOGFeature

TOP
OOGFeature.h

#pragma once
#include "definitions.h"

class OOGFeature
{
public:
    OOGFeature(void);
    double getX1();
    double getX2();
    OOGFEATURE_TYPES getType();

public:
    ~OOGFeature(void);
};

OOGFeature.cpp

#include "OOGFeature.h"

OOGFeature::OOGFeature(void)
{
}

OOGFeature::~OOGFeature(void)
{
}

double OOGFeature::getX1()
{
    return 0;
}
double OOGFeature::getX2()
{
    return 0;
}

Example 4-4: Implementation of V2 Methods


V2Slot

TOP
V2Slot.h

#pragma once
#include "slotfeature.h"
#include "OOGFeature.h"

class V2Slot :
    public SlotFeature
{
public:
    V2Slot(OOGFeature *feature);
    double getX1();
    double getX2();
public:
    ~V2Slot(void);
private:
    OOGFeature *myOOGFeature;
};

V2Slot.cpp

#include "V2Slot.h"
#include "OOGFeature.h"

V2Slot::V2Slot(OOGFeature *feature)
{
    myOOGFeature= feature;
}
V2Slot::~V2Slot(void)
{
}

double V2Slot::getX1 () 
{
    return myOOGFeature->getX1();
}
double V2Slot::getX2 () 
{

    return myOOGFeature->getX2();
}