File tree Expand file tree Collapse file tree
business-delegate/src/main/java/com/iluwatar Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33public class App {
44
55 public static void main (String [] args ) {
6- System .out .println ("Hello World!" );
6+
7+ BusinessDelegate businessDelegate = new BusinessDelegate ();
8+ businessDelegate .setServiceType (ServiceType .EJB );
9+
10+ Client client = new Client (businessDelegate );
11+ client .doTask ();
12+
13+ businessDelegate .setServiceType (ServiceType .JMS );
14+ client .doTask ();
715 }
816}
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class BusinessDelegate {
4+
5+ private BusinessLookup lookupService = new BusinessLookup ();
6+ private BusinessService businessService ;
7+ private ServiceType serviceType ;
8+
9+ public void setServiceType (ServiceType serviceType ) {
10+ this .serviceType = serviceType ;
11+ }
12+
13+ public void doTask () {
14+ businessService = lookupService .getBusinessService (serviceType );
15+ businessService .doProcessing ();
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class BusinessLookup {
4+
5+ public BusinessService getBusinessService (ServiceType serviceType ) {
6+ if (serviceType .equals (ServiceType .EJB )) {
7+ return new EjbService ();
8+ } else {
9+ return new JmsService ();
10+ }
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public interface BusinessService {
4+
5+ void doProcessing ();
6+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class Client {
4+
5+ private BusinessDelegate businessDelegate ;
6+
7+ public Client (BusinessDelegate businessDelegate ) {
8+ this .businessDelegate = businessDelegate ;
9+ }
10+
11+ public void doTask () {
12+ businessDelegate .doTask ();
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class EjbService implements BusinessService {
4+
5+ @ Override
6+ public void doProcessing () {
7+ System .out .println ("EjbService is now processing" );
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class JmsService implements BusinessService {
4+
5+ @ Override
6+ public void doProcessing () {
7+ System .out .println ("JmsService is now processing" );
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public enum ServiceType {
4+
5+ EJB , JMS ;
6+ }
You can’t perform that action at this time.
0 commit comments