코드로 대화하기 – 숨기고 싶으면 보여주지 말아라~!
숨기고 싶으면 보여주지 말아라~!
class Cart { private List cartList; public Cart() { cartList = new ArrayList(); } public void addItem(String item) { if (item == null || item.isEmpty() == true) { throw new EmptyItemException("item == null || item.isEmpty() == true"); } cartList.add(item); } public void addAllItem(List<String> cartList) { if (cartList == null || cartList.size() == 0) { throw new EmptyItemException("item == null || item.isEmpty() == true"); } this.cartList.addAll(cartList); } }
cartList의 타입을 외부에 노출 시키고 싶지 말아야 할때는 확실히 숨겨야한다. 안보여서 나쁠것은 없다. 보이면 자꾸 건드리고 싶어진다.
댓글 남기기