U E D R , A S I H C RSS

Refactoring/Organizing Data

Refactoring

1. Chapter 8 Organizing Data p169

1.1. 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.

~cpp 
    private int _low, _high;
    boolean includes (int arg){
        return arg >= _low && arg <= _high;
    }

~cpp 
    private int _low, _high;
    boolean includes (int arg){
        return arg >= getLow() && arg <= getHigh();
    }
    int getLow() {return _low;}
    int getHigh() {return _high;}

1.2. Replace Data Value with Object p175

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


1.3. 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.


1.4. Change Reference to Value p183

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


1.5. 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.

~cpp 
    String[] row = new String[3];
    row [0] = "Liverpool";
    row [1] = "15";
~cpp 
    Performance row = new Performance();
    row.setName("Liverpool");
    row.setWins("15");

1.6. 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.


1.7. 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.


1.8. 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.


1.9. 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.

~cpp 
    double potentialEnergy(double mass, double height){
        return mass * 9.91 * height;
    }
~cpp 
    double potentialEnergy(double mass, double height){
        return mass * GRAVITATION_CONSTNAT * height;
    }
    static final double GRAVITATIONAL_CONSTANT = 9,81;

1.10. Encapsulate Field p206

  • There is a public field.
    Make it private and provide accessors.

~cpp 
    public String _name;
~cpp 
    private String _name;
    public String getName() {return _name;}
    public void setName(String arg) { _name = arg;}

1.11. Encapsulate Collection p208

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


1.12. 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.

1.13. 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.


1.14. 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.


1.15. 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.


1.16. 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.



Refactoring
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:53
Processing time 0.0199 sec