Guice學習

    Guice的github地址: https://github.com/google/guice/wiki/Motivationjava

    這個例子很簡單,若是會點Guice,那麼不須要再往下看了。git

    簡單來講,它是dependeny injection框架,它與Spring相比,做者已經給出了,見原文github

1.添加依賴

    List-1 pom.xml中添加以下依賴框架

<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>4.0</version>
</dependency>

2.定義接口及實現類

2.1 CreditCardProcessor

    List-2ide

/**
 * @author dmj1161859184@126.com 2018-09-10 20:00
 * @version 1.0
 * @since 1.0
 */
public interface CreditCardProcessor {
    ChargeResult charge(CreditCard credit, Object amount);
}

/**
 * @author dmj1161859184@126.com 2018-09-10 20:42
 * @version 1.0
 * @since 1.0
 */
public class PaypalCreditCardProcessorImpl implements CreditCardProcessor {
    @Override
    public ChargeResult charge(CreditCard credit, Object amount) {
        LOG.info("charge方法");
        return null;
    }
}

2.2 TransactionLog

    List-3測試

/**
 * @author dmj1161859184@126.com 2018-09-10 20:00
 * @version 1.0
 * @since 1.0
 */
public interface TransactionLog {
    void logChargeResult(ChargeResult result);

    void logConnectException(Exception e);
}

/**
 * @author dmj1161859184@126.com 2018-09-10 20:07
 * @version 1.0
 * @since 1.0
 */
public class DatabaseTransactionLogImpl implements TransactionLog {
    @Override
    public void logChargeResult(ChargeResult result) {
        LOG.info("logChargeResult方法");
    }

    @Override
    public void logConnectException(Exception e) {
        LOG.info("logConnectException方法");
    }
}

2.3 BillingService

    List-4ui

/**
 * @author dmj1161859184@126.com 2018-09-10 19:59
 * @version 1.0
 * @since 1.0
 */
public interface BillingService {

    Receipt chargeOrder(PizzaOrder order, CreditCard credit);
}

/**
 * @author dmj1161859184@126.com 2018-09-10 20:00
 * @version 1.0
 * @since 1.0
 */
public class BillingServiceImpl implements BillingService {
    private final CreditCardProcessor processor;
    private final TransactionLog transactionLog;

    @Inject
    public BillingServiceImpl(CreditCardProcessor processor,
                              TransactionLog transactionLog) {
        this.processor = processor;
        this.transactionLog = transactionLog;
    }

    @Override
    public Receipt chargeOrder(PizzaOrder order, CreditCard credit) {
        try {
            ChargeResult result = processor.charge(credit, order.getAmount());
            transactionLog.logChargeResult(result);

            return result.wasSuccessful()
                    ? Receipt.forSuccessfulCharge(order.getAmount())
                    : Receipt.forDeclinedCharge(result.getDeclineMessage());
        } catch (Exception e) {
            transactionLog.logConnectException(e);
            return Receipt.forSystemFailure(e.getMessage());
        }
    }
}

3.測試

    List-5this

public class Main {

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new BillingModule());
        BillingService billingService = injector.getInstance(BillingService.class);
        billingService.chargeOrder(new PizzaOrder(), new CreditCard());
    }
}

    List-5的運行結果以下圖1所示:google

                                                  圖1 List-5的運行結果spa

相關文章
相關標籤/搜索