简体   繁体   English

Java-使用策略实现迭代器

[英]Java - implement iterator with strategy

I am trying to implement a custom iterator for a composite class and use different strategies in the iterator implementation depending on how the client wants to traverse the composite structure. 我正在尝试为复合类实现自定义迭代器,并在迭代器实现中使用不同的策略,具体取决于客户端希望如何遍历复合结构。

public class MyComposite implements Iterable<MyComponent> {

    ArrayList<MyComponent> childComponents;

    //MyComposite methods

    @Override
    public Iterator<MyComponent> iterator() {
        return new MyIterator(this);
    }
}

I would like to pass a MyIteratorStrategy object as part of the iterator construction, however the Iterable interface does not permit passing objects to the iterator method. 我想将MyIteratorStrategy对象作为迭代器构造的一部分传递,但是Iterable接口不允许将对象传递给iterator方法。

public class SomeClient {
    private void traverseComposite() {
        MyComposite myComposite = new MyComposite();

        MyIteratorStrategy fooStrategy = new MyIteratorStrategy("foo");
        MyIteratorStrategy barStrategy = new MyIteratorStrategy("bar");

        MyIterator fooIterator = myComposite.iterator(fooStrategy);
        MyIterator barIterator = myComposite.iterator(barStrategy);     
    }
}

Here's how I'm utilizing the strategy in the iterator implementation: 这是我在迭代器实现中利用策略的方式:

public class MyIterator implements Iterator<MyComponent> {

    MyComponent component;
    MyIteratorStrategy strategy;

    public MyIterator(MyComponent component, MyIteratorStrategy strategy) {
        this.component = component;
        this.strategy = strategy;
    }

    @Override
    public Component next() {
        if(strategy.isDone(component) {
            //return some child component
        } else {
            //return some other child component
        }
    }

    //rest of implementation
}

I'm trying to learn how to utilize design patterns, so perhaps I'm being overly general. 我正在尝试学习如何利用设计模式,所以也许我过于笼统了。 How can I cleanly inject the strategy into the iterator? 如何将策略完全注入迭代器?

Change MyIterator to be the class that implements Iterable<MyComponent> , instead of Iterator . MyIterator更改为实现Iterable<MyComponent> ,而不是Iterator (You might want to rename it something like MyComponentScanner for clarity.) (为清楚起见,您可能希望将其重命名为MyComponentScanner的名称。)

Move the public Iterator<MyComponent> iterator() method over from the MyComposite class to MyIterator (or whatever you rename it). public Iterator<MyComponent> iterator()方法从MyComposite类移至MyIterator (或任何您重命名的方法)。

Example... 例...

public class MyComponentScanner implements Iterable<MyComponent> {

    MyComponent component;
    MyIteratorStrategy strategy;

    public MyComponentScanner(MyComponent component, MyIteratorStrategy strategy) {
        this.component = component;
        this.strategy = strategy;
    }

    @Override
    public Iterator<MyComponent> iterator() {
        return new Iterator() {
            @Override
            public Component next() {
                if(strategy.isDone(component) {
                    //return some child component
                } else {
                    //return some other child component
                }
            }
        };
    }

    //rest of implementation
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM