/**
* Created by Panda on 2018/6/10.
*/
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
private Implementation1(){}
@Override
public void method1() {
System.out.println("Implementation1 method1()");
}
@Override
public void method2() {
System.out.println("Implementation1 method2()");
}
public static ServiceFactory factory=new ServiceFactory() {
@Override
public Service getService() {
return new Implementation1();
}
};
}
class Implementation2 implements Service{
private Implementation2(){}
public void method1() {
System.out.println("Implementation2 method1()");
}
@Override
public void method2() {
System.out.println("Implementation1 method2()");
}
public static ServiceFactory factory=new ServiceFactory() {
@Override
public Service getService() {
return new Implementation2();
}
};
}
public class Factories {
public static void serviceConsumer(ServiceFactory serviceFactory){
Service service = serviceFactory.getService();
service.method1();
service.method2();
}
public static void main(String[] args) {
serviceConsumer(Implementation1.factory);
serviceConsumer(Implementation2.factory);
}
}
/**
* Created by Panda on 2018/6/10.
*/
interface Game{boolean move();}
interface GameFactory{Game getGame();}
class Checks implements Game{
private Checks(){}
private int moves=0;
private static final int MOVES=3;
@Override
public boolean move() {
System.out.println("Checks move "+moves);
return ++moves!=MOVES;
}
public static GameFactory factory = new GameFactory() {
@Override
public Game getGame() {
return new Checks();
}
};
}
class Chess implements Game{
private Chess(){}
private int moves=0;
private static final int MOVES=4;
@Override
public boolean move() {
System.out.println("Chess move "+moves);
return ++moves!=MOVES;
}
public static GameFactory factory = new GameFactory() {
@Override
public Game getGame() {
return new Chess();
}
};
}
public class Games {
public static void playGame(GameFactory gameFactory){
Game game = gameFactory.getGame();
while (game.move());
}
public static void main(String[] args) {
playGame(Checks.factory);
playGame(Chess.factory);
}
}