1. 程式人生 > >一個故事貫穿設計模式(四)工廠模式

一個故事貫穿設計模式(四)工廠模式

   不習慣csdn的markdown編輯器。  又重新整理下。還好寫的東西不多。

   包結構:

   類結構:

   核心實現:

package com.automannn.design_mode.factory.test;

import com.automannn.design_mode.factory.OOP_class.WeaponIterator;
import com.automannn.design_mode.factory.OOP_interface.Weapon;
import com.automannn.design_mode.factory.entity_alive.LongGe;
import com.automannn.design_mode.factory.entity_alive.YuGe;
import com.automannn.design_mode.factory.entity_logicalOperation.ArmorFactory;
import com.automannn.design_mode.factory.entity_logicalOperation.SimpleGunTemplate;
import com.automannn.design_mode.factory.entity_nonliving.*;

/**
 * @author 
[email protected]
* @time 2018/9/13 15:06 */ public class Test { static LongGe longGe= new LongGe(); public static void main(String[] args) { //龍哥的適配管理器 AdapterManager am = new AdapterManager(); new Thread(am).start(); YuGe yuGe = new YuGe(); //龍哥通過迭代器的方式拿武器,於哥不知道它的車身到底有什麼武器 WeaponIterator wp = longGe.weaponItertor(); //因為武器在車上,所以龍哥可以拿來攻擊 while (wp.haseNext()){ Weapon weapon = (Weapon) wp.next(); System.out.print(longGe.getName()+": "); weapon.attack(); } //於哥有些慌張,想要去龍哥的車子搶奪刀,後者斧子,或者槍 System.out.println(yuGe.getName()+"憤然反抗,去車裡搶奪 刀,或者 槍, 或者 斧子"); //這時候,於哥不知道總共有什麼武器和多少武器,於是隨便去拿 try { //做好了拿刀的手勢,卻發現是斧子... Knife knife = (Knife) longGe.getBmw_car().getWeapon(0); }catch (ClassCastException e){ System.out.print(yuGe.getName()+": "); System.out.println("迭代器模式真是厲害,我甘拜下風。"); } //突然龍哥的刀掉了,情急之下,忘記了自己的寶馬車上有哪些武器 WeaponDescriptor descriptor= new WeaponDescriptor(60.0f,300.0f); longGe.setAdapter(new WeaponAdapter(descriptor)); longGe.setAssosation(am); longGe.AssosationDo(); //龍哥使用介面卡模式,拿到了一把槍,可是他不知道怎麼用,於是按照槍使用的模板使用 System.out.println(longGe.getName()+"通過模板模式,使用槍支:"); Weapon wpp = am.getWeapon(); try { Gun gg = (Gun) wpp; new SimpleGunTemplate().run(); }catch (ClassCastException e){ System.out.println("介面卡出錯!"); } //於哥發現龍哥欺人太甚了,想用點什麼來防禦一下,但是自己製作太費時間,於是使用工廠模式生產防彈衣 ArmorFactory armorFactory = new ArmorFactory(); armorFactory.produceWithCahce(); yuGe.setArmor((Armor) armorFactory.getProduct()); System.out.print(yuGe.getName()+" "); yuGe.getArmor().run(); } public static class Trigger{ MessageQueue queue= new MessageQueue(); public void trigger(){ queue.cacheMessage(); } } private static class AdapterManager extends Trigger implements Runnable{ private Weapon weapon; @Override public void run() { while (true){ if (queue.hasMessage()&& longGe.getAdapter()!=null){ weapon = (Weapon) longGe.getAdapter().getTarget(); System.out.print(longGe.getName()+" 通過介面卡取得了武器。又開始了攻擊"); weapon.attack(); } } } public Weapon getWeapon() { return weapon; } } private static class MessageQueue{ //接收五個訊號用作緩衝,,這裡沒有用到,但是它邏輯上是存在的 boolean[] bb = new boolean[5]; //以0 記錄佇列頭 int top =0; //遊標,控制佇列的位置 int corsor=0; private void cacheMessage(){ //bb[corsor]=true; move(); } public boolean hasMessage(){ if (top == corsor) return false; Tmove(); return true; } //定義浮標移動的方式 private void move(){ //若當前的遊標已經到達線性尾部,則回到頭部 if ((corsor+1)%5==0){ corsor=0; return; } corsor++; } private void Tmove(){ //若當前的遊標已經到達線性尾部,則回到頭部 if ((top+1)%5==0){ top=0; return; } top++; } } }

  執行結果: