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

Chapter 4: A Standard Object-Oriented Solution
Code Examples

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

TOP

// segment of code that instantiates the features
// no error checking provided--for illustration 
// purposes only
public class V1Model extends Model {
    static public Model buildV1Model (String modelName) {
        V1Model myModel= new V1Model();
        myModel.modelNumber= myModel.openModel( modelName);
        if (myModel.modelNumber <= 0) return null;
        myModel.buildModel();
        return myModel;
    }
    private void 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( modelNumber, i);
            switch( V1.getFeatureType( modelNumber, ID)) {
                case FEATURE_SLOT:
                    features[i]= new V1Slot( modelNumber, ID);
                    break;
                case FEATURE_HOLE:
                    features[i]= new V1Hole( modelNumber, ID);
                    break;
                // Other cases
            }
        }
    }  
}

Example 4-2: Implementation of V1 Methods

TOP

// myModelNumber and myID are private members containing
// information about the model and feature (in V1) this 
// feature corresponds to
public class V1Slot extends SlotFeature {
    . . .
    public double getX1 () {
        return V1.getX1forSlot( myModelNumber, myID);
    }
    public double getX2 () {
        return V1.getX2forSlot( myModelNumber, myID);
    }
}
public class V1Hole extends HoleFeature {
    . . .
    public double getXLoc () {
        return V1.getXforHole( myModelNumber, myID);
    } 
}

Example 4-3: Instantiating the V2 Features

TOP

// segment of code that instantiates the features
// no error checking provided--for illustration 
// purposes only
public class V2Model extends Model {
    static public Model buildV2Model (String modelName) {
        // open model
        V2Model myModel= new V2Model();
        if (!myModel.openModel( modelName)) return null;
        myModel.buildModel();
        return myModel;
    }
    private void 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;
        // 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 OOG_SLOT:
                    features[i]= new V2Slot( oogF);
                    break;
                case OOG_HOLE:
                    features[i]= new V2Hole( oogF);
                    break;
                // other cases
            }
        }
    }
}

Example 4-4: Implementation of V2 Methods

TOP

// oogF is a reference to the feature object in V2 that 
// the object containing it corresponds to
public class V2Slot extends SlotFeature {
    . . . 
    public double getX1 () throws Exception {
        // call appropriate method on oogF to 
        // get needed information. 
       return myOogF.getX1Loc();
    } 
    public double getX2 () throws Exception {
        // call appropriate method on oogF to
        // get needed information. 
        return myOogF.getX2Loc();
    }
} 
public class V2Hole extends HoleFeature {
    . . .
    public double getXLoc () throws Exception {
        // call appropriate method on oogF to 
        // get needed information. 
        return myOogF.getXLoc();
    } 
}