首页 > 其他 > 详细

12. 迭代器模式与命令模式

时间:2021-03-31 14:16:55      阅读:23      评论:0      收藏:0      [点我收藏+]

一、迭代器模式

1、迭代器模式 [Iteratir Parttern] 又称为游标模式 [Cursor Pattern],它提供一种顺序访问集合/容器对象元素的方法,而又无需暴露集合内部表示

2、本质,抽离集合对象迭代行为到迭代器种,提供一致访问接口

3、属于《行为型模式》

【适用场景】

1、访问一个集合对象的内容而不需暴露它的内部表示

2、为表里不同的集合结构提供一个统一的访问接口

技术分享图片

//课程为例
public class Course {
    private String name;
    public Course(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}
//迭代器
public interface Iterator<E> {
    E next();
    boolean hasNext();
}
//集合接口
public interface ICourseAggregate {
    void add(Course course);
    void remove(Course course);
    Iterator<Course> iterator();
}
//迭代器实现
public class IteratorImpl<E> implements Iterator<E> {
    private List<E> list;
    private int cursor;//游标
    private E element;//元素
    public IteratorImpl(List<E> list) {
        this.list = list;
    }
    public E next() {
        System.out.print("当前位置 " + cursor + " : ");
        element = list.get(cursor);
        cursor ++;
        return element;
    }
    public boolean hasNext() {
        if(cursor > list.size() - 1){
            return false;
        }
        return true;
    }
}
//集合实现
public class CourseAggregateImpl implements ICourseAggregate {
    private List courseList;
    public CourseAggregateImpl() {
        this.courseList = new ArrayList();
    }
    public void add(Course course) {
        courseList.add(course);
    }
    public void remove(Course course) {
        courseList.remove(course);
    }
    public Iterator<Course> iterator() {
        return new IteratorImpl<Course>(courseList);
    }
}
//调用
public static void main(String[] args) {
    Course java = new Course("Java架构");
    Course javaBase = new Course("Java基础");
    Course design = new Course("设计模式");
    Course ai = new Course("人工智能");

    ICourseAggregate aggregate = new CourseAggregateImpl();
    aggregate.add(java);
    aggregate.add(javaBase);
    aggregate.add(design);
    aggregate.add(ai);

    System.out.println("===========课程列表==========");
    printCourse(aggregate);

    aggregate.remove(ai);

    System.out.println("===========删除操作之后的课程列表==========");
    printCourse(aggregate);
}

private static void printCourse(ICourseAggregate aggregate) {
    Iterator<Course> i = aggregate.iterator();
    while (i.hasNext()){
        Course course = i.next();
        System.out.println("《" + course.getName() + "》");
    }
}

迭代器模式优点

1、多态迭代,为不同的聚合结构提供一致的遍历接口,即一个迭代接口可以访问不同的聚集对象

2、简化集合对象接口,迭代器模式将集合对象本身应该提供的元素迭代接口抽取到了迭代器种,使集合对象无须关心具体迭代行为

3、元素迭代功能多样化,每一个集合对象都可以提供一个或多个不同的迭代器,使同种元素聚合结构可以有不同的迭代行为

4、解耦迭代集合,迭代器模式封装了具体的迭代算法,迭代算法的变化,不会影响到集合对象的架构

迭代器模式缺点

1、对于比较简单的遍历[像数组或者有序列表],适用迭代器方式遍历较为繁琐

二、命令模式

1、命令模式[ Command Pattern ] 是对命令的封装,每一个命令都是一个操作。 请求方请求执行,接收方收到并执行,命令模式解耦了请求方和接收方,请求方只需请求执行命令不用关心命令使怎么被接收,怎么样被操以及是否被执行...

2、本质,解耦命令请求与处理

3、属于《行为型模式》

【适用场景】

1、显示语义种具备"命令"的操作[ 如命令菜单,shell命令... ]

2、请求调用者和请求的接收者需要解耦,使得调用者和接收者不直接交互

3、需要抽象出等待执行的行为,比如撤销[ Undo ]操作和恢复 [Redo]操作

4、需要支持命令宏[ 即命令组合操作 ]

技术分享图片

//播放器
public class GPlayer {
    public void play(){
        System.out.println("正常播放");
    }
    public void speed(){
        System.out.println("拖动进度条");
    }
    public void stop(){
        System.out.println("停止播放");
    }
    public void pause(){
        System.out.println("暂停播放");
    }
}
//执行接口
public interface IAction {
    void execute();
}
//执行实现
public class PlayAction implements IAction {
    private GPlayer gplayer;
    public PlayAction(GPlayer gplayer) {
        this.gplayer = gplayer;
    }
    public void execute() {
        gplayer.play();
    }
}
public class SpeedAction implements IAction {
    private GPlayer gplayer;
    public SpeedAction(GPlayer gplayer) {
        this.gplayer = gplayer;
    }
    public void execute() {
        gplayer.speed();
    }
}
public class StopAction implements IAction {
    private GPlayer gplayer;
    public StopAction(GPlayer gplayer) {
        this.gplayer = gplayer;
    }
    public void execute() {
        gplayer.stop();
    }
}
public class PauseAction implements IAction {
    private GPlayer gplayer;
    public PauseAction(GPlayer gplayer) {
        this.gplayer = gplayer;
    }
    public void execute() {
        gplayer.pause();
    }
}
//控制
public class Controller {
    private List<IAction> actions = new ArrayList<IAction>();
    public void addAction(IAction action){
        actions.add(action);
    }
    public void execute(IAction action){
        action.execute();
    }
    public void executes(){
        for (IAction action:actions) {
            action.execute();
        }
        actions.clear();
    }
}
//调用
public static void main(String[] args) {
    GPlayer player = new GPlayer();
    Controller controller = new Controller();
    controller.execute(new PlayAction(player));

    controller.addAction(new PauseAction(player));
    controller.addAction(new PlayAction(player));
    controller.addAction(new StopAction(player));
    controller.addAction(new SpeedAction(player));
    controller.executes();
}

命令模式的优点

1、通过引入中间件[ 抽象接口 ],解耦了命令请求与实现

2、扩展性良好,可以很容易的增加新命令

3、支持组合命令,支持命令队列

4、可以在现有命令的基础上,增加额外功能 [比如日志记录....结合装饰器模式更酸爽]

命令模式的缺点

1、具体命令类可能过多

2、增加了程序的复杂度,理解更加困难

12. 迭代器模式与命令模式

原文:https://www.cnblogs.com/JustDoIt-1221/p/14600701.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!