I'm a visitor, i would visit the ones i knew.

bigNing發表於2003-01-10
I'm learning Design Pattern, Here is what i think about the visitor Pattern. if something wrong, please inform me. thanks

//I'm a visitor, i could visit the ones i knew.
//if i want to visit a new one, i must know him, and he must accept me first.


//the following is Me -- a visitor, and i know He and She, they both accept me.
interface Visitor {
    public void visit(He h);            // i know him
    public void visit(She s);            // i know her
}

class He {
    public void accept(Visitor me) {    // he accepts me
        me.visit(this);                    // so i could visit him
    }
}
class She {
    public void accept(Visitor me) {    // she accepts me 2
        me.visit(this);                    // so i could visit her
    }
}


//and now if i want to visit It, i must know It, that is:
interface Visitor {
    public void visit(He h);            // i know him
    public void visit(She s);            // i know her
    
    public void visit(It i);            // i must know it first
}

class It {
    public void accept(Visitor me) {    // and it must accept me 2.
        me.visit(this);                    // so i could visit it.
    }
}
// at this time, i could visit them separately

//if i want to visit them one by one, i must know all of them, and each of them must accept me. that is:
interface Acceptable {
    public void accept(Visitor me);
}
// and  each of them is acceptable, so :
class He implements Acceptalbe {
    public void accept(Visitor me) {    // he accepts me
        me.visit(this);                    // so i could visit him
    }
    public void running() { }
}
class She implements Acceptalbe {
    public void accept(Visitor me) {    // she accepts me 2
        me.visit(this);                    // so i could visit her
    }
    public void crying() {    }
}
class It implements Acceptalbe {
    public void accept(Visitor me) {    // and it also accept me.
        me.visit(this);                    // so i could visit it.
    }
    public void sleeping()  {    }
}


//now, i was visiting all the one i knows here:

class Visiting {
    public static void main(String []args) {
        Visitor me=new Visitor() {        // this is the real Me
            public void visit(He h) {    // and i know all of them
                h.runing();
            }
            public void visit(She s) {
                s.crying();
            }
            public void visit(It i) {
                i.sleeping();
            }
        };
        Collection me=new LinkedList();    // now i'm ready to visit them 
        me.add(new He());
        me.add(new She());
        me.add(new It());
        
        Iterator iter = me.iterator();
        while ( iter.hasNext() ) {        // so i will visit them one by one.
            Acceptable x=(Acceptable)iter.next();
            x.accept(me);                // and they all accept me, so i could visit them successful them.
        }

    }
}

<p class="indent">

相關文章