Demo entry 2689386

ifcA

   

Submitted by anonymous on Sep 15, 2015 at 21:34
Language: Java. Code size: 2.1 kB.

public interface ServiceA {
  String call();
}

public interface ServiceB {
  String call();
}

public class MyObject {

  private final ServiceA serviceA;
  private final ServiceB serviceB;
  
  ...

  public String doStuff() {
    return serviceA.call() + serviceB.call();
  }
}

public class MyObject {

  private final ServiceA serviceA;
  private final ServiceB serviceB;
  
  public MyObject() {
    this.serviceA = new ServiceAFactory.getService();
    this.serviceB = new ServiceBFactory.getService();
  }

  public String doStuff() {
    return serviceA.call() + serviceB.call();
  }
}

public class MyObject {

  private final ServiceA serviceA;
  private final ServiceB serviceB;
  
  public MyObject() {
    this.serviceA = ServiceLocator.getServiceA();
    this.serviceB = ServiceLocator.getServiceB();
  }

  public String doStuff() {
    return serviceA.call() + serviceB.call();
  }
}

public class MyObject {

  private final ServiceA serviceA;
  private final ServiceB serviceB;
  
  public MyObject() {
    this.serviceA = ServiceLocator.getService(ServiceA.class);
    this.serviceB = ServiceLocator.getService(ServiceB.class);
  }

  public String doStuff() {
    return serviceA.call() + serviceB.call();
  }
}

public class MyObjectFactory {
  ServiceAFactory svcAFact;
  ServiceBFactory svcBFact;

  MyObject build() {
    return new MyObject(svcAFact.build(), svcBFact.build());
  }
}

public class MyObject {

  private final ServiceA serviceA;
  private final ServiceB serviceB;
  
  @Inject
  public MyObject(ServiceA serviceA, ServiceB serviceB) {
    this.serviceA = serviceA;
    this.serviceB = serviceB;
  }

  public String doStuff() {
    return serviceA.call() + serviceB.call();
  }
}

public class App() {

  public static void main(String args[]) {
    ServiceA serviceA = new ServiceAImpl();
	ServiceB serviceB = new ServiceBImpl();
	MyObject myObject = new MyObject(serviceA, serviceB);
	
	System.out.println(myObject.doStuff());
  }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).