当前位置: 首页 > wzjs >正文

企业商城网站开发建设汽车营销策划方案ppt

企业商城网站开发建设,汽车营销策划方案ppt,新冠咳嗽怎么办,一个空间可以做多少个网站迭代器模式(Iterator Pattern)学习笔记 编程相关书籍分享:https://blog.csdn.net/weixin_47763579/article/details/145855793 1. 模式定义 行为型设计模式,提供一种方法顺序访问聚合对象中的各个元素,而无需暴露该对…

迭代器模式(Iterator Pattern)学习笔记


编程相关书籍分享:https://blog.csdn.net/weixin_47763579/article/details/145855793


1. 模式定义

行为型设计模式,提供一种方法顺序访问聚合对象中的各个元素,而无需暴露该对象的内部表示。将遍历逻辑与聚合对象解耦,实现多种遍历方式。

2. 适用场景

✅ 需要统一遍历不同结构的聚合对象
✅ 需要支持多种遍历方式(正序/逆序/过滤等)
✅ 需要隐藏聚合对象的内部结构
✅ 需要为同一聚合对象提供多个并行遍历
✅ 遵循单一职责原则(分离遍历职责)

3. 模式结构

creates
«interface»
Aggregate
+createIterator() : Iterator
ConcreteAggregate
-items: List
+createIterator()
«interface»
Iterator
+hasNext() : boolean
+next() : Object
+remove()
ConcreteIterator
-aggregate: ConcreteAggregate
-index: int
+hasNext()
+next()
Client
+traverse()

4. 核心角色

角色说明
Aggregate聚合接口,定义创建迭代器的方法
ConcreteAggregate具体聚合类,实现创建对应迭代器
Iterator迭代器接口,定义遍历方法
ConcreteIterator具体迭代器,实现遍历逻辑
Client客户端,通过迭代器访问聚合对象

5. 代码示例

5.1 书架遍历示例

// 聚合接口
public interface BookShelf {Iterator<Book> iterator();void addBook(Book book);
}// 具体聚合
public class ConcreteBookShelf implements BookShelf {private List<Book> books = new ArrayList<>();public Iterator<Book> iterator() {return new BookIterator(this);}public void addBook(Book book) {books.add(book);}public int size() {return books.size();}public Book getAt(int index) {return books.get(index);}
}// 迭代器接口
public interface Iterator<T> {boolean hasNext();T next();
}// 具体迭代器
public class BookIterator implements Iterator<Book> {private ConcreteBookShelf bookShelf;private int index = 0;public BookIterator(ConcreteBookShelf bookShelf) {this.bookShelf = bookShelf;}public boolean hasNext() {return index < bookShelf.size();}public Book next() {Book book = bookShelf.getAt(index);index++;return book;}
}// 客户端
public class Client {public static void main(String[] args) {BookShelf shelf = new ConcreteBookShelf();shelf.addBook(new Book("Design Patterns"));shelf.addBook(new Book("Clean Code"));shelf.addBook(new Book("Refactoring"));Iterator<Book> it = shelf.iterator();while(it.hasNext()) {System.out.println(it.next().getTitle());}}
}class Book {private String title;public Book(String title) { this.title = title; }public String getTitle() { return title; }
}

6. 模式变种

  1. 内部迭代器
    将迭代逻辑封装在聚合内部,客户端通过回调函数操作元素

    public interface InternalIterator {void forEach(Consumer<Book> action);
    }// 客户端调用
    bookShelf.forEach(book -> System.out.println(book));
    
  2. 过滤迭代器
    实现条件遍历功能

    public class FilterIterator implements Iterator<Book> {private Iterator<Book> source;private Predicate<Book> predicate;public FilterIterator(Iterator<Book> source, Predicate<Book> predicate) {this.source = source;this.predicate = predicate;}public boolean hasNext() {while(source.hasNext()) {if(predicate.test(source.next())) return true;}return false;}// 实现next()...
    }
    

7. 优缺点分析

✔️ 优点

  • 分离集合对象与遍历算法
  • 支持多种遍历方式
  • 简化聚合接口
  • 支持并行遍历
  • 符合单一职责和开闭原则

缺点

  • 增加系统复杂度(简单集合可能不需要)
  • 遍历效率可能低于直接访问
  • 需要维护迭代器状态

8. 相关模式对比

模式目的关键区别
访问者模式对对象结构元素执行操作迭代器关注遍历,访问者关注操作
组合模式处理树形结构常与迭代器模式结合使用
工厂方法模式创建对象迭代器常使用工厂方法创建具体迭代器

