zoukankan      html  css  js  c++  java
  • java----设计模式--行为型模式(GOF23)

    行为型模式关注系统中对象之间的相互交互,研究系统在运行时对象之间的相互通信和协作,进一步明确对象的职责

     责任链模式:

    将能够处理同一类请求的对象连成一条链,所提交的请求沿着链传递,链上的对象逐个判断是否有能力处理该请求,如果能则处理,如果不能则传递给链上的下一个对象。

    场景:例如请假审批,将请求交给班长,班长不行交给辅导员,辅导员不行交给学生办

    我们可以使用 if  if else 等处理请求,但是这种方式的弊端在于扩展性不好,并且代码之间的耦合度较高,不满足开闭原则。

    开发中常见的场景:
      -Java中,异常机制就是一种责任链模式。一个try可以对应多个catch,当第一个catch不匹配类型,则自动跳到第二个catch.
      -Javascript语言中,事件的冒泡和捕获机制。Java语言中,事件的处理采用观察者模式。
      -Servlet开发中,过滤器的链式处理
      -Struts2中,拦截器的调用也是典型的责任链模式

    //请假对象
    public class LeaveRequest {
        public String name;
        public int day;
        public LeaveRequest(String name, int day) {
            this.name = name;
            this.day = day;
        }
    }
    abstract class RequestHandle{
        RequestHandle nextRequestHandle;
        abstract void handleRequest(LeaveRequest leaveRequest);
        public void setNextRequestHandle(RequestHandle requestHandle){
            this.nextRequestHandle = requestHandle;
        };
    }
    class Monitor extends RequestHandle{
        public void handleRequest(LeaveRequest leaveRequest){
            if (leaveRequest.day<3){
                System.out.println("请假成功");
                return;
            }else {
                if (nextRequestHandle!=null){
                    nextRequestHandle.handleRequest(leaveRequest);
                }else {
                    System.out.println("请假不成功");
                }
            }
        }
    }
    class Counselor extends RequestHandle {
        public void handleRequest(LeaveRequest leaveRequest){
            if (leaveRequest.day<10){
                System.out.println("请假成功");
                return;
            }else {
                if (nextRequestHandle!=null){
                    nextRequestHandle.handleRequest(leaveRequest);
                }else {
                    System.out.println("请假不成功");
                }
            }
        }
    }
    class Demo{
        public static void main(String[] args) {
            Monitor monitor = new Monitor();
            Counselor counselor = new Counselor();
            monitor.setNextRequestHandle(counselor);
    
            monitor.handleRequest(new LeaveRequest("A",5));
        }
    }
    

     迭代器模式:

    核心是游标,容器是数组或者集合。

    如果使用链表也是可以的(就不需要游标了)

    interface MyIterator{
        public boolean hasNext();
        public Object next();
    }
    public class ConcreteMyAggregate {
        private List<Object> list = new ArrayList<>();
    
        public void addObject(Object object) {
            list.add(object);
        }
        public void removeObject(Object object) {
            list.remove(object);
        }
        private ConcreteIterator getIterator() {
            return new ConcreteIterator();
        }
        private class ConcreteIterator implements MyIterator{
            private int cursor = 0;
            public boolean hasNext() {
                return cursor < list.size() ? true : false;
            }
    
            public Object next() {
                Object o = list.get(cursor);
                cursor++;
                return o;
            }
        }
    
        public static void main(String[] args) {
            ConcreteMyAggregate concreteMyAggregate = new ConcreteMyAggregate();
            concreteMyAggregate.addObject("1");
            concreteMyAggregate.addObject("2");
            concreteMyAggregate.addObject("3");
            ConcreteIterator iterator = concreteMyAggregate.getIterator();
            while (iterator.hasNext()){
                Object next = iterator.next();
                System.out.println(next);
            }
        }
    }
    

     中介者模式:

      如果一个系统中对象之间的联系呈现为网状结构,对象之间存在大量多对多关系,将导致关系及其复杂,这些对象称为“同事对象",我们可以引入一个中介者对象,使各个同事对象只跟中介者对象打交道,将复杂的网络结构化解为如下的星形结构。

      注意:如果对象调用关系简单,一定不要使用中介者模式。反而会变得复杂起来

      

  • 相关阅读:
    8.10
    今日头条笔试题 1~n的每个数,按字典序排完序后,第m个数是什么?
    Gym 100500B Conference Room(最小表示法,哈希)
    CodeForces 438D The Child and Sequence(线段树)
    UVALIVE 6905 Two Yachts(最小费用最大流)
    Gym Conference Room (最小表示法,哈希)
    hdu 2389 Rain on your Parade(二分图HK算法)
    Codeforces Fox And Dinner(最大流)
    zoj 3367 Counterfeit Money(dp)
    ZOJ3370. Radio Waves(2-sat)
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/11644837.html
Copyright © 2011-2022 走看看