Design Architecture/Refactoring

Replace Data Value with Object

lipnus 2022. 4. 20. 08:38
반응형

데이터의 기능이 복잡해지면 객체로 만든다.

 

 

 

Before

public class ReplaceDataValueWithObject_before {

	static public class Order {
	    private String customer;
	    
	    public Order(String customer) {
	        this.customer = customer;
	    }
	    
	    public String getCustomer() {
	        return this.customer;
	    }
	    
	    public void setCustomer(String arg) {
	        this.customer = arg;
	    }
	}
	
	public static void main(String[] args) {
		Order order = new Order("david");
		
		System.out.println(order.getCustomer());
	}

}

 

After

public class ReplaceDataValueWithObject_before {

	static public class Order {
	    private Customer customer;
	    
	    public Order(String customer) {
	        this.customer = new Customer(customer);
	    }
	    
	    public String getCustomer() {
	        return this.customer.toString();
	    }
	    
//	    public void setCustomer(String arg) {
//	        this.customer = arg;
//	    }
	}

	static class Customer {
		String value;

		Customer(String value) {
			this.value = value;
		}

		public String toString() {
			return value;
		}
	}
	
	public static void main(String[] args) {
		Order order = new Order("david");
		
		System.out.println(order.getCustomer());
	}

}
반응형

'Design Architecture > Refactoring' 카테고리의 다른 글

Replace Nested Conditional With Guard Clauses  (0) 2022.04.20
Decompose Conditional  (0) 2022.04.20
Replace Type Code with Subclass  (0) 2022.04.20
Replace type Code with Class  (0) 2022.04.20
Encapsulate Collection  (0) 2022.04.20