北风设计模式课程---24、迭代模式

北风设计模式课程---24、迭代模式

一、总结

一句话总结:

迭代模式就是把迭代功能委托给迭代器,实现自身类的简单简洁。

1、什么是迭代模式?

把对容器中包含的内部对象的访问委让给外部类


   Iterator模式也叫迭代模式,是行为模式之
一,它把对容器中包含的内部对象的访问委让给
外部类,使用Iterator(遍历)按顺序进行遍历
访问的设计模式。

2、不使用迭代模式 怎么实现遍历?

1、容器自身添加遍历方法:不简洁:容器本身要做的事情太多了
2、让调用者实现遍历方法:不安全:需要将内部数据暴露给调用者


在应用Iterator模式之前,首先应该明白Iterator
模式用来解决什么问题。或者说,如果不使用
Iterator模式,会存在什么问题。
1.由容器自己实现顺序遍历。直接在容器类里直接添加顺序遍历方法
2.让调用者自己实现遍历。直接暴露数据细节给外部。

3、不使用迭代模式的缺点?

1,容器类承担了太多功能:一方面需要提供添加删除等本身应有的功能;一方面还需要提供遍历访问功能。
2,很容易引起混乱和程序运行错误:往往容器在实现遍历的过程中,【需要保存遍历状态】,当跟元素的添加删除等功能夹杂在一起,很容易引起混乱和程序运行错误等。

4、Iterator模式的应用场景是什么?

- 访问容器中包含的内部对象
- 按顺序访问

5、迭代模式的角色和职责?

Iterator(迭代器接口):该接口必须定义实现迭代功能的最小定义方法集,比如提供hasNext()和next()方法。
ConcreteIterator(迭代器实现类):迭代器接口Iterator的实现类。可以根据具体情况加以实现。
Aggregate(容器接口):定义基本功能以及提供类似Iterator iterator()的方法。
concreteAggregate(容器实现类):容器接口的实现类。必须实现Iterator iterator()方法。

6、迭代模式的优点?

1,功能分离:实现功能分离,简化容器接口。让容器只实现本身的基本功能,把迭代功能委让给外部类实现,符合类的设计原则。
2,隐藏细节:隐藏容器的实现细节。
3,统一接口:为容器或其子容器提供了一个统一接口,一方面方便调用;另一方面使得调用者不必关注迭代器的实现细节。
4,多个迭代方法:可以为容器或其子容器实现不同的迭代方法或多个迭代方法。

7、迭代模式的作用是什么,为什么要有迭代模式?

迭代模式就是把迭代功能委托给迭代器,实现自身类的简单简洁。

二、内容在总结中

1、相关知识

Iterator(迭代器接口):该接口必须定义实现迭代功能的最小定义方法集,比如提供hasNext()和next()方法。
ConcreteIterator(迭代器实现类):迭代器接口Iterator的实现类。可以根据具体情况加以实现。
Aggregate(容器接口):定义基本功能以及提供类似Iterator iterator()的方法。
concreteAggregate(容器实现类):容器接口的实现类。必须实现Iterator iterator()方法。

 

2、代码

Iterator(迭代器接口):该接口必须定义实现迭代功能的最小定义方法集,比如提供hasNext()和next()方法。

ConcreteIterator(迭代器实现类):迭代器接口Iterator的实现类。可以根据具体情况加以实现。

Aggregate(容器接口):定义基本功能以及提供类似Iterator iterator()的方法。

concreteAggregate(容器实现类):容器接口的实现类。必须实现Iterator iterator()方法。

BookList.java

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class BookList {
    private List<Book> bookList;
    private int index;
    private Iterator iterator;
    
    public BookList() {
        bookList = new ArrayList<Book>();
    }
    
    //添加书籍
    public void addBook(Book book) {
        bookList.add(book);
    }
    
    //删除书籍
    public void deleteBook(Book book) {
        int bookIndex = bookList.indexOf(book);
        bookList.remove(bookIndex);
    }
    
//    //判断是否有下一本书
//    public boolean hasNext() {
//        if(index >= bookList.size()) {
//            return false;
//        }
//        return true;
//    }
//    
//    //获得下一本书
//    public Book getNext() {
//        return bookList.get(index++);
//    }
    
//    public List<Book> getBookList() {
//        return bookList;
//    }
    
    public Iterator Iterator() {
        return new Itr();
    }
    
    private class Itr implements Iterator{

        public boolean hasNext() {
            if(index >= bookList.size()) {
                return false;
            }
            return true;
        }

        public Object next() {
            return bookList.get(index++);
        }

        public void remove() {
            
        }
        
    }
    
}

客户端实现

MainClss.java

import java.util.Iterator;


public class MainClss {
    public static void main(String[] args) {
        BookList bookList = new BookList();
        
        Book book1 = new Book("010203","Java编程思想",90);
        Book book2 = new Book("010204","Java从入门到精通",60);
        
        bookList.addBook(book1);
        bookList.addBook(book2);
        
//        while(bookList.hasNext()) {
//            Book book = bookList.getNext();
//            book.display();
//        }
        
//        List<Book> bookDateList = bookList.getBookList();
//        for(int i = 0; i < bookDateList.size(); i++) {
//            Book book = bookDateList.get(i);
//            book.display();
//        }
        
        Iterator iter = bookList.Iterator();
        while(iter.hasNext()) {
            Book book = (Book) iter.next();
            book.display();
        }
        
        
    }
}

物品基类

Book.java

public class Book {
    private String ISBN;
    private String name;
    private double price;
    
    public Book(String isbn, String name, double price) {
        ISBN = isbn;
        this.name = name;
        this.price = price;
    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String isbn) {
        ISBN = isbn;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    
    public void display() {
        System.out.println("ISBN=" + ISBN + ",name=" + name + ",price" + price);
    }
    
    
}
 

转载于:https://www.cnblogs.com/Renyi-Fan/p/11079712.html

猜你喜欢

转载自blog.csdn.net/weixin_33751566/article/details/93571703