Upcoming SlideShare
×

重構—改善既有程式的設計（chapter 8）part 1

1,034 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,034
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
60
0
Likes
1
Embeds 0
No embeds

No notes for slide
• Maybe need First name/Last name in the future.
• 重構—改善既有程式的設計（chapter 8）part 1

1. 1. New Hires Orientation<br />Refactoring Ch8 (part 1)Mel Huang<br />
2. 2. Outline<br />8.1 Self Encapsulate Field<br />8.2 Replace Data Value with Object<br />8.3 Change Value to Reference<br />8.4 Change Reference to Value<br />8.5 Replace Array with Object<br />8.6 Duplicate Observed Data<br />8.7 Change Unidirectional Association to Bidirectional<br />8.8 Change Bidirectional Association to Unidirectional<br />Classification 5/10/2011<br />2<br />
3. 3. 8.1 Self Encapsulate Field<br />Classification 5/10/2011<br />3<br />direct access: simple!<br />private int _low, _high;<br />boolean includes (intarg) {<br /> return arg >= _low && arg <= _high;<br />}<br />private int _low, _high;<br />boolean includes(intarg) {<br /> return arg >= getLow() && arg <= getHigh();<br />}<br />intgetLow() { return _low; }<br />intgetHigh() { return _high; }<br />intsetLow(int low) { _low = low; }<br />intsetHigh(int high) { _high = high; }<br />getter/setter: flexible!<br />
4. 4. 8.2 Replace Data Value with Object<br />Classification 5/10/2011<br />4<br />class Customer {<br /> public Customer (String name) {<br /> _name = name;<br /> }<br /> public String getName() {<br /> return _name;<br /> }<br /> private final String _name;<br />}<br />class Order {<br /> public Order (String customer) {<br />_customer = new Customer(customer);<br /> }<br /> private Customer _customer;<br />}<br />class Order {<br /> public Order (String customer) {<br />_customer = customer;<br /> }<br /> private String _customer;<br />}<br />data value<br />object: easier to complicate data item(s)<br />
5. 5. 8.3 Change Value to Reference<br />Classification 5/10/2011<br />5<br />Order<br />*<br />Customer<br />Order<br />*<br />…<br />class Customer {<br />public static Customer create (String name) {<br /> return (Customer) _instances.get(name);<br /> }<br /> static void loadCustomers() {<br /> new Customer("Lemon Car Hire").store();<br /> new Customer("Associated Coffee Machines").store();<br /> ...<br /> }<br /> private void store() {<br /> _instances.put(this.getName(), this);<br /> }<br /> public String getNamed(String name) {<br /> return (Customer) _instances.get(name);<br /> }<br /> private final String _name;<br />private static Dictionary _instances = new Hashtable();<br />}<br />class Order {<br /> public Order (String customer) {<br /> _customer = Customer.create(customer);<br /> }<br /> private Customer _customer;<br />}<br />Order<br />Customer<br />Order<br />Customer<br />…<br />class Customer {<br /> public Customer (String name) {<br /> _name = name;<br /> }<br /> public String getName() {<br /> return _name;<br /> }<br /> private final String _name;<br />}<br />class Order {<br /> public Order (String customer) {<br /> _customer = new Customer(customer);<br /> }<br /> private Customer _customer;<br />}<br />
6. 6. 8.4 Change Reference to Value<br />Classification 5/10/2011<br />6<br />class Currency {<br /> private String _code;<br /> public String getCode() {<br /> return _code;<br /> }<br /> private Currency (String code) {<br /> _code = code;<br /> }<br />}<br />class Currency {<br /> private String _code;<br /> public String getCode() {<br /> return _code;<br /> }<br /> private Currency (String code) {<br /> _code = code;<br /> }<br />public boolean equals(Object arg) {<br /> if (! (arginstanceof Currency))<br /> return false;<br /> Currency other = (Currency) arg;<br /> return (_code.equals(other._code));<br /> }<br /> public inthashCode() {<br /> return _code.hashCode();<br /> }<br />}<br />immutable?<br />
7. 7. 8.5 Replace Array with Object<br />Classification 5/10/2011<br />7<br />array: hard to remember each column<br />String[] row = new String[3];<br />row[0] = "Liverpool";<br />row[1] = "15";<br />Performance row = new Performance();<br />row.setName("Liverpool");<br />row.setWins("15");<br />object: easy to maintain<br />
8. 8. 8.6 Duplicate Observed Data<br />Classification 5/10/2011<br />8<br />public class IntervalWindow extends Frame {<br />TextField _startField;<br />TextField _endField;<br />TextField _lengthField;<br /> void StartField_FocusLost(FocusEvent event) {<br /> ...<br />calculateLength();<br /> }<br /> void EndtField_FocusLost(FocusEvent event) {<br /> ...<br />calculateLength();<br /> }<br /> void StartField_FocusLost(FocusEvent event) {<br /> ...<br />calculateLength();<br /> }<br /> void calculateLength() {<br /> ...<br /> }<br /> void calculateEnd() {<br /> ...<br /> }<br />}<br />public class IntervalWindow extends Frame implements Observer {<br />private Interval _subject;<br />TextField _startField;<br />TextField _endField;<br />TextField _lengthField;<br /> public IntervalWindow() {<br /> _subject = new Interval();<br /> _subject.addObserver(this);<br /> update(_subject, null);<br /> }<br />public void update(Observable observed, Object arg) {<br /> _endField.setText(_subject.getEnd());<br /> }<br /> String getEnd() {<br /> return _subject.getEnd();<br /> }<br /> void setEnd (Srtingarg) {<br /> _subject.setEnd(arg);<br /> }<br /> void StartField_FocusLost(FocusEvent event) {<br /> ...<br />calculateLength();<br /> }<br /> void EndtField_FocusLost(FocusEvent event) {<br /> ...<br />calculateLength();<br /> }<br /> void StartField_FocusLost(FocusEvent event) {<br /> ...<br />calculateLength();<br /> }<br />}<br />class Interval extends Observable {<br /> void calculateLength() {<br /> ...<br /> }<br /> void calculateEnd() {<br /> ...<br /> }<br />}<br />
9. 9. 8.7 Change Unidirectional Association to Bidirectional<br />Classification 5/10/2011<br />9<br />Order<br />Customer<br />*<br />Order<br />Customer<br />*<br />class Order {<br /> Customer getCustomer() {<br /> return _customer;<br /> }<br /> void setCustomer (Customer arg) {<br /> _customer = arg;<br /> }<br /> Customer _customer; // order->customer<br />}<br />class Customer {<br /> private Set _orders = new HashSet();<br />}<br />class Order {<br /> Customer getCustomer() {<br /> return _customer;<br /> }<br /> void setCustomer (Customer arg) {<br />if (_customer != null) _customer.friendOrders().remove(this);<br /> _customer = arg;<br /> if (_customer != null) _customer.friendOrders().add(this);<br /> }<br /> Customer _customer; // order->customer<br />}<br />class Customer {<br /> private Set _orders = new HashSet();<br />Set friendOrders() {<br /> return _orders<br /> }<br /> void addOrder(Order arg) {<br />arg.setCustomer(this);<br /> }<br />}<br />
10. 10. 8.8 Change Bidirectional Association to Unidirectional<br />Classification 5/10/2011<br />10<br />Order<br />Customer<br />*<br />Order<br />Customer<br />*<br />class Order {<br /> Customer getCustomer() {<br /> return _customer;<br /> }<br /> void setCustomer (Customer arg) {<br /> if (_customer != null) _customer.friendOrders().remove(this);<br /> _customer = arg;<br /> if (_customer != null) _customer.friendOrders().add(this);<br /> }<br /> double getDiscountedPrice() {<br /> return getGrossPrice() * (1 - _customer.getDiscount());<br /> }<br /> Customer _customer; // order->customer<br />}<br />class Customer {<br />getPriceFor(Order order) {<br />Assert.isTrue(_orders.contains(order));<br /> return order.getDiscountedPrice();<br /> }<br />}<br />class Order {<br /> Customer getCustomer() {<br /> return _customer;<br /> }<br />CustomersetCustomer () {<br />Iteratoriter = Customer.getInstances().iterator();<br /> while (iter.hasNext()) {<br /> Customer each = (Customer)iter.next();<br /> if (each.containsOrder(this)) return each;<br /> }<br /> return null;<br /> }<br /> double getDiscountedPrice(Customer customer) {<br /> return getGrossPrice() * (1 - customer.getDiscount());<br /> }<br /> Customer _customer; // order->customer<br />}<br />class Customer {<br />getPriceFor(Order order) {<br />Assert.isTrue(_orders.contains(order));<br /> return order.getDiscountedPrice(this);<br /> }<br />}<br />
11. 11. Thank You<br />Classification 5/10/2011<br />11<br />