????С????????????????????????

????????????????????汾??????????е???????????????????????????汾?? :D

?????????????template?汾?????????????????????SubTemplate1??SubTemplate2??template()??д?????????????AbstractTemplate??template()????SomeDependency??AnotherDependency?????????????????????У???????????Stub/Mock???????滻??????????????????????????????????????implementThis()???????return 1 ??return 2??

?????????????strategy?汾??????????????????UsingStrategy??д?????????Stub/Mock?滻Strategy??SomeDependency??AnotherDependency???????????Strategy1??Strategy2??д?????

?????????????strategy?汾???????????????????Щ??????????????????????????????????о???Template??????????Template??????????????????????????????

???????????????

??????Щ?????????????????????д??????羭??? service-dao????????????????????

@Transactional  ??1??
@Override
public void cancel(String orderId) {
    Order order = orderRepository.findBy(orderId);  ??2??
    order.cancel();
    orderRepository.store(order);   ??3??
}

@Transactional   ??4??
@Override
public void acknowledge(String orderId) {
    Order order = orderRepository.findBy(orderId);  ??5??
    order.acknowledge();
    orderRepository.store(order);   ??6??
}

??????δ?????????????????????????????????????????????????????Order??????????????????У?2??????5??  ?? ??3??????6??????????????????????????????????????????????????????ε????1??????4?????????д??????????????????????????????????????????????????????

@Test
public void orderIsCanceledAfterCancelling() throws Exception {
    final Order order = new OrderFixture().build();
    final String orderId = order.getId();
 
    context.checking(new Expectations() {
        {
             allowing(orderRepository).findBy(orderId);
             will(returnValue(order));
           
             oneOf(orderRepository).store(order);
        }
   });
   target.cancel(orderId);
   assertTrue(order.isCanceled());
}

@Test
public void orderIsAcknowledgedAfterAcknowledging() throws Exception {
    final Order order = new OrderFixture().build();
    final String orderId = order.getId();
 
    context.checking(new Expectations() {
        {
             allowing(orderRepository).findBy(orderId);
             will(returnValue(order));
           
             oneOf(orderRepository).store(order);
        }
   });
   target.acknowledge(orderId);
   assertTrue(order.isAcknowledged());
}