猿问
下载APP

工厂模式。何时使用工厂方法?

工厂模式。何时使用工厂方法?

什么时候在对象中使用工厂方法而不是Factory类是个好主意?



茅侃侃
浏览 44回答 3
3回答

慕容4345310

UML来自在此输入图像描述Product:它定义Factory方法创建的对象的接口。ConcreteProduct:实现产品界面创建者:声明工厂方法ConcreateCreator: 实现Factory方法以返回ConcreteProduct的实例问题陈述:使用工厂方法创建游戏工厂,工厂方法定义游戏界面。代码段:import java.util.HashMap;/* Product interface as per UML diagram */interface Game{&nbsp; &nbsp; /* createGame is a complex method, which executes a sequence of game steps */&nbsp; &nbsp; public void createGame();}/* ConcreteProduct implementation as per UML diagram */class Chess implements Game{&nbsp; &nbsp; public Chess(){&nbsp; &nbsp; }&nbsp; &nbsp; public void createGame(){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---------------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Create Chess game");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Opponents:2");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Define 64 blocks");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Place 16 pieces for White opponent");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Place 16 pieces for Black opponent");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Start Chess game");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---------------------------------------");&nbsp; &nbsp; }}class Checkers implements Game{&nbsp; &nbsp; public Checkers(){&nbsp; &nbsp; }&nbsp; &nbsp; public void createGame(){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---------------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Create Checkers game");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Opponents:2 or 3 or 4 or 6");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("For each opponent, place 10 coins");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Start Checkers game");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---------------------------------------");&nbsp; &nbsp; }}class Ludo implements Game{&nbsp; &nbsp; public Ludo(){&nbsp; &nbsp; }&nbsp; &nbsp; public void createGame(){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---------------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Create Ludo game");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Opponents:2 or 3 or 4");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("For each opponent, place 4 coins");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Create two dices with numbers from 1-6");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Start Ludo game");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---------------------------------------");&nbsp; &nbsp; }}/* Creator interface as per UML diagram */interface IGameFactory {&nbsp; &nbsp; public Game getGame(String gameName);}/* ConcreteCreator implementation as per UML diagram */class GameFactory implements IGameFactory {&nbsp; &nbsp; &nbsp;HashMap<String,Game> games = new HashMap<String,Game>();&nbsp; &nbsp; /*&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Since Game Creation is complex process, we don't want to create game using new operator every time.&nbsp; &nbsp; &nbsp; &nbsp; Instead we create Game only once and store it in Factory. When client request a specific game,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Game object is returned from Factory instead of creating new Game on the fly, which is time consuming&nbsp; &nbsp; */&nbsp; &nbsp; public GameFactory(){&nbsp; &nbsp; &nbsp; &nbsp; games.put(Chess.class.getName(),new Chess());&nbsp; &nbsp; &nbsp; &nbsp; games.put(Checkers.class.getName(),new Checkers());&nbsp; &nbsp; &nbsp; &nbsp; games.put(Ludo.class.getName(),new Ludo());&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public Game getGame(String gameName){&nbsp; &nbsp; &nbsp; &nbsp; return games.get(gameName);&nbsp; &nbsp; }}public class NonStaticFactoryDemo{&nbsp; &nbsp; public static void main(String args[]){&nbsp; &nbsp; &nbsp; &nbsp; if ( args.length < 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Usage: java FactoryDemo gameName");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; GameFactory factory = new GameFactory();&nbsp; &nbsp; &nbsp; &nbsp; Game game = factory.getGame(args[0]);&nbsp; &nbsp; &nbsp; &nbsp; if ( game != null ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; game.createGame();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Game="+game.getClass().getName());&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(args[0]+&nbsp; " Game does not exists in factory");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }}输出:java NonStaticFactoryDemo Chess---------------------------------------Create Chess gameOpponents:2Define 64 blocksPlace 16 pieces for White opponentPlace 16 pieces for Black opponentStart Chess game---------------------------------------Game=Chess此示例Factory通过实现a来显示一个类FactoryMethod。Game是所有类型游戏的界面。它定义了复杂的方法:createGame()Chess, Ludo, Checkers&nbsp;是游戏的不同变体,提供实现&nbsp;createGame()public Game getGame(String gameName)是FactoryMethod在IGameFactory类GameFactory在构造函数中预先创建不同类型的游戏。它实现了IGameFactory工厂方法。游戏名称作为命令行参数传递给&nbsp;NotStaticFactoryDemogetGamein&nbsp;GameFactory接受游戏名称并返回相应的Game对象。厂:创建对象而不将实例化逻辑暴露给客户端。FactoryMethod定义用于创建对象的接口,但让子类决定实例化哪个类。Factory方法允许类将实例化延迟到子类使用案例:何时使用:Client不知道在运行时需要创建哪些具体类,但只是想获得一个可以完成工作的类。

慕盖茨4494581

任何将对象创建推迟到其需要使用的对象的子类的类都可以看作是Factory模式的一个示例。

MMMHUHU

如果你想在使用方面创建一个不同的对象。它是有益的。public&nbsp;class&nbsp;factoryMethodPattern&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;String&nbsp;planName&nbsp;=&nbsp;"COMMERCIALPLAN"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;int&nbsp;units&nbsp;=&nbsp;3; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String&nbsp;args[])&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetPlanFactory&nbsp;planFactory&nbsp;=&nbsp;new&nbsp;GetPlanFactory(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Plan&nbsp;p&nbsp;=&nbsp;planFactory.getPlan(planName); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print("Bill&nbsp;amount&nbsp;for&nbsp;"&nbsp;+&nbsp;planName&nbsp;+&nbsp;"&nbsp;of&nbsp;&nbsp;"&nbsp;+&nbsp;units &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;"&nbsp;units&nbsp;is:&nbsp;"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.getRate(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.calculateBill(units); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}abstract&nbsp;class&nbsp;Plan&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;double&nbsp;rate; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;abstract&nbsp;void&nbsp;getRate(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;calculateBill(int&nbsp;units)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(units&nbsp;*&nbsp;rate); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}class&nbsp;DomesticPlan&nbsp;extends&nbsp;Plan&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;@override &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;getRate()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rate&nbsp;=&nbsp;3.50; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}class&nbsp;CommercialPlan&nbsp;extends&nbsp;Plan&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;@override &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;getRate()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rate&nbsp;=&nbsp;7.50; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}class&nbsp;InstitutionalPlan&nbsp;extends&nbsp;Plan&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;@override &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;getRate()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rate&nbsp;=&nbsp;5.50; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}class&nbsp;GetPlanFactory&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;use&nbsp;getPlan&nbsp;method&nbsp;to&nbsp;get&nbsp;object&nbsp;of&nbsp;type&nbsp;Plan &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Plan&nbsp;getPlan(String&nbsp;planType)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(planType&nbsp;==&nbsp;null)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(planType.equalsIgnoreCase("DOMESTICPLAN"))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;DomesticPlan(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(planType.equalsIgnoreCase("COMMERCIALPLAN"))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;CommercialPlan(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(planType.equalsIgnoreCase("INSTITUTIONALPLAN"))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;InstitutionalPlan(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答