迭代器模式
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.iterator;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/4 14:49
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public class Course {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对课程进行处理
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/4 14:48
|
||||||
|
*/
|
||||||
|
public interface CourseAggregate {
|
||||||
|
|
||||||
|
void addCourse(Course course);
|
||||||
|
|
||||||
|
void removeCourse(Course course);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取课程迭代器
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
CourseIterator getCourseIterator();
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.iterator;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/4 14:50
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.iterator;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JDK迭代器
|
||||||
|
* {@link java.util.Iterator}
|
||||||
|
* 集合内部类实现 {@link ArrayList.Itr#hasNext()}
|
||||||
|
* 同时做了进一步扩展 {@link ArrayList.ListItr}
|
||||||
|
* <p>
|
||||||
|
* mybatis中 {@link org.apache.ibatis.cursor.defaults.DefaultCursor}
|
||||||
|
* 其持有游标迭代器对象 {@link org.apache.ibatis.cursor.defaults.DefaultCursor.CursorIterator}
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/4 14:49
|
||||||
|
*/
|
||||||
|
public interface CourseIterator {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取下一个课程
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Course nextCourse();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否是最后一个课程
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean isLastCourse();
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.iterator;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/4 14:50
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
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() {
|
||||||
|
log.info("返回课程,位置是: {}", position);
|
||||||
|
course = (Course) courseList.get(position);
|
||||||
|
position++;
|
||||||
|
return course;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isLastCourse() {
|
||||||
|
if (position < courseList.size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.iterator;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/3/4 14:52
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
class CourseIteratorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void nextCourse() {
|
||||||
|
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 courseAggregate = new CourseAggregateImpl();
|
||||||
|
|
||||||
|
courseAggregate.addCourse(course1);
|
||||||
|
courseAggregate.addCourse(course2);
|
||||||
|
courseAggregate.addCourse(course3);
|
||||||
|
courseAggregate.addCourse(course4);
|
||||||
|
courseAggregate.addCourse(course5);
|
||||||
|
courseAggregate.addCourse(course6);
|
||||||
|
|
||||||
|
log.info("-----课程列表-----");
|
||||||
|
printCourses(courseAggregate);
|
||||||
|
|
||||||
|
courseAggregate.removeCourse(course4);
|
||||||
|
courseAggregate.removeCourse(course5);
|
||||||
|
|
||||||
|
log.info("-----删除操作之后的课程列表-----");
|
||||||
|
printCourses(courseAggregate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printCourses(CourseAggregate courseAggregate) {
|
||||||
|
CourseIterator courseIterator = courseAggregate.getCourseIterator();
|
||||||
|
while (!courseIterator.isLastCourse()) {
|
||||||
|
Course course = courseIterator.nextCourse();
|
||||||
|
log.info(course.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user