9. 实际应用案例

  • Java集合框架的Iterator接口
  • JDBC的ResultSet遍历
  • XML解析器的节点遍历(DOM4J的NodeIterator)
  • Android的Cursor接口
  • Spring的ResourceArrayPropertyEditor
  • MyBatis的Cursor接口(流式查询)

10. 最佳实践建议

  1. 优先使用现有迭代器:Java集合的Iterator已足够应对多数场景
  2. 实现Iterable接口:方便与增强for循环集成
    public class BookShelf implements Iterable<Book> {// ...public Iterator<Book> iterator() {return new BookIterator(this);}
    }
    
  3. 支持快速失败(fail-fast):检测并发修改
    public class SafeIterator implements Iterator<Book> {private int modCount = expectedModCount;public boolean hasNext() {checkModification();// ...}private void checkModification() {if(modCount != expectedModCount) {throw new ConcurrentModificationException();}}
    }
    
  4. 使用泛型:增强类型安全性
  5. 考虑线程安全:同步访问或返回独立迭代器
  6. 组合使用其他模式:如工厂方法创建迭代器

11. 扩展应用(树形结构遍历)

// 树节点
class TreeNode {String value;List<TreeNode> children = new ArrayList<>();public Iterator<TreeNode> depthFirstIterator() {return new DepthFirstIterator(this);}public Iterator<TreeNode> breadthFirstIterator() {return new BreadthFirstIterator(this);}
}// 深度优先迭代器
class DepthFirstIterator implements Iterator<TreeNode> {private Stack<TreeNode> stack = new Stack<>();public DepthFirstIterator(TreeNode root) {stack.push(root);}public boolean hasNext() {return !stack.isEmpty();}public TreeNode next() {TreeNode current = stack.pop();for(int i = current.children.size()-1; i >=0; i--) {stack.push(current.children.get(i));}return current;}
}// 广度优先迭代器
class BreadthFirstIterator implements Iterator<TreeNode> {private Queue<TreeNode> queue = new LinkedList<>();public BreadthFirstIterator(TreeNode root) {queue.offer(root);}public boolean hasNext() {return !queue.isEmpty();}public TreeNode next() {TreeNode current = queue.poll();queue.addAll(current.children);return current;}
}

🔄 设计原则体现

  1. 单一职责原则:将遍历职责从集合类中分离
  2. 开闭原则:新增迭代器无需修改现有代码
  3. 接口隔离原则:客户端仅依赖迭代器接口

通过迭代器模式,可以实现灵活的集合遍历机制,特别适合需要支持多种遍历方式或隐藏集合内部结构的场景。该模式在集合框架和复杂数据结构处理中应用广泛,是解耦遍历逻辑的经典解决方案。

http://www.dtcms.com/wzjs/560216.html

相关文章:

  • 网站开发需要哪些条件免费建设网站那个好
  • 做服务网站成都网站建设哪家好文章
  • 遵义公司做网站企业展示网站建设
  • 怎样用织梦建设网站微信营销的模式
  • 做运营常用的网站福田欧曼服务站电话
  • 网站开发工程师绩效考核免费公司网址
  • 平面设计网站培训中心奥尔马手表官方网站
  • 网站开发前端课程外国语学院英文网站建设
  • 自己做网站做淘宝联盟如何进行新产品的推广
  • 哪个网站看电视剧最全还免费温州企业自助建站系统
  • 深圳专业做网站建设wordpress 复杂表单
  • 网站规划思想方法有哪些内容建设网站有哪些参考文献
  • 视频网站如何建设深汕特别合作区
  • 古色古香 网站模板大兴网站开发网站建设报价
  • 创建网站目录权限不备案的网站很慢
  • 桂林网站建设哪家好什么是网站托管
  • 石家庄做网络推广的网站网站建设好与管理在哪就业
  • 756ka网站建设alexa排名是什么意思
  • 备案 增加网站花卉网站建设的总结与
  • 营销类网站 英文旅游电子商务网站模板
  • 企业网站搭建网页游戏在线玩不用登录
  • 网站建设的软件知识有哪些内容搜狗推广登录平台
  • 东莞市建网站WordPress 门票
  • 网站建设文化信息管理咨询公司调研报告
  • 百度合作的网盟网站wordpress网页如何上传下载
  • 做第三方的qq互联接口时_回调到自己的网站时要延时很久是什么原因wordpress清除多余附件
  • 玩具网站建设wordpress所有分类目录的地址
  • 公司使用威联通nas做网站存储安庆做网站
  • 济南网站推广效果个人网站设计总结
  • 有做学历在网站能查的到的凡科网做网站要钱吗