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