设计模式 - 调停者&&门面&&责任链&&策略

设计模式 - 调停者&&门面&&责任链&&策略,第1张

设计模式 - 调停者&&门面&&责任链&&策略

责任链模式:

调停者
  • 类似于MQ
  • 所有类都继承他,公共的全抽象出来


  • 门面facade模式,将Tankframe与数据分离
  • 工厂模式:生产
  • 策略模式:制定各种Collder策略
  • 责任链模式:将策略组合起来,形成不同的责任链模式
  • GameModel :即使调停者,也是门面
package com.fengzi.tank.tank;

import com.fengzi.tank.abstractFactory.baseTank;
import com.fengzi.tank.abstractFactory.DefaultFactory;
import com.fengzi.tank.abstractFactory.GameFactory;
import com.fengzi.tank.cor.ColliderChain;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;


public class GameModel {
    
    List myTanks = new ArrayList<>();
    //责任链模式
    ColliderChain collider = new ColliderChain();
    //调停者模式
    private List object = new ArrayList<>();


    public void add(GameObject go) {
        this.object.add(go);
    }

    public void remove(GameObject go) {
        this.object.remove(go);
    }

    public int getSize() {
        return this.object.size();
    }

    
//    GameFactory gf = new RactFactory();
    public GameFactory gf = new DefaultFactory();

    public GameModel() {
        //初始化地方坦克
        int initTankCount = Integer.valueOf((String) PropertyMgr.getValue("initTankCount"));
        for (int i = 0; i < initTankCount; i++) {
            add(gf.createTank(50 + i * 80, 200, Dir.DOWN, Group.BAD, this));
        }
        for (int i = 0; i < 1; i++) {
            myTanks.add(gf.createTank(200, 400, Dir.UP, Group.GOOD, this));
        }
    }

    public void paint(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.white);
        g.drawString("子弹数量:" + getSize(), 10, 60);
        g.drawString("敌方坦克数量:" + getSize(), 10, 80);
        g.drawString("我方坦克数量:" + getSize(), 10, 100);
        if (0 == getSize()) {
            Color color1 = g.getColor();
            g.setColor(Color.RED);
            g.drawString("你输了!", 400, 300);
            g.setColor(color1);
            return;
        }
        if (0 == getSize()) {
            Color color2 = g.getColor();
            g.setColor(Color.YELLOW);
            g.drawString("你赢了!", 400, 300);
            g.setColor(color2);
            return;
        }
        g.setColor(color);

        //子弹画  不能用迭代 因为里边有move方法
        for (int i = 0; i < object.size(); i++) {
            object.get(i).paint(g);
        }
//        //我方坦克画
        for (int i = 0; i < myTanks.size(); i++) {
            myTanks.get(i).paint(g);
        }

        //碰撞检测 -- 责任链模式和策略模式
        for (int i = 0; i < object.size(); i++) {
            for (int j = i + 1; j < object.size(); j++) {
                GameObject o1 = object.get(i);
                GameObject o2 = object.get(j);
                collider.collider(o1, o2);
            }
        }
    }

    public List getMainTank() {
        return myTanks;
    }
}

  • 调停者模式 :GameObject,管理所有的实体类
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IyfiIGtI-1640601891953)(https://note.youdao.com/yws/res/23385/WEBRESOURCE76aca0d6e61c09acd021f81cbb4c53e0)]
package com.fengzi.tank.tank;

import java.awt.*;


public abstract class GameObject {

    int x, y;

    public abstract void paint(Graphics g);
}

  • 策略模式:Collider,指定不同的策略
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nuaJZyAp-1640601891954)(https://note.youdao.com/yws/res/23390/WEBRESOURCEb002b4bc29c165e86e0a517edcaaafe6)]
package com.fengzi.tank.cor;

import com.fengzi.tank.tank.GameObject;


public interface Collider {
    
    boolean collider(GameObject o1, GameObject o2);
}

  • 不同策略:BulletTankCollider
package com.fengzi.tank.cor;

import com.fengzi.tank.abstractFactory.baseBullet;
import com.fengzi.tank.abstractFactory.baseTank;
import com.fengzi.tank.tank.Explode;
import com.fengzi.tank.tank.GameObject;
import com.fengzi.tank.tank.TankModel;


public class BulletTankCollider implements Collider{
    @Override
    public boolean collider(GameObject o1, GameObject o2) {
        if(o1 instanceof baseBullet && o2 instanceof baseTank){
            baseBullet baseBullet = (baseBullet) o1;
            baseTank baseTank = (baseTank) o2;
            //按理应该吧碰撞的逻辑放到这么来
            return collideWith(baseBullet,baseTank);
        }else if (o1 instanceof baseTank && o2 instanceof baseBullet) {
            return collider(o2, o1);
        }
        return false;
    }



    public boolean collideWith(baseBullet baseBullet,baseTank tankModel) {
        //判断是否是同一阵营
        if (baseBullet.getGroup() == tankModel.getGroup()) {
            return false;
        }
        if (baseBullet.getRectangle().intersects(tankModel.rectangle)) {
            baseBullet.del();
            tankModel.del();
            int xe = tankModel.getX() + TankModel.WIDTH/2 - Explode.WIDTH/2;
            int ye = tankModel.getY() + TankModel.HEIGHT/2 -Explode.HEIGHT/2;
//            tf.explodes.add(new Explode(xe, ye, tf));
            baseBullet.getGm().add(baseBullet.getGm().gf.createExplode(xe, ye, baseBullet.getGm()));
            return true;
        }
        return false;

    }
}

  • 不同策略:TankTankCollider
package com.fengzi.tank.cor;

import com.fengzi.tank.abstractFactory.baseTank;
import com.fengzi.tank.tank.GameObject;


public class TankTankCollider implements Collider {
    @Override
    public boolean collider(GameObject o1, GameObject o2) {
        if (o1 instanceof baseTank && o2 instanceof baseTank) {
            baseTank baseTank1 = (baseTank) o1;
            baseTank baseTank2 = (baseTank) o2;
            //按理应该吧碰撞的逻辑放到这么来
            return collideWith(baseTank1, baseTank2);
        }
        return false;
    }


    public boolean collideWith(baseTank baseTank1, baseTank baseTank2) {
        //判断是否是同一阵营
        baseTank1.resiver();
        baseTank2.resiver();
        return true;
    }
}

  • 责任链模式:ColliderChain,组合不同策略
package com.fengzi.tank.cor;

import com.fengzi.tank.tank.GameObject;

import java.util.linkedList;
import java.util.List;


public class ColliderChain implements Collider{

    private List colliders = new linkedList<>();

    public ColliderChain() {
        //策略模式
        add(new BulletTankCollider());
        add(new TankTankCollider());
//        add(new ColliderChain());
    }

    public void add(Collider collider) {
        colliders.add(collider);
    }

    @Override
    public boolean collider(GameObject o1, GameObject o2) {
        for (int i = 0; i < colliders.size(); i++) {
            if(colliders.get(i).collider(o1, o2)){
                return true;
            }
        }
        return false;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5683220.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存