Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Refactoring/OrganizingData

From ZeroWiki
Revision as of 00:29, 27 March 2026 by Maintenance script (talk | contribs) (Repair batch-0003 pages from live compare)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Refactoring


Chapter 8 Organizing Data p169

Self Encapsulate Field p171

  • You are accessing a field directly, but the coupling to the field is becoming awkward.
    Create getting and setting methods for the field and use only those to access the field.
    private int _low, _high;
    boolean includes (int arg){
        return arg >= _low && arg <= _high;
    }
    private int _low, _high;
    boolean includes (int arg){
        return arg >= getLow() && arg <= getHigh();
    }
    int getLow() {return _low;}
    int getHigh() {return _high;}

Replace Data Value with Object p175

  • You have a data item that needs additional data or behavior.
    Turn the data item into an object.

http://zeropage.org/~reset/zb/data/ReplaceDateValueWithObject.gif

Change Value to Reference p179

  • You have a class with many equal instances that you want to replace with a single object.
    Turn the object into a reference object.

http://zeropage.org/~reset/zb/data/ChangeValueToReference.gif

Change Reference to Value p183

  • You have a reference object that is small, immutable, and awkward to manage.
    Turn it into a balue object.

http://zeropage.org/~reset/zb/data/ChangeReferenceToValue.gif

Replace Array with Object p186

  • You have an array in which certain elements mean different things.
    Replace the array with an object that has a field for each element.
    String[] row = new String[3];
    row [0] = "Liverpool";
    row [1] = "15";
    Performance row = new Performance();
    row.setName("Liverpool");
    row.setWins("15");

Duplicate Observed Data p189

  • You have domain data available only in a GUI control, and domain methods need access.
    Copy the data to a domain object. Set up an observer to synchronize the two pieces of data.

http://zeropage.org/~reset/zb/data/DuplicateObservedData.gif

Change Unidirectional Association to Bidirectional p197

  • You have two classes that need to use each other's features, but there is only a one-way link.
    Add back pointers, and change modifiers to update both sets.

http://zeropage.org/~reset/zb/data/ChangeUnidirectionalAssociationToBidirectional.gif

Change Bidirectional Association to Unidirectional p200

  • You have a two-way associational but one class no longer needs features from the other.
    Drop the unneeded end of the association.

http://zeropage.org/~reset/zb/data/ChangeBidirectionAssociationToUnidirectional.gif

Replace Magic Number with Symbolic Constant p204

  • You have a literal number with a paricular meaning.
    Crate a constant, name it after the meaning, and replace the number with it.
    double potentialEnergy(double mass, double height){
        return mass * 9.91 * height;
    }
    double potentialEnergy(double mass, double height){
        return mass * GRAVITATION_CONSTNAT * height;
    }
    static final double GRAVITATIONAL_CONSTANT = 9,81;

Encapsulate Field p206

  • There is a public field.
    Make it private and provide accessors.
    public String _name;
    private String _name;
    public String getName() {return _name;}
    public void setName(String arg) { _name = arg;}

Encapsulate Collection p208

  • A method return a collection.
    Make it return a read-only view and provide add/remove methods.

http://zeropage.org/~reset/zb/data/EncapsulateCollection.gif

Replace Record with Data Class p217

  • You need to interface with a record structure in a traditional programming environment.
    Make a dumb data object for the record.

Replace Type Code with Class p218

  • A class has a numeric type code that does not affect its behavior.
    Replace the number with a new class.

http://zeropage.org/~reset/zb/data/ReplaceTypeCodeWithClass.gif

Replace Type Code with Subclasses p223

  • You have an immutable type code that affects the bahavior of a class.
    Replace the type code with subclasses.

http://zeropage.org/~reset/zb/data/ReplaceTypeCodeWithSubclasses.gif

Replace Type code with State/Strategy p227

  • You have a type code that affects the behavior of a class, but you cannot use subclassing.
    REplace the type code with a state object.

http://zeropage.org/~reset/zb/data/ReplaceTypeCodeWithStateStrategy.gif

Replace Subclass with Fields p232

  • You have subclasses that vary only in methods that return constant data.
    Change the mehtods to superclass fields and eliminate the subclasses.

http://zeropage.org/~reset/zb/data/ReplaceSubclassWithFields.gif


Refactoring