大话设计模式-备忘录模式-2020-10-20

2020-10-21  本文已影响0人  勇往直前888

定义

在不破坏封装性的前提下,捕捉一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

结构图

image.png

使用场景

游戏进度备忘录的例子

image.png
/**
 * 游戏角色;就是Originator类
 */
class GameRole {
    private int vitality;//生命力
    private int attack;//攻击力
    private int defense;// 防御力
    final private int limit = 30; // 判断标准
    private boolean flag;

    /**
     * 状态显示
     */
    public String display() {
        String state = "角色当前状态:" + "\n"
                + "体力:"+ vitality + "\n"
                + "攻击力:"+ attack + "\n"
                + "防御力:"+ defense + "\n";
        if (flag == true) {
            state += "状态良好,可以继续战斗";
        } else {
            state += "状态糟糕,请注意";
        }
        Log.v("GameRole", state);
        return state;
    }

    /**
     * 获得初始状态
     */
    public void initState() {
        vitality = 100;
        attack = 100;
        defense = 100;
        flag = true;
    }

    /**
     * 战斗,与大boss决战,损耗; 这里简单表示为[0 ~ 100)之间的随机数
     */
    public void fight() {
        vitality = new Random().nextInt(100);
        attack = new Random().nextInt(100);
        defense = new Random().nextInt(100);
        if ((vitality > limit) && (attack > limit) && (defense > limit)) {
            flag = true;
        } else {
            flag = false;
        }
    }

    /**
     * 保存角色状态,将三个值通过实例化:角色状态存储箱返回
     */
    public RoleStateMemento saveState() {
        return new RoleStateMemento(vitality, attack, defense);
    }

    /**
     * 恢复角色状态
     */
    public void recoveryState(RoleStateMemento memento) {
        if (memento != null) {
            this.vitality = memento.getVitality();
            this.attack = memento.getAttack();
            this.defense = memento.getDefense();
            if ((vitality > limit) && (attack > limit) && (defense > limit)) {
                flag = true;
            } else {
                flag = false;
            }
        }
    }
}
/**
 * 角色状态存储箱类, 就是Caretaker类
 */
class RoleStateCaretaker {
    private RoleStateMemento memento;

    public void setMemento(RoleStateMemento memento) {
        this.memento = memento;
    }

    public RoleStateMemento getMemento() {
        return memento;
    }
}
/**
 * 角色状态存储箱类; Memento类
 */
class RoleStateMemento {
    private int vitality;//生命力
    private int attack;//攻击力
    private int defense;// 防御力

    /**
     * 将生命力、攻击力、防御力存入状态存储箱对象中
     */
    public RoleStateMemento(int vitality, int attack, int defense) {
        this.vitality = vitality;
        this.attack = attack;
        this.defense = defense;
    }

    /**
     * getter
     */
    public int getVitality() {
        return vitality;
    }

    public int getAttack() {
        return attack;
    }

    public int getDefense() {
        return defense;
    }
}
image.png
public class MementoActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, MementoActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    GameRole gameRole;
    RoleStateCaretaker caretaker;
    TextView stateTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memento);
        setTitle("备忘录模式");

        stateTextView = findViewById(R.id.textViewState);
        gameRole = new GameRole();
        caretaker = new RoleStateCaretaker();

        // 初始状态是满血复活的状态
        gameRole.initState();
        stateTextView.setText(gameRole.display());
    }

    public void onInitialButtonClick(View view) {
        gameRole.initState();
        stateTextView.setText(gameRole.display());
    }

    public void onSaveButtonClick(View view) {
        caretaker.setMemento(gameRole.saveState());
    }

    public void onRecoveryButtonClick(View view) {
        gameRole.recoveryState(caretaker.getMemento());
        stateTextView.setText(gameRole.display());
    }

    public void onFightButtonClick(View view) {
        gameRole.fight();
        stateTextView.setText(gameRole.display());
    }
}

Demo地址

https://gitee.com/zhangxusong888/Android/tree/master/design_pattern

上一篇 下一篇

猜你喜欢

热点阅读