I have recently gone through Spring based Application Event
Liked the concept as it with event based mechanism classes are loosely coupled and unaware of dependencies which makes the system better maintainable. With this i am thinking is it a good design to use Application event(or simple events) based message instead of dependencies ?
For example :- If i want to create the customer,
Design with Application Event supporting loose coupling :-
public class CustomerEvent extends AbstractEvent {
public CustomerEvent(String eventId, Object eventContext) {
super(eventId, eventContext);
}
}
public class CustomerController implements ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
private void createCustomer(){
CustomerData customer =new CustomerData();
CustomerEvent event = new CustomerEvent("CustomerCreateEvent", customer);
applicationEventPublisher.publishEvent(event);
}
}
public class CustomerEventListener extends AbstractApplicationListener<CustomerEvent>{
@Override
public void onEvent(CustomerEvent event) throws HCException{
if(event.getId.equals("CustomerCreateEvent"))
// call Dao and store data in DB
}
}
Design with usual dependencies (tight coupling) :-
public class CustomerController implements ApplicationEventPublisherAware {
private CustomerService customerServiceImpl;
private void createCustomer(){
CustomerData customer =new CustomerData();
customerServiceImpl.createCustomer(customer);
}
}
Then service will have DAO as dependency which will further create the customer
I see first design is better than second one but i don't see it being used in most of the times. Any cons of using approach one over two ?
Aucun commentaire:
Enregistrer un commentaire