• [设计模式] javascript 之 迭代子模式


    迭代子模式:定义

    迭代子模式,又称游标模式,是一种用于对聚集进行顺序访问规则的模式,是一种行为模式;它用于提供对聚集对象的一种统一的访问接口,使客户能够在不了解聚集对象内部结构的情况对聚集对象进行访问。它涉及两个部分,一个是聚集对象,一个迭代子对象,迭代对象(Iterator)用于提供访问聚集对象的标题访问方法;

    主要组成角色:

    • 抽象迭代子角色:用于定义访问聚集的标准方法
    • 具体迭代子角色:用于实现访问聚集的访问方法
    • 抽象聚集角色:用于定义公共的聚集对象访问方法,主要的有迭代对象,当前元素获取,聚集对象大小;
    • 具体聚集角色:用于实现聚集对象的公共访问;

    基础代码:

    //抽象聚集对象类
    public Collections() {
         this.arrs = ['xx'];
         this.iterator = function() {
              console.log('需要返回一个Iterator对象');
              return null;
         };
    
         this.size = function() { //Iterator 需要引用
             this.arrs.length;
         }
    
         this.get = function(idx) { //Iterator 需要引用
             return this.arrs[idx];
        }
    };
    
    //迭代对象
    public Iterator (colls) {
         this.colls = colls;
         this.index = 0;
         this.next = function() {
             if > this.colls.size() //引用
             this.index ++ ;
         }
    
         this.prev = function() {
            //if < 0
            this.index--;
        }
    
        this.get = function() {
            this.colls.get(this.index); //引用
        }
    
        //more
    }

    迭代子模式结构图 

    实例

    1. 抽象迭代角色

    function abstractIterator() {
         this.prev = functiojn() {
         };
    
         this.next = function() {
           
         };
    
         this.first = function() {
    
         };
    
         this.hasNext = function() {
    
        };
    
        this.get = function() {
    
        };
    }

    2. 具体迭代角色

    function Iterator(colls) {
         this.colls = colls;
         this.index = 0;
    };
    
    Inteator.prototype = abstractIterator.prototype;
    
    Inteator.prototype.prev = function() {
        if (this.index > 0)
            this.index --;
    };
    
    Inteator.prototype.next = function() {
       if (this.index < this.colls.size()) {
            this.index++;
      }
    };
    
    Inteator.prototype.first = function() {
       this.index = 0;
    };
    
    Inteator.prototype.hasNext = function() {
       return this.index < this.colls.size();
    };
    
    Inteator.prototype.get = function() {
       return this.colls.get(this.index);
    };

    3. 抽象聚集角色

    function abstractCollection() {
          this.iterator = function() {
               return null;
          };
    }

    4. 具体实现聚集角色公共方法

    function Collection() {
         this.arrars = ['XXX', 'yyy', 'ZZZ'];
    };
    
    Collection.prototype = abstractCollection.prototype;
    
    Collection.prototype.iterator = function() {
         return new Iterator(this);
    };
    
    Collection.prototype.size = function() {
        return this.arrays.length;
    };
    
    Collection.prototype.get = function(index) {
         return this.arrays[index];
    };

    5. 客户端使用

    function Client() {
        var colls = new Collection();
        var iterator = colls.iterator();
    
        for (iterator.hasNext()) {
             console.log(iterator.get());
             iterator.next();
        }
    };

    其他说明

    把聚象对象的访问逻辑统一到迭代对象里,让客户可以不用了解聚象对象的结构,就可以一种统一的方式访问,聚集与业务逻辑的解耦!

  • 相关阅读:
    查询避免Unknown column ‘xxx’ in ‘where clause’
    SSM框架——Spring+SpringMVC+Mybatis的搭建
    虚拟机VMware12安装Windows7教程
    IntelliJ IDEA 安装破解详解
    Jar包进行反编译,修改后重新打包
    Thinking in Java 4th(Java编程思想第四版)文档、源码、习题答案
    面向对象编程思想概览(六)异常处理
    面向对象编程思想概览(五)线程的同步
    面向对象编程思想概览(四)多线程
    DL四(预处理:主成分分析与白化 Preprocessing PCA and Whitening )
  • 原文地址:https://www.cnblogs.com/editor/p/5672800.html
Copyright © 2020-2023  润新知