Your SlideShare is downloading. ×
重構—改善既有程式的設計(chapter 8)part 1
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

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

721
views

Published on

Published in: Education, Business

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
721
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
48
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Maybe need First name/Last name in the future.
  • Transcript

    • 1. New Hires Orientation
      Refactoring Ch8 (part 1)Mel Huang
    • 2. Outline
      8.1 Self Encapsulate Field
      8.2 Replace Data Value with Object
      8.3 Change Value to Reference
      8.4 Change Reference to Value
      8.5 Replace Array with Object
      8.6 Duplicate Observed Data
      8.7 Change Unidirectional Association to Bidirectional
      8.8 Change Bidirectional Association to Unidirectional
      Classification 5/10/2011
      2
    • 3. 8.1 Self Encapsulate Field
      Classification 5/10/2011
      3
      direct access: simple!
      private int _low, _high;
      boolean includes (intarg) {
      return arg >= _low && arg <= _high;
      }
      private int _low, _high;
      boolean includes(intarg) {
      return arg >= getLow() && arg <= getHigh();
      }
      intgetLow() { return _low; }
      intgetHigh() { return _high; }
      intsetLow(int low) { _low = low; }
      intsetHigh(int high) { _high = high; }
      getter/setter: flexible!
    • 4. 8.2 Replace Data Value with Object
      Classification 5/10/2011
      4
      class Customer {
      public Customer (String name) {
      _name = name;
      }
      public String getName() {
      return _name;
      }
      private final String _name;
      }
      class Order {
      public Order (String customer) {
      _customer = new Customer(customer);
      }
      private Customer _customer;
      }
      class Order {
      public Order (String customer) {
      _customer = customer;
      }
      private String _customer;
      }
      data value
      object: easier to complicate data item(s)
    • 5. 8.3 Change Value to Reference
      Classification 5/10/2011
      5
      Order
      *
      Customer
      Order
      *

      class Customer {
      public static Customer create (String name) {
      return (Customer) _instances.get(name);
      }
      static void loadCustomers() {
      new Customer("Lemon Car Hire").store();
      new Customer("Associated Coffee Machines").store();
      ...
      }
      private void store() {
      _instances.put(this.getName(), this);
      }
      public String getNamed(String name) {
      return (Customer) _instances.get(name);
      }
      private final String _name;
      private static Dictionary _instances = new Hashtable();
      }
      class Order {
      public Order (String customer) {
      _customer = Customer.create(customer);
      }
      private Customer _customer;
      }
      Order
      Customer
      Order
      Customer

      class Customer {
      public Customer (String name) {
      _name = name;
      }
      public String getName() {
      return _name;
      }
      private final String _name;
      }
      class Order {
      public Order (String customer) {
      _customer = new Customer(customer);
      }
      private Customer _customer;
      }
    • 6. 8.4 Change Reference to Value
      Classification 5/10/2011
      6
      class Currency {
      private String _code;
      public String getCode() {
      return _code;
      }
      private Currency (String code) {
      _code = code;
      }
      }
      class Currency {
      private String _code;
      public String getCode() {
      return _code;
      }
      private Currency (String code) {
      _code = code;
      }
      public boolean equals(Object arg) {
      if (! (arginstanceof Currency))
      return false;
      Currency other = (Currency) arg;
      return (_code.equals(other._code));
      }
      public inthashCode() {
      return _code.hashCode();
      }
      }
      immutable?
    • 7. 8.5 Replace Array with Object
      Classification 5/10/2011
      7
      array: hard to remember each column
      String[] row = new String[3];
      row[0] = "Liverpool";
      row[1] = "15";
      Performance row = new Performance();
      row.setName("Liverpool");
      row.setWins("15");
      object: easy to maintain
    • 8. 8.6 Duplicate Observed Data
      Classification 5/10/2011
      8
      public class IntervalWindow extends Frame {
      TextField _startField;
      TextField _endField;
      TextField _lengthField;
      void StartField_FocusLost(FocusEvent event) {
      ...
      calculateLength();
      }
      void EndtField_FocusLost(FocusEvent event) {
      ...
      calculateLength();
      }
      void StartField_FocusLost(FocusEvent event) {
      ...
      calculateLength();
      }
      void calculateLength() {
      ...
      }
      void calculateEnd() {
      ...
      }
      }
      public class IntervalWindow extends Frame implements Observer {
      private Interval _subject;
      TextField _startField;
      TextField _endField;
      TextField _lengthField;
      public IntervalWindow() {
      _subject = new Interval();
      _subject.addObserver(this);
      update(_subject, null);
      }
      public void update(Observable observed, Object arg) {
      _endField.setText(_subject.getEnd());
      }
      String getEnd() {
      return _subject.getEnd();
      }
      void setEnd (Srtingarg) {
      _subject.setEnd(arg);
      }
      void StartField_FocusLost(FocusEvent event) {
      ...
      calculateLength();
      }
      void EndtField_FocusLost(FocusEvent event) {
      ...
      calculateLength();
      }
      void StartField_FocusLost(FocusEvent event) {
      ...
      calculateLength();
      }
      }
      class Interval extends Observable {
      void calculateLength() {
      ...
      }
      void calculateEnd() {
      ...
      }
      }
    • 9. 8.7 Change Unidirectional Association to Bidirectional
      Classification 5/10/2011
      9
      Order
      Customer
      *
      Order
      Customer
      *
      class Order {
      Customer getCustomer() {
      return _customer;
      }
      void setCustomer (Customer arg) {
      _customer = arg;
      }
      Customer _customer; // order->customer
      }
      class Customer {
      private Set _orders = new HashSet();
      }
      class Order {
      Customer getCustomer() {
      return _customer;
      }
      void setCustomer (Customer arg) {
      if (_customer != null) _customer.friendOrders().remove(this);
      _customer = arg;
      if (_customer != null) _customer.friendOrders().add(this);
      }
      Customer _customer; // order->customer
      }
      class Customer {
      private Set _orders = new HashSet();
      Set friendOrders() {
      return _orders
      }
      void addOrder(Order arg) {
      arg.setCustomer(this);
      }
      }
    • 10. 8.8 Change Bidirectional Association to Unidirectional
      Classification 5/10/2011
      10
      Order
      Customer
      *
      Order
      Customer
      *
      class Order {
      Customer getCustomer() {
      return _customer;
      }
      void setCustomer (Customer arg) {
      if (_customer != null) _customer.friendOrders().remove(this);
      _customer = arg;
      if (_customer != null) _customer.friendOrders().add(this);
      }
      double getDiscountedPrice() {
      return getGrossPrice() * (1 - _customer.getDiscount());
      }
      Customer _customer; // order->customer
      }
      class Customer {
      getPriceFor(Order order) {
      Assert.isTrue(_orders.contains(order));
      return order.getDiscountedPrice();
      }
      }
      class Order {
      Customer getCustomer() {
      return _customer;
      }
      CustomersetCustomer () {
      Iteratoriter = Customer.getInstances().iterator();
      while (iter.hasNext()) {
      Customer each = (Customer)iter.next();
      if (each.containsOrder(this)) return each;
      }
      return null;
      }
      double getDiscountedPrice(Customer customer) {
      return getGrossPrice() * (1 - customer.getDiscount());
      }
      Customer _customer; // order->customer
      }
      class Customer {
      getPriceFor(Order order) {
      Assert.isTrue(_orders.contains(order));
      return order.getDiscountedPrice(this);
      }
      }
    • 11. Thank You
      Classification 5/10/2011
      11