728x90 반응형 컴포짓패턴2 2021.12.09_컴포짓패턴04.자바와스프링에서찾아보는패턴 package me.whiteship.designpatterns._02_structural_patterns._08_composite._03_java; import javax.swing.*; public class SwingExample { public static void main(String[] args) { JFrame frame = new JFrame(); JTextField textField = new JTextField(); textField.setBounds(200, 200, 200, 40); frame.add(textField); JButton button = new JButton("click"); button.setBounds(200, 100, 60, 40); button.addAction.. 2021. 12. 9. 2021.12.09_컴포짓패턴02.패턴적용하기 지난 코드 베이스 컴포짓 적용하기 Component public interface Component{ int getPrice(); } 공통된 오퍼레이션이 정의되어야하는것이 핵심 Item public class Item implements Component{ private String name; private int price; public Item(String name, int price){ this.name = name; this.price = price; } @Override public int getPrice(){ return this.price; } } Bag before 앞에서 말한것 처럼 leaf타입을 참조하면 안되고 compoent를 참조해야함 그래서 아래와 같이해야함 public Bag i.. 2021. 12. 9. 이전 1 다음 728x90 반응형