More actions
imported>Unknown No edit summary |
(Repair MoniWiki formatting after migration) |
||
| Line 4: | Line 4: | ||
= Chapter 8 Organizing Data p169 = | = Chapter 8 Organizing Data p169 = | ||
== Self Encapsulate Field p171 == | == Self Encapsulate Field p171 == | ||
* You are accessing a field directly, but the coupling to the field is becoming awkward. | * 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.'' | ||
private int _low, _high; | private int _low, _high; | ||
boolean includes (int arg){ | boolean includes (int arg){ | ||
| Line 17: | Line 17: | ||
int getHigh() {return _high;} | int getHigh() {return _high;} | ||
== Replace Data Value with Object p175 == | == Replace Data Value with Object p175 == | ||
* You have a data item that needs additional data or behavior. | * 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 | http://zeropage.org/~reset/zb/data/ReplaceDateValueWithObject.gif | ||
== Change Value to Reference p179 == | == Change Value to Reference p179 == | ||
* You have a class with many equal instances that you want to replace with a single object. | * 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 | http://zeropage.org/~reset/zb/data/ChangeValueToReference.gif | ||
== Change Reference to Value p183 == | == Change Reference to Value p183 == | ||
* You have a reference object that is small, immutable, and awkward to manage. | * 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 | http://zeropage.org/~reset/zb/data/ChangeReferenceToValue.gif | ||
== Replace Array with Object p186 == | == 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.'' | * 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.'' | ||
String[] row = new String[3]; | String[] row = new String[3]; | ||
row [0] = "Liverpool"; | row [0] = "Liverpool"; | ||
| Line 37: | Line 37: | ||
row.setWins("15"); | row.setWins("15"); | ||
== Duplicate Observed Data p189 == | == Duplicate Observed Data p189 == | ||
* You have domain data available only in a GUI control, and domain methods need access. | * 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 | http://zeropage.org/~reset/zb/data/DuplicateObservedData.gif | ||
== Change Unidirectional Association to Bidirectional p197 == | == 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.'' | * 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 | http://zeropage.org/~reset/zb/data/ChangeUnidirectionalAssociationToBidirectional.gif | ||
== Change Bidirectional Association to Unidirectional p200 == | == 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.'' | * 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 | http://zeropage.org/~reset/zb/data/ChangeBidirectionAssociationToUnidirectional.gif | ||
== Replace Magic Number with Symbolic Constant p204 == | == Replace Magic Number with Symbolic Constant p204 == | ||
* You have a literal number with a paricular meaning. | * You have a literal number with a paricular meaning. <br /> ''Crate a constant, name it after the meaning, and replace the number with it.'' | ||
double potentialEnergy(double mass, double height){ | double potentialEnergy(double mass, double height){ | ||
return mass * 9.91 * height; | return mass * 9.91 * height; | ||
| Line 58: | Line 58: | ||
static final double GRAVITATIONAL_CONSTANT = 9,81; | static final double GRAVITATIONAL_CONSTANT = 9,81; | ||
== Encapsulate Field p206 == | == Encapsulate Field p206 == | ||
* There is a public field. | * There is a public field. <br /> ''Make it private and provide accessors.'' | ||
public String _name; | public String _name; | ||
private String _name; | private String _name; | ||
| Line 64: | Line 64: | ||
public void setName(String arg) { _name = arg;} | public void setName(String arg) { _name = arg;} | ||
== Encapsulate Collection p208 == | == Encapsulate Collection p208 == | ||
* A method return a collection. | * 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 | http://zeropage.org/~reset/zb/data/EncapsulateCollection.gif | ||
== Replace Record with Data Class p217 == | == 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.'' | * 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 == | == Replace Type Code with Class p218 == | ||
* A class has a numeric type code that does not affect its behavior. | * 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 | http://zeropage.org/~reset/zb/data/ReplaceTypeCodeWithClass.gif | ||
== Replace Type Code with Subclasses p223 == | == Replace Type Code with Subclasses p223 == | ||
* You have an immutable type code that affects the bahavior of a class. | * 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 | http://zeropage.org/~reset/zb/data/ReplaceTypeCodeWithSubclasses.gif | ||
== Replace Type code with State/Strategy p227 == | == Replace Type code with State/Strategy p227 == | ||
* You have a type code that affects the behavior of a class, but you cannot use subclassing. | * 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 | http://zeropage.org/~reset/zb/data/ReplaceTypeCodeWithStateStrategy.gif | ||
== Replace Subclass with Fields p232 == | == 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.'' | * 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 | http://zeropage.org/~reset/zb/data/ReplaceSubclassWithFields.gif | ||
---- | ---- | ||
[[Refactoring]] | [[Refactoring]] | ||
Revision as of 14:01, 26 March 2026
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