interface and factories

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/85634993

An interface is a gateway to multiple implementations, and a typical way to produce objects that fit the interface is the Factory Method design pattern.

For example:

// interfaces/Factories.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

interface Service {
  void method1();

  void method2();
}

interface ServiceFactory {
  Service getService();
}

class Service1 implements Service {
  Service1() {} // Package access

  public void method1() {
    System.out.println("Service1 method1");
  }

  public void method2() {
    System.out.println("Service1 method2");
  }
}

class Service1Factory implements ServiceFactory {
  @Override
  public Service getService() {
    return new Service1();
  }
}

class Service2 implements Service {
  Service2() {} // Package access

  public void method1() {
    System.out.println("Service2 method1");
  }

  public void method2() {
    System.out.println("Service2 method2");
  }
}

class Service2Factory implements ServiceFactory {
  @Override
  public Service getService() {
    return new Service2();
  }
}

public class Factories {
  public static void serviceConsumer(ServiceFactory fact) {
    Service s = fact.getService();
    s.method1();
    s.method2();
  }

  public static void main(String[] args) {
    serviceConsumer(new Service1Factory());
    // Services are completely interchangeable:
    serviceConsumer(new Service2Factory());
  }
}
/* Output:
Service1 method1
Service1 method2
Service2 method1
Service2 method2
*/

Without the Factory Method, your code must somewhere specify the exact type of Service created, to call the apropriate constructor. 

Why would you add this extra level of indirection? One common reason is to create a framework.

For instance:

// interfaces/Games.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// A Game framework using Factory Methods

interface Game {
  boolean move();
}

interface GameFactory {
  Game getGame();
}

class Checkers implements Game {
  private int moves = 0;
  private static final int MOVES = 3;

  @Override
  public boolean move() {
    System.out.println("Checkers move " + moves);
    return ++moves != MOVES;
  }
}

class CheckersFactory implements GameFactory {
  @Override
  public Game getGame() {
    return new Checkers();
  }
}

class Chess implements Game {
  private int moves = 0;
  private static final int MOVES = 4;

  @Override
  public boolean move() {
    System.out.println("Chess move " + moves);
    return ++moves != MOVES;
  }
}

class ChessFactory implements GameFactory {
  @Override
  public Game getGame() {
    return new Chess();
  }
}

public class Games {
  public static void playGame(GameFactory factory) {
    Game s = factory.getGame();
    while (s.move()) {; // Separating control from the business
    }
  }

  public static void main(String[] args) {
    playGame(new CheckersFactory());
    playGame(new ChessFactory());
  }
}
/* Output:
Checkers move 0
Checkers move 1
Checkers move 2
Chess move 0
Chess move 1
Chess move 2
Chess move 3
*/

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/Factories.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/Games.java

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/85634993