java设计模式——迭代器模式
一. 定义与类型
定义:提供一种方法,顺序访问一个集合对象中的各个元素,而又不暴露该对象的内部表示
类型:行为型。
二. 使用场景
(1) 访问一个集合对象的内容而无需暴露它的内部表示
(2) 为遍历不同的集合结构提供一个统一的接口
三. 优缺点
优点:
(1) 分离了集合对象的遍历行为
缺点:
(1) 类的个数成对增加
四. 相关设计模式
迭代器模式和访问者模式
它们都是迭代的访问一个集合对象中的元素。访问者模式中扩展开放的部分在作用于对象的操作上;而迭代器模式中,扩展开放的部分是在集合对象的种类上
六. Coding
虽然迭代器模式在jdk中应用很广泛,但是一般在开发过程中,不会自己写一个迭代器。
/**
* @program: designModel
* @description:
* @author: YuKai Fan
* @create: 2019-02-13 15:46
**/
public class Course {
private String name; public Course(String name) {
this.name = name;
} public String getName() {
return name;
}
}
/**
* @program: designModel
* @description:
* @author: YuKai Fan
* @create: 2019-02-13 15:47
**/
public interface CourseAggregate {
void addCourse(Course course);
void removeCourse(Course course); CourseIterator getCourseIterator(); }
/**
* @program: designModel
* @description:
* @author: YuKai Fan
* @create: 2019-02-13 15:49
**/
public class CourseAggregetImpl implements CourseAggregate { private List courseList; public CourseAggregetImpl() {
this.courseList = new ArrayList();
} public void addCourse(Course course) {
courseList.add(course);
} public void removeCourse(Course course) {
courseList.remove(course);
} public CourseIterator getCourseIterator() {
return new CourseIteratorImpl(courseList);
}
}
/**
* @program: designModel
* @description:
* @author: YuKai Fan
* @create: 2019-02-13 15:48
**/
public interface CourseIterator {
Course nextCourse();
boolean isLastCourse();
}
/**
* @program: designModel
* @description:
* @author: YuKai Fan
* @create: 2019-02-13 15:51
**/
public class CourseIteratorImpl implements CourseIterator { private List courseList;
int position;
Course course; public CourseIteratorImpl(List courseList) {
this.courseList = courseList;
} public Course nextCourse() {
System.out.println("返回课程,位置是: " + position);
course = (Course) courseList.get(position);
position++;
return course;
} public boolean isLastCourse() {
if (position < courseList.size()) {
return false;
}
return true;
}
}
/**
* @program: designModel
* @description:
* @author: YuKai Fan
* @create: 2019-02-13 15:53
**/
public class Test {
public static void main(String[] args) {
Course course1 = new Course("Java电商一期");
Course course2 = new Course("Java电商二期");
Course course3 = new Course("Java设计模式");
Course course4 = new Course("Python课程");
Course course5 = new Course("算法课程");
Course course6 = new Course("前端课程"); CourseAggregate courseAggreget = new CourseAggregetImpl(); courseAggreget.addCourse(course1);
courseAggreget.addCourse(course2);
courseAggreget.addCourse(course3);
courseAggreget.addCourse(course4);
courseAggreget.addCourse(course5);
courseAggreget.addCourse(course6); System.out.println("----课程列表----");
printCourses(courseAggreget); courseAggreget.removeCourse(course4);
courseAggreget.removeCourse(course5); System.out.println("----删除操作之后的课程列表----");
printCourses(courseAggreget);
} public static void printCourses(CourseAggregate courseAggregate) {
CourseIterator courseIterator = courseAggregate.getCourseIterator();
while (!courseIterator.isLastCourse()) {
Course course = courseIterator.nextCourse();
System.out.println(course.getName());
}
}
}
结果:

七. 源码分析
JDK中的Iterator中的hasNext()是标准的迭代器模式
java设计模式——迭代器模式的更多相关文章
- JAVA 设计模式 迭代器模式
用途 迭代器模式 (Iterator) 提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示. 迭代器模式是一种行为型模式. 结构
- Java设计模式の迭代器模式
迭代器模式定义 迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示. 迭代器模式的角色构成 (1)迭代器角色(Iterator):定义遍历元素所需 ...
- JAVA设计模式---迭代器模式
1.定义: 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 2.实例:1)需求: 菜单(煎饼屋菜单.餐厅菜单和咖啡菜单)采用不同的集合存取(ArrayList,String[] ...
- java设计模式----迭代器模式和组合模式
迭代器模式: 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 设计原则: 单一责任:一个类应该只有一个引起变化的原因 组合模式: 允许你将对象组合成树形结构来表现“整体/部分” ...
- 19. 星际争霸之php设计模式--迭代器模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- Java设计模式——组合模式
JAVA 设计模式 组合模式 用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模 ...
- java设计模式--单列模式
java设计模式--单列模式 单列模式定义:确保一个类只有一个实例,并提供一个全局访问点. 下面是几种实现单列模式的Demo,每个Demo都有自己的优缺点: Demo1: /** * 单列模式需要满足 ...
- 3.java设计模式-建造者模式
Java设计模式-建造者模式 在<JAVA与模式>一书中开头是这样描述建造(Builder)模式的: 建造模式是对象的创建模式.建造模式可以将一个产品的内部表象(internal repr ...
- Java设计模式-代理模式之动态代理(附源代码分析)
Java设计模式-代理模式之动态代理(附源代码分析) 动态代理概念及类图 上一篇中介绍了静态代理,动态代理跟静态代理一个最大的差别就是:动态代理是在执行时刻动态的创建出代理类及其对象. 上篇中的静态代 ...
随机推荐
- toTop插件(三)
前言 当窗体内容过多会出现滚动, 点击回到顶部滚动条在在上边(大家都懂得,我语文学的不好,表达不清^_^) 看代码 CSS : .toTop{ position: fixed; width: 50px ...
- grunt 安装使用(一)
grunt 依赖nodejs,所有在使用前确保你安装了nodejs,然后开始执行grunt命令. .安装node nodejs安装教程 安装完成后在命令行,执行命令: node -v 出现版本信息, ...
- PlayMaker Get Parent 拿到父物体
这里是拿到自己的父物体,然后存储到Parent这个GameObject变量里. 然后在Parent那个位置生成一个Coin,也就是在父物体那生成一个Coin.
- python函数(四)
一.函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pasc ...
- MakeFile基本使用
MakeFile Making makefile demo # Run this line when useing `make` command # default is the target whi ...
- POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
Steady Cow Assignment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- checkbox判断选中的三种方法
方法一: if ($("#checkbox-id")get(0).checked) { // do something } 方法二: if($('#checkbox-id' ...
- 连接字符串(web.config)
data source=ip; initial catalog=db1; user id=sa; password=*** <connectionStrings> <add name ...
- NIM(1) 一排石头的游戏
最近在实习面试过程中,一个朋友遇到了该问题,从简单到复杂的思路如下,希望能给遇到相同问题的朋友一些启发和帮助.(内容来源网络和<编程之美>) 1.问题1 100个苹果 桌上有100个苹果, ...
- SublimeText插件autoprefixer : css 添加前续
/* 使用前 */ body { background: linear-gradient(to bottom, #DADADA, #000); } p a { -webkit-border-radiu ...