package com.thoughtworks.refactoring;

import com.thoughtworks.refactoring.module.Customer;
import com.thoughtworks.refactoring.module.Order;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ztang on 31/03/2017.
 */
public class ChangeDirectional {
    void logicCode() {
        List<Order> orders = new ArrayList();
        orders.add(newOrderWithCustomer("Smith"));
        orders.add(newOrderWithCustomer("John"));
        orders.add(newOrderWithCustomer("John"));

        // I want get the order count of John

        // 1. Add a field for the back pointer.
        // 2. Decide which class will control the association.
        // 3. Create a helper method on the noncontrolling side of the association.
        //    Name this method to clearly indicate its restricted use.
        // 4. If the existing modifier is on the controlling side, modify it to update the back points.
        // 5. If the existing modifier is on the controlled side, create a controlling
        //    method on the controlling side to call it from the existing modifier.
    }

    private Order newOrderWithCustomer(String name) {
        Order order = new Order();
        order.setCustomer(new Customer(name));
        return order;
    }
}

@@ -272,50 +272,156 @@
     
-void logicCode() %7B%0A        List%3COrder%3E ord
+private List%3COrder%3E orders;%0A    private List%3CCustomer%3E customers;%0A%0A    void ChangeDirectional() %7B%0A        orders = new ArrayList%3C%3E();%0A        custom
 ers 
@@ -435,16 +435,18 @@
 rrayList
+%3C%3E
 ();%0A    
@@ -453,51 +453,211 @@
     
-orders.add(
+Order orderOne = new Order();%0A        Order orderTwo = new Order();%0A        Order orderThree = 
 new
+ 
 Order
-WithCustomer(%22Smith%22)
+();%0A        Customer smith = new Customer(%22Smith%22);%0A        Customer john = new Customer(%22John%22
 );%0A
+%0A
     
@@ -669,45 +669,167 @@
 rder
-s.add(newOrderWithCustomer(%22J
+One.setCustomer(smith);%0A        orderTwo.setCustomer(john);%0A        orderThree.setCustomer(john);%0A%0A        customers.add(smith);%0A        customers.add(j
 ohn
-%22)
 );%0A
+%0A
     
@@ -847,44 +847,88 @@
 add(
-newOrderWithCustomer(%22John%22)
+orderOne);%0A        orders.add(orderTwo);%0A        orders.add(orderThree
 );%0A
-%0A
     
+%7D%0A%0A
     
@@ -966,20 +966,16 @@
 f John%0A%0A
-    
     // 1
@@ -1010,20 +1010,16 @@
 ointer.%0A
-    
     // 2
@@ -1073,20 +1073,16 @@
 on.%0A    
- 
-   
 // 3. Cr
@@ -1149,20 +1149,16 @@
 iation.%0A
-    
     //  
@@ -1216,20 +1216,16 @@
 ed use.%0A
-    
     // 4
@@ -1316,20 +1316,16 @@
 points.%0A
-    
     // 5
@@ -1403,20 +1403,16 @@
 ing%0A    
-  
-  
 //    me
@@ -1483,177 +1483,6 @@
 er.%0A
-    %7D%0A%0A    private Order newOrderWithCustomer(String name) %7B%0A        Order order = new Order();%0A        order.setCustomer(new Customer(name));%0A        return order;%0A    %7D%0A
 %7D%0A

[changeDirectional] reformat