Kontekst:Fasada lub Dekorator
mam REST Usługi powiedzmy CustomerService
który na razie ma jedną metodę getCustomer(id, country)
. Teraz wymaganie jest takie, że w zależności od kraju muszę wykonywać różne logiki biznesowe, takie jak dostęp do innej bazy danych lub niektórych reguł niestandardowych, a następnie zgłaszać, że otrzymałem takie żądanie.
Po pierwsze, aby rozwiązać różne implementacje zależnie od kraju użyłem wzoru fabryczne, jak pokazano poniżej:
wspólny interfejs dla wszystkich krajów implementacje oparte
public Interface CustomerServiceHandler{
Cusomer getCustomer(String id, String country);
}
Następnie fabryka jako
public class CustomerServiceHandlerFactory{
public CustomerServiceHandler getHandler(String country){...};
}
Wdrażanie Szczegóły przy użyciu elewacji
Uwaga ta fasada jest wywoływana z klasy REST czyli CustomerService
public CustomerServiceFacade{
public Customer getCustomer(String id, String country){
//use factory to get handler and then handler.getCustomer
Customer customer = factory.getHandler(country).getCustomer(id,country);
//report the request
reportingService.report('fetch-customer',....);
return customer;
}
}
Przechodzenie przez SRP (Jednoosobowy odpowiedzialności co do zasady), to fasada nie jest osiągnięcie jednego celu. Ściąga klienta, a także informuje, że takie żądanie zostało otrzymane. Więc myślałem o wzorze dekoratora, jak następuje.
Realizacja użyciu Dekorator Wzorzec:
//this is called from Rest layer
public ReportingCustomerHandler implements CustomerServiceHandler{
//this delegate is basically the default implementation and has factory too
private CustomerServiceHandler delegate;
private ReportingService reporting;
public Customer getCustomer(String id, String country){
Customer customer = delegate.getCustomer(id, country);
reporting.report(....);
return customer;
}
}
//this is called from ReportingCustomerHandler
public DefaultCustomerServiceHandler implements CustomerServiceHandler{
private CustomerServiceHandlerFactory factory;
public Customer getCustomer(String id, String country){
//get factory object else use itself, even default implementation is provided by factory
CustomerServiceHandler handler = factory.getHandler(country);
return handler.getCustomer(id,country);
}
}
Uwaga W drugim podejściu jestem ponowne interfejs CustomerServiceHandler
(pokazany w kodzie fabrycznym) dla Reporting and Default implementations
również.
Więc jaki jest właściwy sposób, lub jaka jest alternatywa dla tego, jeśli jest coś bardziej odpowiedniego.
Druga część pytania
Co jeśli mam zachować dwa różne interfejsy to jeden CustomerServiceHandler
realizacji wdrożenia różnych krajów i jeden służyć REST warstwy. Więc jaka może być konstrukcja lub alternatywa. W tym przypadku wydaje mi się, że fasada będzie pasować.
dzięki za twoje dane wejściowe. Jeśli chodzi o raportowanie, nie chcę przenosić go do warstwy rest, ponieważ lubię utrzymywać warstwę Rest niezależną od dowolnej logiki, aby łatwo można było przełączać ten interfejs z dowolną implementacją. Co myślisz o wzorze dekoratora? – Sikorski
dekorator może działać, ale twój 'ReportingCustomerHandler' ma wywołanie zarówno dla' getCustomer' i 'report', które naśladuje" problem ", który mieliśmy w' CustomerServiceFacade', więc starałem się zaprojektować od tego jak najwięcej. –