기본버거 콜라 감자튀김을 만든다

package composite;

import lombok.Data;

@Data // getter, setter 생성
public class Burger {
	private int price;
	private String desc;

	public Burger() {
		this(1500, "기본버거");
	}

	public Burger(int price, String desc) {
		this.price = price;
		this.desc = desc;
		System.out.println(desc + "가 만들어졌습니다.");
	}
}
package composite;

import lombok.Data;

@Data
public class Coke {
	private int price;
	private String desc;
	
	public Coke() {
		this(1500, "코카콜라");
	}
	
	public Coke(int price, String desc) {
		this.price = price;
		this.desc = desc;
		System.out.println(desc + "가 만들어졌습니다.");
	}
}
package composite;

import lombok.Data;

@Data
public class FrenchFried {
	private int price;
	private String desc;
	
	public FrenchFried() {
		this(2000, "감자칩");
	}
	
	public FrenchFried(int price, String desc) {
		this.price = price;
		this.desc = desc;
		System.out.println(desc + "이 만들어졌습니다.");
	}
}

 

버거를 상속 받아 빅버거와 쉬림프버거를 만든다

package composite;

public class BigBurger extends Burger {
	public BigBurger() {
		super(4000, "빅버거");
	}
	
	public BigBurger(int price, String desc) {
		super(price, desc);
	}
}
package composite;

public class ShrimpBurger extends Burger {

	public ShrimpBurger() {
		super(3500, "쉬림프버거");
	}

	public ShrimpBurger(int price, String desc) {
		super(price, desc);
	}
}

 

 

 

콤포지션

- 상속이 아닌 만들어 놓은 클래스를 가져오는 것

 

빅버거, 쉬림프버거, 감자튀김, 콜라 를 이용해서 세트를 만들기

package composite;

import lombok.Data;

// 자바는 다중 상속이 안됨 (부모가 여럿이 될 수 없음)

@Data
public class BigBurgerSet {
	// 콤포지션(중요) - 상속이 아닌 만들어 놓은 클래스를 가져오는 것
	private BigBurger bigBurger;
	private Coke coke;
	private FrenchFried frenchFried;
	
	public BigBurgerSet() {
		this(new BigBurger(), new Coke(), new FrenchFried());
	}

	public BigBurgerSet(BigBurger bigBurger, Coke coke, FrenchFried frenchFried) {
		this.bigBurger = bigBurger;
		this.coke = coke;
		this.frenchFried = frenchFried;
	}

}
package composite;

import lombok.Data;

@Data
public class ShrimpBurgerSet {

	// 콤포지션(중요) - 상속이 아닌 만들어 놓은 클래스를 가져오는 것
	private ShrimpBurger shrimpBurger;
	private Coke coke;
	private FrenchFried frenchFried;

	public ShrimpBurgerSet() {
			this(new ShrimpBurger(), new Coke(), new FrenchFried());
		}

	public ShrimpBurgerSet(ShrimpBurger shrimpBurger, Coke coke, FrenchFried frenchFried) {
			this.shrimpBurger = shrimpBurger;
			this.coke = coke;
			this.frenchFried = frenchFried;

		}

}

 

실행 파일을 만들어서 실행해보기

package composite;

public class LotteriaApp {
	public static void main(String[] args) {
		// 빅버거 세트
		BigBurgerSet set1 = new BigBurgerSet();
		
		System.out.println();
		
		// 기본버거
		Burger burger1 = new Burger();
		
		System.out.println();
		
		// 콜라
		Coke coke1 = new Coke();
		
		System.out.println();
		
		
		// BigBurgerSet
		BigBurgerSet set2 = new BigBurgerSet(
				new BigBurger(3000, "빅버거할인"),
				new Coke(),
				new FrenchFried()	
				);
		
		System.out.println();
		
		BigBurgerSet set3 = new BigBurgerSet(
				new BigBurger(2000, "빅버거할인"),
				new Coke(),
				new FrenchFried()
				);
		
		System.out.println();
		
		
		// 새우버거 세트 추가요
		ShrimpBurgerSet set4 = new ShrimpBurgerSet();
		
		System.out.println();
	}
}

 

생성자 오버로딩

package composite;

import lombok.Data;

@Data
public class ShrimpBurgerSet {

	// 콤포지션(중요) - 상속이 아닌 만들어 놓은 클래스를 가져오는 것
	private ShrimpBurger shrimpBurger;
	private Coke coke;
	private FrenchFried frenchFried;
	
	public ShrimpBurgerSet() {
		this(new ShrimpBurger(), new Coke(), new FrenchFried());
	}
	
	public ShrimpBurgerSet(ShrimpBurger shrimpBurger) {
		this(shrimpBurger, new Coke(), new FrenchFried());
	}
	
	public ShrimpBurgerSet(Coke coke) {
		this(new ShrimpBurger(), coke, new FrenchFried());
	}
	
	public ShrimpBurgerSet(FrenchFried frenchFried) {
		this(new ShrimpBurger(), new Coke(), frenchFried);
	}
	
	public ShrimpBurgerSet(ShrimpBurger shrimpBurger, Coke coke) {
		this(shrimpBurger, coke, new FrenchFried());
	}
	
	public ShrimpBurgerSet(ShrimpBurger shrimpBurger, FrenchFried frenchFried) {
		this(shrimpBurger, new Coke(), frenchFried);
	}
	
	public ShrimpBurgerSet(Coke coke, FrenchFried frenchFried) {
		this(new ShrimpBurger(), coke, frenchFried);
	}
	
	
	// 생성자가 제일 많은 것이 기준
	public ShrimpBurgerSet(ShrimpBurger shrimpBurger, Coke coke, FrenchFried frenchFried) {
		this.shrimpBurger = shrimpBurger;
		this.coke = coke;
		this.frenchFried = frenchFried;
	}
	
	public static void main(String[] args) {
		ShrimpBurgerSet s1 = new ShrimpBurgerSet(new Coke(), new FrenchFried());
	}
}

 

+ Recent posts