등급별 고객 혜택을 제공하는 쇼핑몰 시스템 만들기
Benefit.class
public interface Benefit {
void getBenefit(GoldMember member);
void getBenefit(VipMember member);
}
DiscountBenefit.class
public class DiscountBenefit implements Benefit {
@Override
public void getBenefit(GoldMember member) {
System.out.println("Discount for Gold Member");
}
@Override
public void getBenefit(VipMember member) {
System.out.println("Discount for Vip Member");
}
}
PointBenefit.class
public class PointBenefit implements Benefit {
@Override
public void getBenefit(GoldMember member) {
System.out.println("Point for Gold Member");
}
@Override
public void getBenefit(VipMember member) {
System.out.println("Point for Vip Member");
}
}
Member.class
public interface Member {
void getBenefit(Benefit benefit);
}
GoldMember.class
public class GoldMember implements Member {
@Override
public void getBenefit(Benefit benefit) {
benefit.getBenefit(this);
}
}
VipMember.class
public class VipMember implements Member {
@Override
public void getBenefit(Benefit benefit) {
benefit.getBenefit(this);
}
}
benefit.getBenefit(this);
만 추가하면 된다.Main.class
public static void main(String[] args) {
Member goldMember = new GoldMember();
Member vipMember = new VipMember();
Benefit pointBenefit = new PointBenefit();
Benefit discountBenefit = new DiscountBenefit();
goldMember.getBenefit(pointBenefit);
vipMember.getBenefit(pointBenefit);
goldMember.getBenefit(discountBenefit);
vipMember.getBenefit(discountBenefit);
}
실행결과
>>>Point for Gold Member
>>>Point for Vip Member
>>>Discount for Gold Member
>>>Discount for Vip Member
참조: