["Refactoring"] ---- [[TableOfContents]] = Chapter 8 Organizing Data p169 = == Self Encapsulate Field p171 == * You are accessing a field directly, but the coupling to the field is becoming awkward. [[BR]] ''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;} }}} == Replace Data Value with Object p175 == * You have a data item that needs additional data or behavior. [[BR]] ''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. [[BR]] ''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. [[BR]] ''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. [[BR]]''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"); }}} == Duplicate Observed Data p189 == * You have domain data available only in a GUI control, and domain methods need access. [[BR]] ''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.[[BR]]''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. [[BR]]''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. [[BR]] ''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; }}} == Encapsulate Field p206 == * There is a public field. [[BR]] ''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;} }}} == Encapsulate Collection p208 == * A method return a collection. [[BR]] ''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. [[BR]]''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. [[BR]] ''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. [[BR]] ''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. [[BR]] ''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.[[BR]]''Change the mehtods to superclass fields and eliminate the subclasses.'' http://zeropage.org/~reset/zb/data/ReplaceSubclassWithFields.gif ---- ["Refactoring"]