工厂模式包括以下几步:
①、使用一个接口规定工厂模式生产的类型(Game)。
②、使用一个接口规定工厂模式的工厂类型(GameFactory)。
③、按需使用多个类去实现工厂模式生产的类型(类Checkers、类Chess),同时在这些类中都必须持有工厂类的static引用,并且使用匿名内部类去实现(public static GameFactory factory = new GameFactory()…)。
④、一个类似框架的方法,专门与工厂类打交道(playGame(GameFactory factory))。
⑤、测试类(public class Game)。
// 使用匿名内部类简化工厂模式//接口Game,实际上它规定了工厂生产什么类型interface Game{//方法move()boolean move();}//接口GameFactory,实际上它代表的是一个工厂专门生产某种类型,在这里是类型Gameinterface GameFactory{//方法getGame(),返回类型为Game,这是工厂的生产方法Game getGame();}//接下来按照给定的接口类型创建具体的类,再将它放入工厂中,以便工厂能生产出来。class Checkers implements Game{//统一由工厂模式生产,所以构造方法不对外开放.//private的构造方法private Checkers(){}//private的int类型的类变量moves,初始化值为0private int moves = 0;//private static final的int类型的类变量MOVES,//初始化值为3private static final int MOVES = 3;//重写方法move(),返回类型为booleanpublic boolean move(){ //ture继续进行,False//打印字符串"Checkers move" + movesSystem.out.println("Checkers move" + moves);//返回表达式++moves是否不等于MOVES//这里之所以是++moves是因为在调用方法move()的时候//需要变量move在原先的基础上自增1return ++moves != MOVES;}//static的GameFactory类型的类变量factory,//通过匿名内部类给它赋值了一个GameFactory的对象实例public static GameFactory factory = new GameFactory(){//实现方法getGame()public Game getGame(){//返回一个类Checkers的对象实例return new Checkers();}};}//接下来按照给定的接口类型创建具体的类,再将它放入工厂//中,以便工厂能生产出来。class Chess implements Game{//统一由工厂模式生产,所以构造方法不对外开放.//private的构造方法private Chess(){}//private的int类型的类变量moves,初始化值为0private int moves = 0;//private static final的int类型的类变量MOVES,//初始化值为3private static final int MOVES = 4;//重写方法move(),返回类型为booleanpublic boolean move(){//打印字符串"Chess move" + movesSystem.out.println("Chess move" + moves);//返回表达式++moves是否不等于MOVES//这里之所以是++moves是因为在调用方法move()的时候//需要变量move在原先的基础上自增1return ++moves != MOVES;}//static的GameFactory类型的类变量factory,//通过匿名内部类给它赋值了一个GameFactory的对象实例public static GameFactory factory = new GameFactory(){//实现方法getGame()public Game getGame(){//返回一个类Chess的对象实例return new Chess();}};}//测试类Gamepublic class GameDemo{//类似框架的方法playGame(GameFactory factory),带一个//GameFactory类型的形式参数factorypublic static void playGame(GameFactory factory){//调用方法getGame()获得Game类型的对象实例Game s = factory.getGame();//while循环,因为s.move()得出的结果是boolean类型while(s.move()){//...}}//程序执行入口main方法public static void main(String[] args){//测试工厂模式生产类Checkers的对象playGame(Checkers.factory);//测试工厂模式生产类Chess的对象playGame(Chess.factory);}}
// 匿名内部类优化工厂设计模式//接口Serviceinterface Service{//方法method1()void method1();//方法method2()void method2();}//接口ServiceFactory,代表用来产生Service类型的工厂interface ServiceFactory{//方法getService(),返回类型为Service类型Service getService();}//类Implementation1实现接口Serviceclass Implementation1 implements Service{//private的构造方法private Implementation1() {}//实现方法method1()public void method1(){//打印字符串"Implementation1 method1"System.out.println("Implementation1 method1");}//实现方法method2()public void method2(){//打印字符串"Implementation1 method2"System.out.println("Implementation1 method2");}//static修饰的ServiceFactory类型的类变量factory.//创建了一个ServiceFactory类型的匿名内部类复制给factorypublic static ServiceFactory factory = newServiceFactory(){//实现了方法getService()public Service getService(){//实际返回的是类Implementation1的对象实例return new Implementation1();}};}//类Implementation2实现接口Serviceclass Implementation2 implements Service{//private的构造方法private Implementation2() {}//实现方法method1()public void method1(){//打印字符串"Implementation2 method1"System.out.println("Implementation2 method1");}//实现方法method2()public void method2(){//打印字符串"Implementation2 method2"System.out.println("Implementation2 method2");}//static修饰的ServiceFactory类型的类变量factory.//创建了一个ServiceFactory类型的匿名内部类复制给factorypublic static ServiceFactory factory = newServiceFactory(){//实现了方法getService()public Service getService(){//实际返回的是类Implementation2的对象实例return new Implementation2();}};}//测试类Factoriespublic class Factories{//固定方法serviceConsumer(ServiceFactory fact),带一个//ServiceFactory类型的形式参数fact,实际上这就是一个类似//框架的方法,至于父接口打交道,但是实际传入的可以是它的子类对象public static void serviceConsumer(ServiceFactory fact){//调用方法(工厂方法)获得一个Service类型,赋值给//Service类型的变量sService s = fact.getService();//调用方法method1()s.method1();//调用方法method2()s.method2();}//程序执行入口main方法public static void main(String[] args){//调用方法serviceConsumer(ServiceFactory fact),//实际传入的参数是Implementation1.factory,它返回//一个类Implementation1的对象实例。serviceConsumer(Implementation1.factory);//同上serviceConsumer(Implementation2.factory);}}
在上面的代码示例中,类Implementation1和类Implementation2的构造方法都是private的,并且没有任何必要去创建作为工厂的具名类(直接匿名内部类就行了):
