深入理解享元模式、解释器模式、访问者模式

Love The Way You Lie 2021-10-13 02:28 252阅读 0赞

一,享元模式

池技术都有了解,线程池,数据库连接池。都是通过共享对象的方式来减少内存的压力。享元模式就是通过共享技术来实现对象的共享。之前有了解工厂模式,工厂模式往往更加适用于少对象的,在特定情况下的对象判断调用,假如要去实现一个国家计算机考试的代码,通过工厂模式来创建,那肯定不行的。有多少个考生 ,就会要创建多少个对象出来。往往通过一个共享的对象下嵌入不同对象去获取数据录入即可。下面的这个例子出自《大话设计模式》中对于相同html代码实现不同需求与服务器挂载的共享模拟。这样可以减少服务器的租赁与增强代码的复用性。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzczODYz_size_16_color_FFFFFF_t_70

  1. public abstract class WebSiteFlyweight {
  2. public abstract void use(User user);
  3. }
  4. public class ConcreteWebSite extends WebSiteFlyweight{
  5. //内部内容
  6. private String instate;
  7. private final String outState;
  8. public ConcreteWebSite(String outState){
  9. this.outState = outState;
  10. }
  11. @Override
  12. public void use(User user) {
  13. System.out.println("use for" + outState);
  14. }
  15. }
  16. public class ConcreteWsFactory {
  17. private Map<String,WebSiteFlyweight> map = new HashMap<String,WebSiteFlyweight>();
  18. public WebSiteFlyweight getConcreteWebSite(String outState){
  19. if(!map.containsKey(outState)){
  20. map.put(outState, new ConcreteWebSite(outState));
  21. }
  22. return map.get(outState);
  23. }
  24. }
  25. public class User {
  26. private String name;
  27. public User(String name){
  28. this.name = name;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. }
  37. public class Main {
  38. public static void main(String[] args) {
  39. ConcreteWsFactory cwdfactory = new ConcreteWsFactory();
  40. ConcreteWebSite cws = (ConcreteWebSite) cwdfactory.getConcreteWebSite("博客");
  41. cws.use(new User("小明"));
  42. ConcreteWebSite cws2 = (ConcreteWebSite) cwdfactory.getConcreteWebSite("博客");
  43. cws.use(new User("晓东"));
  44. ConcreteWebSite cws3 = (ConcreteWebSite) cwdfactory.getConcreteWebSite("主题");
  45. cws3.use(new User("小红"));
  46. }
  47. }

二,解释器模式

先说一个工作中的实际经历,富文本框表情前后端交互往往只能交换一串类似 “/00010X”之类的字符串,这个时候,在前端需要将这串字符串转换成实际的富文本框中的具体某一表情在页面上显示。这就要求前端需要有一套转换解释这些字符串的“字典”。这个“字典”便是起到一个解释的作用。解释器模式对给定的一门语言,定义它的文法的一个表示方式,定义一个解释器去解释具体的含义。下面的代码就简单模拟一下解释器ABC与阿拉伯数字之间的翻译。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzczODYz_size_16_color_FFFFFF_t_70 1

  1. public class PlayerContext {
  2. private String context;
  3. public String getContext() {
  4. return context;
  5. }
  6. public void setContext(String context) {
  7. this.context = context;
  8. }
  9. }
  10. public abstract class Expression {
  11. public PlayerContext interrupt(PlayerContext pc ){
  12. if(pc.getContext().length() <=0){
  13. return null;
  14. }else{
  15. String useContext = pc.getContext().substring(0, 1);
  16. pc.setContext(pc.getContext().substring(1)); //作为新的context存入
  17. excute(useContext);
  18. return pc;
  19. }
  20. }
  21. protected abstract void excute(String useContext);
  22. }
  23. public class MusicExpression extends Expression {
  24. @Override
  25. protected void excute(String useContext) {
  26. String note="";
  27. switch(useContext){
  28. case "A":
  29. note="1";
  30. break;
  31. case "B":
  32. note="2";
  33. break;
  34. case "C":
  35. note="3";
  36. break;
  37. }
  38. System.out.print(note);
  39. }
  40. }
  41. public class Main {
  42. public static void main(String[] args) {
  43. PlayerContext playerContext = new PlayerContext();
  44. playerContext.setContext("ABACCABBACCC");
  45. Expression expression = new MusicExpression();
  46. while(playerContext.getContext().length()!=0){
  47. expression.interrupt(playerContext);
  48. }
  49. }
  50. }

三,访问者模式

《大话设计模式》中对访问者模式是这样解释的,表示一个作用与某对象结构中的各元素的操作,它使你可以在不改变元素的前提下定义作用这些元素的新操作。有些难以理解,我个人归纳了一下,访问者模式通过将被访问对象的行文封装到一起,方便了访问的内容切换,同时也方便增加新的操作。但是却会复杂了数据结构本身,也就是访问者。看下面的代码,对于需要访问的对象,在Acess中声明添加非常的容易,很好的做到了单一原则。但是对于访问者,假如还出现了一个新的性别类型呢?添加的数据结构变化就会很多。实际上对于访问者,代码之间的耦合性是很强很强的。这边就是访问者模式的缺点,所以在使用该模式,需要针对数据结构相对稳定的系统,不容易对数据结构新添加。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzczODYz_size_16_color_FFFFFF_t_70 2

  1. public abstract class Person {
  2. protected abstract void Accept(Action action);
  3. }
  4. public class Woman extends Person {
  5. @Override
  6. protected void Accept(Action action) {
  7. action.getWomanConculusion(this);
  8. }
  9. }
  10. public class Man extends Person {
  11. @Override
  12. protected void Accept(Action action) {
  13. action.getManConclusion(this);
  14. }
  15. }
  16. //将被访问操作封装成一个 被访问对象
  17. public abstract class Action {
  18. protected abstract void getManConclusion(Man man);
  19. protected abstract void getWomanConculusion(Woman woman);
  20. }
  21. public class AccessAction extends Action{
  22. @Override
  23. protected void getManConclusion(Man man) {
  24. System.out.println("男人成功时,背后多半有一个伟大的女人");
  25. }
  26. @Override
  27. protected void getWomanConculusion(Woman woman) {
  28. System.out.println("女人成功时,背后多半有一个伟大的男人");
  29. }
  30. }
  31. public class ActionOperation {
  32. List<Person> plist = new ArrayList<Person>();
  33. public void add(Person person){
  34. plist.add(person);
  35. }
  36. public void rm(Person person){
  37. plist.remove(person);
  38. }
  39. //遍历让每个访问者去访问被访问对象
  40. public void itera(Action action){
  41. for(int i=0;i<plist.size();i++){
  42. plist.get(i).Accept(action);
  43. }
  44. }
  45. }
  46. public class Main {
  47. public static void main(String[] args) {
  48. ActionOperation ao = new ActionOperation();
  49. Person man = new Man();
  50. Person woman = new Woman();
  51. ao.add(man);
  52. ao.add(woman);
  53. Action action = new AccessAction();
  54. ao.itera(action);
  55. }
  56. }

发表评论

表情:
评论列表 (有 0 条评论,252人围观)

还没有评论,来说两句吧...

相关阅读

    相关 设计模式-模式

    享元模式 : 运用共享技术有效地支持大量细粒度的对象。 也就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象。大大节约了资源。在Fl