美文网首页
大话设计模式(9) --- 中介者模式|享元模式|解释器模式|访

大话设计模式(9) --- 中介者模式|享元模式|解释器模式|访

作者: 官子寒 | 来源:发表于2020-02-10 16:13 被阅读0次

1. 中介者模式

中介者模式,用一个中介对象来封装一系列的对象交互。中介者使个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互

public abstract class Mediator {
    public abstract void sendMessage(String message, Colleague colleague);
}
public abstract class Colleague {
    Mediator mediator;
    public Colleague(Mediator mediator) {
        this.mediator = mediator;
    }
    public abstract void note(String msg);
}
public class ConcreteMediator extends Mediator {
    ConcreteColleague1 concreteColleague1;
    ConcreteColleague2 concreteColleague2;

    public void setConcreteColleague1(ConcreteColleague1 concreteColleague1) {
        this.concreteColleague1 = concreteColleague1;
    }

    public void setConcreteColleague2(ConcreteColleague2 concreteColleague2) {
        this.concreteColleague2 = concreteColleague2;
    }

    @Override
    public void sendMessage(String message, Colleague colleague) {
        if(colleague == concreteColleague1) {
            concreteColleague2.note(message);
        } else {
            concreteColleague1.note(message);

        }
    }
}
public class ConcreteColleague1 extends Colleague {

    public ConcreteColleague1(Mediator mediator) {
        super(mediator);
    }

    public void sendMessage(String msg) {
        mediator.sendMessage(msg, this);
    }

    @Override
    public void note(String msg) {
        System.out.println("colleague1 received msg" + msg);
    }
}
public class ConcreteColleague2 extends Colleague {

    public ConcreteColleague2(Mediator mediator) {
        super(mediator);
    }

    public void sendMessage(String msg) {
        mediator.sendMessage(msg, this);
    }

    @Override
    public void note(String msg) {
        System.out.println("colleague2 received msg" + msg);

    }
}
public class MediatorClient {
    public static void main(String[] args) {
        ConcreteMediator concreteMediator = new ConcreteMediator();
        ConcreteColleague1 concreteColleague1 = new ConcreteColleague1(concreteMediator);
        ConcreteColleague2 concreteColleague2 = new ConcreteColleague2(concreteMediator);
        concreteMediator.setConcreteColleague1(concreteColleague1);
        concreteMediator.setConcreteColleague2(concreteColleague2);

        concreteColleague1.sendMessage("你好呀");
    }
}
中介者模式 中介者模式
  • 中介者模式一般用于一组对象以定义良好但是复杂的方式进行通信的场合,以及想定制一个分布在多个类中的行为,而又不想生成太多子类的场合

2. 享元模式

享元模式,运用共享技术有效地支持大量细粒度的对象

public class User {
    private String userName;
    public User(String userName) {
        this.userName = userName;
    }

    public String getUserName() {
        return userName;
    }
}
public abstract class Web {
    public abstract void Use(User user);
}
public class ConcreteWeb extends Web {
    String webName;
    public ConcreteWeb (String name) {
        this.webName = name;
    }
    @Override
    public void Use(User user) {
        System.out.println("User is "+ user.getUserName());
    }
}
import java.util.HashMap;

public class WebFactory {
    private HashMap<String, Web> hashMap = new HashMap<>();

    public Web getWeb(String name){
        if(hashMap.get(name) == null) {
            hashMap.put(name, new ConcreteWeb(name));
        }
        return hashMap.get(name);
    }
}
public class WebClient {
    public static void main(String[] args) {
        WebFactory webFactory = new WebFactory();
        Web web = webFactory.getWeb("alibaba.com");
        web.Use(new User("马云"));
    }
}
享元模式
  • 享元模式可以避免大量相似类的开销
  • 把不同的参数移动到外部,在方法调用时将它们传进来(用户),相同的参数放在内部(具体网站)

3. 解释器模式

解释器模式,给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子

解释器模式
public class PlayContext {
    private String text;

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}
public abstract class Expression {
    public void Intercept(PlayContext context) {
        if(context.getText().length() == 0) {
            return;
        } else {
            String playKey = context.getText().substring(0, 1);
            String playValue = context.getText().substring(2,3);
            execute(playKey, playValue);
            context.setText(context.getText().substring(context.getText().indexOf(" ")+ 1));
        }
    }

    protected abstract void execute(String playKey, String playValue);
}
public class Note extends Expression {
    @Override
    protected void execute(String playKey, String playValue) {
        String note = "";
        switch (playKey) {
            case "C":
                note = "1";
                break;
            case "D":
                note = "2";
                break;
            case "E":
                note = "3";
                break;
            case "F":
                note = "4";
                break;
            case "G":
                note = "5";
                break;
            case "A":
                note = "6";
                break;
            case "B":
                note = "7";
                break;
        }
        System.out.println(note);
    }
}
public class Scale extends Expression {
    @Override
    protected void execute(String playKey, String playValue) {
        String scale = "";
        switch (playValue) {
            case "1":
                scale = "低音";
                break;
            case "2":
                scale = "中音";
                break;
            case "3":
                scale = "高音";
                break;
        }
        System.out.println(scale + " ");
    }
}
public class PlayClient {
    public static void main(String[] args) {
        PlayContext playContext = new PlayContext();
        playContext.setText("O 2 E 0.5 F 3");
        Expression expression = null;
        while(playContext.getText().length() != 0) {
            String str = playContext.getText().substring(0,1);
            if(str.equals("O")) {
                expression = new Scale();
            } else {
                expression = new Note();
            }

            expression.Intercept(playContext);
        }
    }
}
解释器模式

4. 访问者模式

访问者模式,表示作用域某对象结构中的各元素的操作。它使你可以在不改变各元素的前提下定义作用于这些元素的新操作

  • 适用于数据结构比较稳定的情况
  • 它把数据结构和作用于结构上的操作耦合解脱开来,使得操作集合(Visitor)可以相对自由地变换
  • 优点是增加新的操作很容易,即只需要增加一个新的访问者


    访问者模式

相关文章

网友评论

      本文标题:大话设计模式(9) --- 中介者模式|享元模式|解释器模式|访

      本文链接:https://www.haomeiwen.com/subject/gcoethtx.html