一个故事贯穿设计模式(八)抽象工厂模式

    包结构:

        

   类结构:

          

             

   实现代码:

         

package com.automannn.design_mode.abstract_factory.test;

import com.automannn.design_mode.abstract_factory.OOP_class.ShieldBuilder;
import com.automannn.design_mode.abstract_factory.OOP_class.VehicleAbstractFactory;
import com.automannn.design_mode.abstract_factory.OOP_class.WeaponIterator;
import com.automannn.design_mode.abstract_factory.OOP_interface.Weapon;
import com.automannn.design_mode.abstract_factory.entity_alive.LongGe;
import com.automannn.design_mode.abstract_factory.entity_alive.YuGe;
import com.automannn.design_mode.abstract_factory.entity_logicalOperation.ArmorFactory;
import com.automannn.design_mode.abstract_factory.entity_logicalOperation.ArmorPrototypeManager;
import com.automannn.design_mode.abstract_factory.entity_logicalOperation.CarFactory;
import com.automannn.design_mode.abstract_factory.entity_logicalOperation.SimpleGunTemplate;
import com.automannn.design_mode.abstract_factory.entity_nonliving.*;
import com.automannn.design_mode.abstract_factory.exception.SingletonException;

/**
 * @author automannn@163.com
 * @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();

        //在打斗中,于哥发现旁边旁边有个台阶,于是站上去以便更好的防御
        try {
            HighStep highStep = HighStep.getInstance(yuGe);
        } catch (SingletonException e) {
            System.out.println(e.getMessage());
        }

        //龙哥本来就比于哥矮,于是肯定不甘心,于是追着站上去
        try {
            HighStep highStep = HighStep.getInstance(longGe);
        } catch (SingletonException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("龙哥不甘心,开始想象要是自己先站上去就好了");
        System.out.println("龙哥开始想象:~~~~");

        //于哥发现站不上去了,脑海中想着要是有个分身先站上去就好了

        //以下是他的想象:
        new Thread(()->{
            try {
                HighStepThreadSafe highStepThreadSafe = HighStepThreadSafe.getInstance(longGe);
            } catch (Exception e) {
                System.out.println("~~~~"+e.getMessage());
            }
        }).start();

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            HighStepThreadSafe highStepThreadSafe = HighStepThreadSafe.getInstance(yuGe);
        } catch (Exception e) {
            System.out.println("~~~~"+e.getMessage());
        }

        System.out.println("-------------------------");

        //于哥发现掉血有点严重,再通过工厂模式生产防弹衣代价太大,于是通过原型模式复制一个防弹衣就ok了
        ArmorPrototypeManager armorPrototypeManager= new ArmorPrototypeManager();
        armorPrototypeManager.register("newArmor",yuGe.getArmor());

        Armor newArmor= (Armor) armorPrototypeManager.create("newArmor");

        System.out.println(yuGe.getName()+" 通过原型模式,复制了一个防弹衣");

        newArmor.run();

        //于哥决定防弹衣不行,还得弄个盾牌
        Shield shield = new Shield();
        shield.run();
        System.out.println("这个盾牌太low了,需要一个更强大的");

        //于哥用通过了一个盾牌构造器构造一个盾牌
        System.out.println("使用盾牌构造器");
        ShieldBuilder shieldBuilder = new ShieldBuilder();
        Shield shield1= shieldBuilder.addParam("金刚石组成").addParam("具有条纹").addParam("可以防子弹").build();
        shield1.run();

        //他们都有点累了,心想回车里坐一下吧,于是通过抽象工厂,龙哥召唤出小汽车,于哥召唤出摩托车
       VehicleAbstractFactory carFactory=  VehicleAbstractFactory.getFactory("com.automannn.design_mode.abstract_factory." +
                "entity_logicalOperation.CarFactory");
       carFactory.createShell();
       carFactory.createWheel();
        System.out.println(longGe.getName()+" 坐骑:");
        carFactory.run();

        VehicleAbstractFactory carFactory1=  VehicleAbstractFactory.getFactory("com.automannn.design_mode.abstract_factory." +
                "entity_logicalOperation.MotorFactory");
        carFactory1.createShell();
        carFactory1.createWheel();
        System.out.println(yuGe.getName() +" 坐骑:");
        carFactory1.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++;
        }
    }


}

    运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值