package com.test;
interface Game {
void dosomething();
}
interface GameFactory {
Game getGame();
}
// Crap为掷骰子
System.out.println("开始掷骰子");
}
}
class CrapFactory implements GameFactory {
public Game getGame() {
return new Crap();
}
}
// TossCoin为投硬币
class TossCoin implements Game {
public void dosomething() {
System.out.println("开始投硬币");
}
}
class TossCoinFactory implements GameFactory {
public Game getGame() {
return new TossCoin();
}
}
public class FactoryExample {
public static void playGame(GameFactory factory) {
Game s = factory.getGame();
s.dosomething();
}
public static void main(String Arg[]) {
playGame(new CrapFactory());
playGame(new TossCoinFactory());
}
interface Game {
void dosomething();
}
interface GameFactory {
Game getGame();
}
// Crap为掷骰子
class Crap implements Game {
public void dosomething() {System.out.println("开始掷骰子");
}
}
class CrapFactory implements GameFactory {
public Game getGame() {
return new Crap();
}
}
// TossCoin为投硬币
class TossCoin implements Game {
public void dosomething() {
System.out.println("开始投硬币");
}
}
class TossCoinFactory implements GameFactory {
public Game getGame() {
return new TossCoin();
}
}
public class FactoryExample {
public static void playGame(GameFactory factory) {
Game s = factory.getGame();
s.dosomething();
}
public static void main(String Arg[]) {
playGame(new CrapFactory());
playGame(new TossCoinFactory());
}
}
//注意接口的实现必须为public,因为Java规定实现接口或者覆写父类方法时不能降低父类方法的访问权限