【学习打卡】第九天 Java设计模式精讲-Debug方式+内存分析 第十八讲
课程名称:Java设计模式精讲-Debug方式+内存分析,真正学懂设计模式
课程章节: 迭代器模式+Coding+源码解析
主讲老师:Geely
课程内容:
今天学习的内容包括:
什么是迭代器模式 迭代器模式 优点 缺点 Coding 源码解析 以及在业务上的应用
课程收获:
迭代器 定义与类型
1.定义
提供一种方法,顺序访问一个集合对象中的各个元素 而又不暴露该对象的内部表示
1.2 类型 : 行为型
2.适用场景
1、访问一个集合对象的内容而无需暴露它的内部表示
2、为遍历不同的集合结构提供一个统一的接口
3.缺点
1.类的个数成对增加
4.优点
1.分离了集合对象的遍历行为
5.迭代器-相关设计模式
迭代器模式和访问者模式
7.uml 设计图
8.代码如下
package com.zw.design.pattern.creational.behavioral.iterator;
public interface CourseIterator {
Course nextCourse();
boolean isLastCourse();
}
package com.zw.design.pattern.creational.behavioral.iterator;
import java.util.List;
public class CourseIteratorImpl implements CourseIterator {
private List courseList;
private int position;
private Course course;
public CourseIteratorImpl(List courseList){
this.courseList=courseList;
}
@Override
public Course nextCourse() {
System.out.println("返回课程,位置是: "+position);
course=(Course)courseList.get(position);
position++;
return course;
}
@Override
public boolean isLastCourse(){
if(position< courseList.size()){
return false;
}
return true;
}
}package com.zw.design.pattern.creational.behavioral.iterator;
public class Course {
private String name;
public Course(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package com.zw.design.pattern.creational.behavioral.iterator;
public interface CourseAggregate {
void addCourse(Course course);
void removeCourse(Course course);
CourseIterator getCourseIterator();
}
package com.zw.design.pattern.creational.behavioral.iterator;
import java.util.ArrayList;
import java.util.List;
public class CourseAggregateImpl implements CourseAggregate {
private List courseList;
public CourseAggregateImpl() {
this.courseList = new ArrayList();
}
@Override
public void addCourse(Course course) {
courseList.add(course);
}
@Override
public void removeCourse(Course course) {
courseList.remove(course);
}
@Override
public CourseIterator getCourseIterator() {
return new CourseIteratorImpl(courseList);
}
}3.测试类
package com.zw.design.pattern.creational.behavioral.iterator;
public class Test {
public static void main(String[] args) {
Course course1 = new Course("springboot ");
Course course2 = new Course("spring");
Course course3 = new Course("springcloud");
Course course4 = new Course("Python课程");
Course course5 = new Course("算法课程");
Course course6 = new Course("前端课程");
CourseAggregate courseAggregate = new CourseAggregateImpl();
courseAggregate.addCourse(course1);
courseAggregate.addCourse(course2);
courseAggregate.addCourse(course3);
courseAggregate.addCourse(course4);
courseAggregate.addCourse(course5);
courseAggregate.addCourse(course6);
System.out.println("-----课程列表-----");
printCourses(courseAggregate);
courseAggregate.removeCourse(course4);
courseAggregate.removeCourse(course5);
System.out.println("-----删除操作之后的课程列表-----");
printCourses(courseAggregate);
}
public static void printCourses(CourseAggregate courseAggregate){
CourseIterator courseIterator= courseAggregate.getCourseIterator();
while(!courseIterator.isLastCourse()){
Course course=courseIterator.nextCourse();
System.out.println(course.getName());
}
}
}测试结果如下
迭代器---源码解析
在jdk当中 ArrayList这个类当中 使用迭代器
在mybatis 当中的应用是这个类 DefaultCursor
今天学习课程共用了2个小时,重新学习一下设计模式 更加清楚知道迭代器模式的应用以及如何在自己项目当中去使用它 大家一起加油 💪🏻
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦




