• 浅谈设计模式(六)


    一、备忘录模式

    用一个中介对象封装一系列交互的对象,使得各个对象不需要显示的交互引用,松散耦合,属于行为模式。

     发起方:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能。

    1. class MAP{
    2. String longitude;
    3. String latitude;
    4. public String getLongitude() {
    5. return longitude;
    6. }
    7. public void setLongitude(String longitude) {
    8. this.longitude = longitude;
    9. }
    10. public String getLatitude() {
    11. return latitude;
    12. }
    13. public void setLatitude(String latitude) {
    14. this.latitude = latitude;
    15. }
    16. public Memento addSign(){
    17. return new Memento(longitude, latitude);
    18. }
    19. public void recover(Memento memento){
    20. this.latitude = memento.getLatitude();
    21. this.longitude = memento.getLongitude();
    22. }
    23. }

    备忘录角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。

    1. class Memento{
    2. String longitude;
    3. String latitude;
    4. public Memento(String longitude, String latitude) {
    5. this.longitude = longitude;
    6. this.latitude = latitude;
    7. }
    8. public String getLongitude() {
    9. return longitude;
    10. }
    11. public void setLongitude(String longitude) {
    12. this.longitude = longitude;
    13. }
    14. public String getLatitude() {
    15. return latitude;
    16. }
    17. public void setLatitude(String latitude) {
    18. this.latitude = latitude;
    19. }
    20. }

    管理者角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。

    1. class CareTaker{
    2. List<Memento> mementoList = new ArrayList<>();
    3. public void addMemento(Memento memento){
    4. mementoList.add(memento);
    5. }
    6. public Memento getMemento(int index){
    7. return mementoList.get(index);
    8. }
    9. }

    客户端调用:

    1. MAP map = new MAP();
    2. map.setLatitude("108");
    3. map.setLongitude("72");
    4. CareTaker careTaker = new CareTaker();
    5. careTaker.addMemento(map.addSign());
    6. map.setLatitude("0");
    7. map.setLongitude("0");
    8. careTaker.addMemento(map.addSign());
    9. Memento memento = careTaker.getMemento(0);
    10. map.recover(memento);

    二、中介者模式

    又叫调停模式,它定义了一个中介角色来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。

    Mediator: 中介者的接口,由具体中介者角色来实现。

    MediatorStruct: 实现中介者接口。

    Consumer:同事类的接口,保存中介者对象,提供同事对象交互的抽象方法,实现所有相互影响的同事类的公共功能。

    RichPeople,PoorPeople: 抽象同事类的实现者,当需要与其他同事对象交互时,由中介者对象负责后续的交互。

    1. abstract class Consumer{
    2. protected String name;
    3. protected Mediator mediator;
    4. public Consumer(String name, Mediator mediator){
    5. this.name = name;
    6. this.mediator = mediator;
    7. }
    8. }
    9. class PoorPeople extends Consumer{
    10. public PoorPeople(String name, Mediator mediator) {
    11. super(name, mediator);
    12. }
    13. public void contact(){
    14. this.mediator.contact("穷人", this);
    15. }
    16. }
    17. class RichPeople extends Consumer{
    18. public RichPeople(String name, Mediator mediator) {
    19. super(name, mediator);
    20. }
    21. public void contact(){
    22. this.mediator.contact("富人", this);
    23. }
    24. }
    1. abstract class Mediator{
    2. abstract void contact(String meaasge, Consumer consumer);
    3. }
    4. class MediatorStruct extends Mediator{
    5. PoorPeople poorPeople;
    6. RichPeople richPeople;
    7. public void setPoorPeople(PoorPeople poorPeople) {
    8. this.poorPeople = poorPeople;
    9. }
    10. public void setRichPeople(RichPeople richPeople) {
    11. this.richPeople = richPeople;
    12. }
    13. @Override
    14. void contact(String meaasge, Consumer consumer) {
    15. if(consumer == richPeople){
    16. System.out.println("富人发来了消息");
    17. }else {
    18. System.out.println("穷人发来了消息");
    19. }
    20. }
    21. }

    客户端调用

    1. MediatorStruct mediatorStruct = new MediatorStruct();
    2. PoorPeople poorPeople = new PoorPeople("张三", mediatorStruct);
    3. RichPeople richPeople = new RichPeople("李四", mediatorStruct);
    4. mediatorStruct.setPoorPeople(poorPeople);
    5. mediatorStruct.setRichPeople(richPeople);
    6. poorPeople.contact();
    7. richPeople.contact();

    三、解释器模式

    给定一个语言表达式,定义它的文法,并定义一个解释器,使用该解释器解释语言中的句子。

    AbstractExpression: 抽象表达式接口。

    Variable: 终结符表达式类。返回具体字符对应的值。

    Plus: 非终结符表达式。用于计算两个变量进行相加的操作。

    Context: 上下文环境。Map集合里面存储的就是变量以及变量对应的值。此外,我们还在该类里面定义了两个方法,一个是assign,该方法是用来向Map集合里面添加变量及其对应的值的;一个是getValue,该方法就是根据变量来获取变量对应的值的。

    1. interface AbstractExpression {
    2. int interpret(Context context);
    3. }
    4. class Plus implements AbstractExpression{
    5. AbstractExpression left;
    6. AbstractExpression right;
    7. @Override
    8. public int interpret(Context context) {
    9. return left.interpret(context) + right.interpret(context);
    10. }
    11. public Plus(AbstractExpression left, AbstractExpression right) {
    12. this.left = left;
    13. this.right = right;
    14. }
    15. }
    16. class Variable implements AbstractExpression{
    17. String name;
    18. @Override
    19. public int interpret(Context context) {
    20. return context.getValue(this);
    21. }
    22. public Variable(String name) {
    23. this.name = name;
    24. }
    25. }
    1. class Context{
    2. private Map<AbstractExpression, Integer> map = new HashMap<>();
    3. public void assign(AbstractExpression variable, Integer number){
    4. map.put(variable, number);
    5. }
    6. public Integer getValue(AbstractExpression variable){
    7. return map.get(variable);
    8. }
    9. }

    客户端调用:

    1. Context context = new Context(); // 下面的操作是添加映射关系
    2. Variable a = new Variable("a");
    3. Variable b = new Variable("b");
    4. Variable c = new Variable("c");
    5. context.assign(a, 1);
    6. context.assign(b, 2);
    7. context.assign(c, 3);
    8. Plus plus = new Plus(c, new Plus(a, b));
    9. int interpret = plus.interpret(context);
    10. System.out.println(interpret);

    四、状态模式

    用来解决对象在多个状态之间进行转换,对外界输出不同的行为状态,显著的优点是方便维护并且符合开闭原则。

    BoxState: 抽象类定义了各个状态的变化操作,并提供一个set方法。

    BoxOpen: 盒子打开的状态,继承并重写状态对应的方法。

    BoxClose: 盒子关闭的状态,继承并重写状态对应的方法。

    Boxer:状态模式里面的环境角色,state变量是用来记录当前的状态。

    1. abstract class BoxState{
    2. Boxer boxState;
    3. public void setBoxState(Boxer boxer){
    4. this.boxState = boxer;
    5. }
    6. abstract void open();
    7. abstract void close();
    8. }
    9. class BoxOpen extends BoxState{
    10. @Override
    11. public void open() {
    12. System.out.println("打开盒子");
    13. }
    14. @Override
    15. public void close() {
    16. this.boxState.setState(Boxer.CLOSE_STATE);
    17. this.boxState.close();
    18. }
    19. }
    20. class BoxClose extends BoxState{
    21. @Override
    22. public void open() {
    23. this.boxState.setState(Boxer.OPENING_STATE);
    24. this.boxState.open();
    25. }
    26. @Override
    27. public void close() {
    28. System.out.println("关上盒子");
    29. }
    30. }
    1. class Boxer{
    2. public final static BoxOpen OPENING_STATE = new BoxOpen();
    3. public final static BoxClose CLOSE_STATE = new BoxClose();
    4. BoxState state;
    5. public BoxState getState() {
    6. return state;
    7. }
    8. public void setState(BoxState state) {
    9. this.state = state;
    10. this.state.setBoxState(this);
    11. }
    12. public void open(){
    13. this.state.open();
    14. }
    15. public void close(){
    16. this.state.close();
    17. }
    18. }

    客户端调用:

    1. Boxer boxer = new Boxer();
    2. boxer.setState(new BoxOpen());
    3. boxer.open();
    4. boxer.close();

  • 相关阅读:
    为什么要在时钟输出上预留电容的工位?
    【分布式应用】消息队列之卡夫卡 + EFLFK集群部署
    7.7 网络(二)
    使用hugo+github搭建免费个人博客
    Linux:firewalld防火墙-介绍(1)
    Java之juc旅途-atomic(五)
    Git GUI工具:SourceTree代码管理
    React整理杂记(一)
    1236 - 二分查找
    如何通过内网穿透实现远程连接NAS群晖drive并挂载电脑硬盘?
  • 原文地址:https://blog.csdn.net/weixin_41678001/article/details/125292490