工厂模式运用-锻造武器
目标:使用一些材料,在一座城市打造出一把武器
包含元素:
一、锻造材料:
1、 源生材料: 源生火焰 源生空气
源生法力
源生之土
2、 矿石: 氪金锭 恒金锭
3、 药水: 超级活力药水 魔能法力药水
二、锻造成品:
1、 双手锤: 雷霆
2、 单手锤: 龙拳之锤
二、锻造地点:
1、奥格瑞玛
2、暴风城
3、沙塔斯
主要逻辑:1、锻造一把武器需要不同数量的材料:雷霆需要3种材料(源生材料、矿石、药水)
龙拳之锤需要2种材料(源生材料、矿石)
2、不同城市锻造武器需要的材料不同:例如:奥格瑞玛(氪金、原生空气、超级活力药水)
沙塔斯(恒金、原生之火、魔能法力药水)
3、运用抽象工厂模式和工厂方法模式
主要代码:
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 抽象工厂接口,负责创建原材料
- * 运用抽象工厂模式
- * 原材料包括 矿石类:Ore
- * 源生类:Primal
- * 药水类:Potion
- */
- public interface CityIngredientFactory {
- public Primal createPrimal();
- public Ore createOre();
- public Potion createPotion();
- }
/* * 抽象工厂接口,负责创建原材料 * 运用抽象工厂模式 * 原材料包括 矿石类:Ore * 源生类:Primal * 药水类:Potion */ public interface CityIngredientFactory { public Primal createPrimal(); public Ore createOre(); public Potion createPotion(); }
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 实现抽象材料工厂
- * 在暴风城锻造城所需要的材料
- * 氪金 + 原生之火 + 超级活力药水
- */
- public class CityStormwindFactory implements CityIngredientFactory {
- //氪金
- public Ore createOre() {
- return new OreKrypton();
- }
- //原生之火
- public Primal createPrimal() {
- return new PrimalMana();
- }
- //超级活力药水
- public Potion createPotion() {
- return new PotionRed();
- }
- }
/* * 实现抽象材料工厂 * 在暴风城锻造城所需要的材料 * 氪金 + 原生之火 + 超级活力药水 */ public class CityStormwindFactory implements CityIngredientFactory { //氪金 public Ore createOre() { return new OreKrypton(); } //原生之火 public Primal createPrimal() { return new PrimalMana(); } //超级活力药水 public Potion createPotion() { return new PotionRed(); } }
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 实现抽象材料工厂
- * 在沙塔斯锻造城所需要的材料
- */
- public class CityShattrathFactory implements CityIngredientFactory {
- //恒金
- public Ore createOre() {
- return new OreEternium();
- }
- //原生之火
- public Primal createPrimal() {
- return new PrimalFire();
- }
- //魔能法力药水
- public Potion createPotion() {
- return new PotionGreen();
- }
- }
/* * 实现抽象材料工厂 * 在沙塔斯锻造城所需要的材料 */ public class CityShattrathFactory implements CityIngredientFactory { //恒金 public Ore createOre() { return new OreEternium(); } //原生之火 public Primal createPrimal() { return new PrimalFire(); } //魔能法力药水 public Potion createPotion() { return new PotionGreen(); } }
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 实现抽象材料工厂
- * 在奥格瑞玛锻造所需要的材料
- */
- public class CityOrgrimmarFactory implements CityIngredientFactory {
- //氪金
- public Ore createOre() {
- return new OreKrypton();
- }
- //原生空气
- public Primal createPrimal() {
- return new PrimalAir();
- }
- //超级活力药水
- public Potion createPotion() {
- return new PotionRed();
- }
- }
/* * 实现抽象材料工厂 * 在奥格瑞玛锻造所需要的材料 */ public class CityOrgrimmarFactory implements CityIngredientFactory { //氪金 public Ore createOre() { return new OreKrypton(); } //原生空气 public Primal createPrimal() { return new PrimalAir(); } //超级活力药水 public Potion createPotion() { return new PotionRed(); } }
以上工厂类已经就绪。
下面开始写武器类
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 工厂类已经就绪,准备武器用到的材料
- * 要锻造的武器的抽象基类
- */
- public abstract class Weapon {
- protected String weaponName;
- //制作武器将用到的原料
- protected Ore ore;
- protected Primal primal;
- protected Potion potion;
- //抽象方法用来收集来自“材料工厂”的材料,在子类中实现
- public abstract void prepare();
- public void makeUp()
- {
- System.out.println("==========准备锻造==========");
- }
- public void make()
- {
- System.out.println("==========开始锻造==========");
- System.out.println("放入材料:");
- System.out.println(" " + ore.toString());
- System.out.println(" " + primal.toString());
- if(null!=potion){
- System.out.println(" " + potion.toString());
- }
- }
- public void makeEnd()
- {
- System.out.println("锻造成品:");
- System.out.println(" " + this.getWeaponName());
- System.out.println("==========锻造结束==========");
- }
- public String getWeaponName() {
- return weaponName;
- }
- public void setWeaponName(String weaponName) {
- this.weaponName = weaponName;
- }
- }
/* * 工厂类已经就绪,准备武器用到的材料 * 要锻造的武器的抽象基类 */ public abstract class Weapon { protected String weaponName; //制作武器将用到的原料 protected Ore ore; protected Primal primal; protected Potion potion; //抽象方法用来收集来自“材料工厂”的材料,在子类中实现 public abstract void prepare(); public void makeUp() { System.out.println("==========准备锻造=========="); } public void make() { System.out.println("==========开始锻造=========="); System.out.println("放入材料:"); System.out.println(" " + ore.toString()); System.out.println(" " + primal.toString()); if(null!=potion){ System.out.println(" " + potion.toString()); } } public void makeEnd() { System.out.println("锻造成品:"); System.out.println(" " + this.getWeaponName()); System.out.println("==========锻造结束=========="); } public String getWeaponName() { return weaponName; } public void setWeaponName(String weaponName) { this.weaponName = weaponName; } }
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 继承自武器的抽象基类
- * 锻造-龙拳之锤-
- */
- public class WeaponDragon extends Weapon{
- CityIngredientFactory cityIngredientFactory;
- //构造函数中取得一个工厂并储存到一个实例变量中
- public WeaponDragon(CityIngredientFactory cityIngredientFactory)
- {
- this.cityIngredientFactory = cityIngredientFactory;
- }
- //覆盖父类方法,获得“材料工厂”的对应的材料对象
- @Override
- public void prepare() {
- //从材料工厂得到正确的材料
- //龙拳之锤需要2种材料
- this.ore = cityIngredientFactory.createOre();
- this.primal = cityIngredientFactory.createPrimal();
- this.weaponName = "龙拳之锤";
- }
- }
/* * 继承自武器的抽象基类 * 锻造-龙拳之锤- */ public class WeaponDragon extends Weapon{ CityIngredientFactory cityIngredientFactory; //构造函数中取得一个工厂并储存到一个实例变量中 public WeaponDragon(CityIngredientFactory cityIngredientFactory) { this.cityIngredientFactory = cityIngredientFactory; } //覆盖父类方法,获得“材料工厂”的对应的材料对象 @Override public void prepare() { //从材料工厂得到正确的材料 //龙拳之锤需要2种材料 this.ore = cityIngredientFactory.createOre(); this.primal = cityIngredientFactory.createPrimal(); this.weaponName = "龙拳之锤"; } }
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 继承自武器的抽象基类
- * 锻造-雷霆-
- */
- public class WeaponWrath extends Weapon {
- CityIngredientFactory cityIngredientFactory;
- //构造函数中取得一个工厂并储存到一个实例变量中
- public WeaponWrath(CityIngredientFactory cityIngredientFactory)
- {
- this.cityIngredientFactory = cityIngredientFactory;
- }
- @Override
- public void prepare() {
- //从材料工厂得到正确的材料
- //雷霆需要3种材料
- this.ore = cityIngredientFactory.createOre();
- this.primal = cityIngredientFactory.createPrimal();
- this.potion = cityIngredientFactory.createPotion();
- this.weaponName = "雷霆";
- }
- }
/* * 继承自武器的抽象基类 * 锻造-雷霆- */ public class WeaponWrath extends Weapon { CityIngredientFactory cityIngredientFactory; //构造函数中取得一个工厂并储存到一个实例变量中 public WeaponWrath(CityIngredientFactory cityIngredientFactory) { this.cityIngredientFactory = cityIngredientFactory; } @Override public void prepare() { //从材料工厂得到正确的材料 //雷霆需要3种材料 this.ore = cityIngredientFactory.createOre(); this.primal = cityIngredientFactory.createPrimal(); this.potion = cityIngredientFactory.createPotion(); this.weaponName = "雷霆"; } }
下面开始打造武器。
在不同的城市、打造需要的材料不同
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 抽象基类
- */
- public abstract class ForgeWeapon {
- //武器的实例
- Weapon weapon;
- public void forgeWeaponNow(String weaponType)
- {
- weapon = createforgeWeapon(weaponType);
- weapon.prepare();
- //锻造步骤
- weapon.makeUp();
- weapon.make();
- weapon.makeEnd();
- }
- /*
- * 实例化在子类进行 运用了工厂方法模式
- */
- protected abstract Weapon createforgeWeapon(String city);
- }
/* * 抽象基类 */ public abstract class ForgeWeapon { //武器的实例 Weapon weapon; public void forgeWeaponNow(String weaponType) { weapon = createforgeWeapon(weaponType); weapon.prepare(); //锻造步骤 weapon.makeUp(); weapon.make(); weapon.makeEnd(); } /* * 实例化在子类进行 运用了工厂方法模式 */ protected abstract Weapon createforgeWeapon(String city); }
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 奥瑞格玛锻造
- */
- public class OrgrimmarForgeWeapom extends ForgeWeapon {
- @Override
- protected Weapon createforgeWeapon(String type) {
- Weapon weapon;
- //因为是在奥瑞格玛锻造,所以实例化一个奥瑞格玛的材料工厂
- CityIngredientFactory cityIngredientFactory = new CityOrgrimmarFactory();
- System.out.println("==============您在奥格瑞玛锻造============");
- //根据传入参数判断锻造哪种武器
- if(type == "Dragon" || "Dragon".equals(type))
- {
- weapon = new WeaponDragon(cityIngredientFactory);
- return weapon;
- }
- else if(type == "Wrath" || "Wrath".equals(type))
- {
- weapon = new WeaponWrath(cityIngredientFactory);
- return weapon;
- }
- return null;
- }
- }
/* * 奥瑞格玛锻造 */ public class OrgrimmarForgeWeapom extends ForgeWeapon { @Override protected Weapon createforgeWeapon(String type) { Weapon weapon; //因为是在奥瑞格玛锻造,所以实例化一个奥瑞格玛的材料工厂 CityIngredientFactory cityIngredientFactory = new CityOrgrimmarFactory(); System.out.println("==============您在奥格瑞玛锻造============"); //根据传入参数判断锻造哪种武器 if(type == "Dragon" || "Dragon".equals(type)) { weapon = new WeaponDragon(cityIngredientFactory); return weapon; } else if(type == "Wrath" || "Wrath".equals(type)) { weapon = new WeaponWrath(cityIngredientFactory); return weapon; } return null; } }
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 沙塔斯城锻造
- */
- public class ShattrathForgeWeapom extends ForgeWeapon {
- @Override
- protected Weapon createforgeWeapon(String type) {
- Weapon weapon;
- //因为是在沙塔斯城锻造,所以实例化一个沙塔斯城的材料工厂
- CityIngredientFactory cityIngredientFactory = new CityShattrathFactory();
- System.out.println("==============您在沙塔斯城锻造============");
- //根据传入参数判断锻造哪种武器
- if(type == "Dragon" || "Dragon".equals(type))
- {
- weapon = new WeaponDragon(cityIngredientFactory);
- return weapon;
- }
- else if(type == "Wrath" || "Wrath".equals(type))
- {
- weapon = new WeaponWrath(cityIngredientFactory);
- return weapon;
- }
- return null;
- }
- }
/* * 沙塔斯城锻造 */ public class ShattrathForgeWeapom extends ForgeWeapon { @Override protected Weapon createforgeWeapon(String type) { Weapon weapon; //因为是在沙塔斯城锻造,所以实例化一个沙塔斯城的材料工厂 CityIngredientFactory cityIngredientFactory = new CityShattrathFactory(); System.out.println("==============您在沙塔斯城锻造============"); //根据传入参数判断锻造哪种武器 if(type == "Dragon" || "Dragon".equals(type)) { weapon = new WeaponDragon(cityIngredientFactory); return weapon; } else if(type == "Wrath" || "Wrath".equals(type)) { weapon = new WeaponWrath(cityIngredientFactory); return weapon; } return null; } }
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 暴风城锻造
- */
- public class StormwindForgeWeapom extends ForgeWeapon {
- @Override
- protected Weapon createforgeWeapon(String type) {
- Weapon weapon;
- //因为是在暴风城锻造,所以实例化一个暴风城的材料工厂
- CityIngredientFactory cityIngredientFactory = new CityStormwindFactory();
- System.out.println("==============您在暴风城锻造============");
- //根据传入参数判断锻造哪种武器
- if(type == "Dragon" || "Dragon".equals(type))
- {
- weapon = new WeaponDragon(cityIngredientFactory);
- return weapon;
- }
- else if(type == "Wrath" || "Wrath".equals(type))
- {
- weapon = new WeaponWrath(cityIngredientFactory);
- return weapon;
- }
- return null;
- }
- }
/* * 暴风城锻造 */ public class StormwindForgeWeapom extends ForgeWeapon { @Override protected Weapon createforgeWeapon(String type) { Weapon weapon; //因为是在暴风城锻造,所以实例化一个暴风城的材料工厂 CityIngredientFactory cityIngredientFactory = new CityStormwindFactory(); System.out.println("==============您在暴风城锻造============"); //根据传入参数判断锻造哪种武器 if(type == "Dragon" || "Dragon".equals(type)) { weapon = new WeaponDragon(cityIngredientFactory); return weapon; } else if(type == "Wrath" || "Wrath".equals(type)) { weapon = new WeaponWrath(cityIngredientFactory); return weapon; } return null; } }
测试类:
![](http://www.javaeye.com/images/icon_copy.gif)
- /*
- * 锻造武器测试类
- */
- public class makeWeaponTest extends TestCase {
- ForgeWeapon forgeWeapon1;
- ForgeWeapon forgeWeapon2;
- ForgeWeapon forgeWeapon3;
- ForgeWeapon forgeWeapon4;
- //初始化
- @Override
- protected void setUp() throws Exception {
- forgeWeapon1 = new OrgrimmarForgeWeapom();
- forgeWeapon2 = new ShattrathForgeWeapom();
- forgeWeapon3 = new ShattrathForgeWeapom();
- forgeWeapon4 = new StormwindForgeWeapom();
- System.out.println("*********测试开始*********");
- }
- @Override
- protected void tearDown() throws Exception {
- System.out.println("*********测试结束*********/n");
- }
- public void testforgeWeapon1()
- {
- System.out.println("测试1:奥格瑞玛锻造 龙拳之锤");
- forgeWeapon1.forgeWeaponNow("Dragon");
- }
- public void testforgeWeapon2()
- {
- System.out.println("测试2:沙塔斯城锻造 龙拳之锤");
- forgeWeapon2.forgeWeaponNow("Dragon");
- }
- public void testforgeWeapon3()
- {
- System.out.println("测试3:沙塔斯城锻造 雷霆");
- forgeWeapon3.forgeWeaponNow("Wrath");
- }
- public void testforgeWeapon4()
- {
- System.out.println("测试4:暴风城锻造 雷霆");
- forgeWeapon4.forgeWeaponNow("Wrath");
- }
- }
/* * 锻造武器测试类 */ public class makeWeaponTest extends TestCase { ForgeWeapon forgeWeapon1; ForgeWeapon forgeWeapon2; ForgeWeapon forgeWeapon3; ForgeWeapon forgeWeapon4; //初始化 @Override protected void setUp() throws Exception { forgeWeapon1 = new OrgrimmarForgeWeapom(); forgeWeapon2 = new ShattrathForgeWeapom(); forgeWeapon3 = new ShattrathForgeWeapom(); forgeWeapon4 = new StormwindForgeWeapom(); System.out.println("*********测试开始*********"); } @Override protected void tearDown() throws Exception { System.out.println("*********测试结束*********/n"); } public void testforgeWeapon1() { System.out.println("测试1:奥格瑞玛锻造 龙拳之锤"); forgeWeapon1.forgeWeaponNow("Dragon"); } public void testforgeWeapon2() { System.out.println("测试2:沙塔斯城锻造 龙拳之锤"); forgeWeapon2.forgeWeaponNow("Dragon"); } public void testforgeWeapon3() { System.out.println("测试3:沙塔斯城锻造 雷霆"); forgeWeapon3.forgeWeaponNow("Wrath"); } public void testforgeWeapon4() { System.out.println("测试4:暴风城锻造 雷霆"); forgeWeapon4.forgeWeaponNow("Wrath"); } }
测试打印:
![](http://www.javaeye.com/images/icon_copy.gif)
- *********测试开始*********
- 测试1:奥格瑞玛锻造 龙拳之锤
- ==============您在奥格瑞玛锻造============
- ==========准备锻造==========
- ==========开始锻造==========
- 放入材料:
- 氪金
- 源生空气
- 锻造成品:
- 龙拳之锤
- ==========锻造结束==========
- *********测试结束*********
- *********测试开始*********
- 测试2:沙塔斯城锻造 龙拳之锤
- ==============您在沙塔斯城锻造============
- ==========准备锻造==========
- ==========开始锻造==========
- 放入材料:
- 恒金
- 源生火焰
- 锻造成品:
- 龙拳之锤
- ==========锻造结束==========
- *********测试结束*********
- *********测试开始*********
- 测试3:沙塔斯城锻造 雷霆
- ==============您在沙塔斯城锻造============
- ==========准备锻造==========
- ==========开始锻造==========
- 放入材料:
- 恒金
- 源生火焰
- 魔能法力药水
- 锻造成品:
- 雷霆
- ==========锻造结束==========
- *********测试结束*********
- *********测试开始*********
- 测试4:暴风城锻造 雷霆
- ==============您在暴风城锻造============
- ==========准备锻造==========
- ==========开始锻造==========
- 放入材料:
- 氪金
- 源生法力
- 超级活力药水
- 锻造成品:
- 雷霆
- ==========锻造结束==========
- *********测试结束*********
*********测试开始********* 测试1:奥格瑞玛锻造 龙拳之锤 ==============您在奥格瑞玛锻造============ ==========准备锻造========== ==========开始锻造========== 放入材料: 氪金 源生空气 锻造成品: 龙拳之锤 ==========锻造结束========== *********测试结束********* *********测试开始********* 测试2:沙塔斯城锻造 龙拳之锤 ==============您在沙塔斯城锻造============ ==========准备锻造========== ==========开始锻造========== 放入材料: 恒金 源生火焰 锻造成品: 龙拳之锤 ==========锻造结束========== *********测试结束********* *********测试开始********* 测试3:沙塔斯城锻造 雷霆 ==============您在沙塔斯城锻造============ ==========准备锻造========== ==========开始锻造========== 放入材料: 恒金 源生火焰 魔能法力药水 锻造成品: 雷霆 ==========锻造结束========== *********测试结束********* *********测试开始********* 测试4:暴风城锻造 雷霆 ==============您在暴风城锻造============ ==========准备锻造========== ==========开始锻造========== 放入材料: 氪金 源生法力 超级活力药水 锻造成品: 雷霆 ==========锻造结束========== *********测试结束*********
知识点:
工厂方法模式:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个,工厂方法让类把实例化推迟到子类。
抽象工厂模式:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。(抽象工厂允许客户使用抽象的接口来创建一组相关的产品,而不需要知道或关心实际产出的具体产品是什么,这样一来,客户就从具体的产品被解耦)
工厂模式运用-锻造武器相关推荐
- 读40020072 朋友的工厂模式运用-锻造武器理解和感想
费话少说,如果还没有人知道我写这篇文章的原因,可以先点击以下这个website查看题目 http://www.iteye.com/topic/233420 以下是我的程序片断,如果我的design不太 ...
- 显示器的秘密武器——主流显示器工厂模式进入方法全析
在以前的电子产品中,如手机.播放器等,最早引入了工程模式,用户可以通过特殊的方式进入工程模式,进行更多.更深层的参数设置,在有的电子产品中甚至可以通过工程模式来改变其特性.而在电脑界,液晶显示器的工厂 ...
- 工厂模式:优雅地生成多种类型对象
工厂模式是一种非常常用的创建型设计模式,其提供了创建对象的最佳方式.在创建对象时,不会对客户端暴露对象的创建逻辑,而是通过使用共同的接口来创建对象.通过使用工厂模式,在业务代码中可以灵活的操控生成 ...
- Java:继承之super关键字,继承之私有权限,继承之方法重写,继承之object类,继承之简单工厂模式
一.继承的概念及用法 继承背后的思想就是 基于已存在的类来构建新类 当从已存在类继承时,就重用了它的方法和属性,还可以添加新的方法和属性来定制新类以应对需求 ...
- 创建型模式---工厂模式
从简单到复杂有简单工厂模式-->工厂方法模式-->抽象工厂模式 简单工厂模式(Simple Factory Pattern) 定义 又称为静态工厂方法(Static Factory Met ...
- 【Golang第8章:面向对象编程】Go语言的结构体是什么,怎么声明;Golang方法的调用和声明;go语言面向对象实例,go语言工厂模式;golang面向对象的三大特性:继承、封装、多态
介绍 这个是在B站上看边看视频边做的笔记,这一章是Glang面向对象编程 这一章内容较多,内容有Go语言的结构体是什么,怎么声明:Golang方法的调用和声明:go语言面向对象实例,go语言工厂模式: ...
- 设计模式之工厂模式(Factory)
设计模式之工厂模式 设计模式之工厂模式 简介 代码准备 需求一 需求二 简单工厂 工厂方法 抽象工厂 需求三 总结 设计模式之工厂模式 简介 工厂顾名思义就是创建产品,根据产品是具体产品还是具体工厂可 ...
- [Unity设计模式与游戏开发]工厂模式
前言 工厂模式(Factory Design Pattern)也是游戏开发中比较常用的创建型模式,一般情况下,工厂模式分为三种更加细分的类型:简单工厂.工厂方法和抽象工厂.在GoF设计模式一书中,它将 ...
- 设计模式之游戏--工厂模式详解
工厂模式 一.简单工厂模式 二.工厂方法模式 三.抽象工厂模式 使用总结 引入问题:在面向对象编程中,创建一个对象的最常用的方法是new一个对象实例,new对象操作符就是用来构造对象实例的,但是在一些 ...
最新文章
- c 语言socket粘包,C# Socket粘包处理讲解示例
- 一文盘点5种聚类算法,数据科学家必备!
- java开发环境选择
- Codeforces 766E Mahmoud and a xor trip(树形DP)
- 5.7 随机采样最小二乘法
- java验证身份证号码是否有效源代码
- linux oracle11g客户端安装
- 【转】灵活运用 SQL SERVER FOR XML PATH
- java性能、代码优化
- 深入理解JVM虚拟机读书笔记——运行时数据区
- (小柯-java- 基础)
- 等比缩放公式_iPhone屏幕适配,等比缩放
- 一个程序员的多年珍藏--收藏
- 新神魔大陆服务器维护,《新神魔大陆》手游8月20日合服维护公告
- 总结yolov5不同模型的网络结构图
- 金蝶显示访问加密服务器失败,金蝶提示“访问加密服务器失败,错误号:-16”到底如何解决?...
- 服务器维护 志愿填报时间顺延,因系统维护耽误考生填报,西藏高考志愿填报截止时间顺延两日...
- sudo gem uninstall cocoapods 报错
- 快速幂算法详解(全)
- 计算机右键管理提示没有关联应用,win10计算机右键管理 没有与之关联的程序来执行此操作的解决方法...