Visitor Pattern solution: few visitors have same interface but should work with different objects

前端 未结 4 1015
梦毁少年i
梦毁少年i 2021-01-20 04:31

I have following class diagram (visitor pattern implementation):

Expected result:
1) WiredVisitor should visit only Router and WiredNetworkCard
2)

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 04:55

    You could do this for example:

    public interface IVisitor {
       public void visit( T card );
    }
    

    And then you'd define your visitors as:

    public class WiredVisitor implements IVisitor {
    ...
    }
    
    
    public class WirelessVisitor implements IVisitor {
    ...
    }
    

    This may not be the textbook solution but it's very clean and very readable.

提交回复
热议问题