Composition 예제

HEP·2023년 1월 4일
0

OOP & Design Pattern

목록 보기
2/5

IPaymentSystem

인터페이스

public interface IPaymentSystem {
    Boolean processPayment();
}

DebitCardPaymentSystem.cls

인터페이스 구현

public with sharing class DebitCardPaymentSystem implements IPaymentSystem{
    public Boolean processPayment() {
        System.debug('processing a debit card');
        return true;
    }
}

ShoppingCart.cls

public with sharing class ShoppingCart {
    IPaymentSystem paymentSystem;
    public ShoppingCart(IPaymentSystem paymentSystem){
        this.paymentSystem = paymentSystem;
    }
    public void processPayment(){
        this.paymentSystem.processPayment();
    }
    public void shipOrder(){
        System.debug('Order is Shipped');
    }
}

실행

IPaymentSystem debit = new DebitCardPaymentSystem();
ShoppingCart cart = new ShoppingCart(debit);
debit.processPayment();
profile
셀포합니다

0개의 댓글