visitor

Generics overkill in visitor pattern

◇◆丶佛笑我妖孽 提交于 2019-12-07 13:58:01
问题 I am working on a project where I am converting an old java 1.2 code written ten years ago to java 7. That project heavily (over)uses a particular visitor. To keep things conceptually simple, lets say that the visitor is something like this: public interface RobotVisitor { public Object visitHead(Head p, Object arg); public Object visitNeck(Neck p, Object arg); public Object visitShoulder(Shoulder p, Object arg); public Object visitArm(Arm p, Object arg); public Object visitHand(Hand p,

Visitor pattern where the visitors choose how to traverse

Deadly 提交于 2019-12-07 09:51:46
问题 As I understand it, in the typical specification of the Visitor pattern, it is the visited objects that decide how to traverse, and generally, they only support one traversal order. (See, e.g., here or here.) Is there a name for the same use of double-dispatch, but where the Visitors get to decide how to traverse the object-hierarchy? In my application, a very heterogeneous collection of document-model types is pushing towards visitors for, e.g., export operations. It seems rigid, though, to

Objective-C categories == visitor pattern?

瘦欲@ 提交于 2019-12-07 02:09:18
问题 Would you say that Objective-C categories are an implementation of the visitor design pattern? 回答1: No, Objective-C categories are not an implementation of the visitor pattern. Categories don't really have an exact match in the design pattern world, since the technique of injecting methods into existing classes without subclasses isn't possible in most languages. I'd say it's closer to the decorator pattern, but that pattern is usually implemented with composition, i.e. by wrapping an

c++ visitor pattern: Why should every derived visited implement Accept()?

岁酱吖の 提交于 2019-12-07 00:59:05
问题 I've seen a couple of examples that demonstrate the visitor pattern. In all of them, each derived visited element implements what's usually called the Accept() method. In a hierarchy of colors, this method might look like: void Red::accept(Visitor *v) { v->visit(*this); } void Blue::accept(Visitor *v) { v->visit(*this); } When Visitor, as well as its inheritors, have the methods: visit(Red red); visit(Blue blue) My question is why not implement this in the same way only in the base class (in

Abstract tree with visitors

旧时模样 提交于 2019-12-06 11:45:49
问题 I have very simple abstract class AbstractTree as below: public abstract class AbstractTree { public abstract void Accept(AbstractTreeVisitor visitor); } and two concrete classes: Node with left and right AbstractTree fields and Leaf with integer value (+ overriden method to accept visitors): public class TreeNode : AbstractTree { public AbstractTree left; public AbstractTree right; public TreeNode(AbstractTree left, AbstractTree right) { this.left = left; this.right = right; } public

Observer pattern + Visitor pattern for message system

巧了我就是萌 提交于 2019-12-06 04:30:31
Recently I got into implementing a message dispatching system that uses the "Observer pattern": nothing special here. As I developed it I thought it would be nice to send "Message" objects from the "subject" that could be fundamentally different from each other and could be read from the many "observers". These different messages took the form of different message classes (for example, think about the "User Logout message", "Screen mode toogle" and "Volume level changed", all of these need different information) and soon I found that the "observers" needed not to know about every different

Visitor pattern where the visitors choose how to traverse

ε祈祈猫儿з 提交于 2019-12-05 17:38:38
As I understand it, in the typical specification of the Visitor pattern, it is the visited objects that decide how to traverse, and generally, they only support one traversal order. (See, e.g., here or here .) Is there a name for the same use of double-dispatch, but where the Visitors get to decide how to traverse the object-hierarchy? In my application, a very heterogeneous collection of document-model types is pushing towards visitors for, e.g., export operations. It seems rigid, though, to say that the various processors (visitors) should all traverse in, say, a breadth-first order. Some of

Objective-C categories == visitor pattern?

↘锁芯ラ 提交于 2019-12-05 06:17:21
Would you say that Objective-C categories are an implementation of the visitor design pattern ? No, Objective-C categories are not an implementation of the visitor pattern. Categories don't really have an exact match in the design pattern world, since the technique of injecting methods into existing classes without subclasses isn't possible in most languages. I'd say it's closer to the decorator pattern , but that pattern is usually implemented with composition, i.e. by wrapping an instance of the object you want to "enhance". The visitor pattern is useful for encapsulating algorithm logic

c++ visitor pattern: Why should every derived visited implement Accept()?

*爱你&永不变心* 提交于 2019-12-05 04:17:21
I've seen a couple of examples that demonstrate the visitor pattern. In all of them, each derived visited element implements what's usually called the Accept() method. In a hierarchy of colors, this method might look like: void Red::accept(Visitor *v) { v->visit(*this); } void Blue::accept(Visitor *v) { v->visit(*this); } When Visitor, as well as its inheritors, have the methods: visit(Red red); visit(Blue blue) My question is why not implement this in the same way only in the base class (in this example: Color ) and polymorphism will do the job, namely, the correct visit will be called since

Generic visitor pattern in java

戏子无情 提交于 2019-12-04 21:20:56
Is the following java implementation of the visitor pattern using generics, general enough to be useful? (I suppose it is). Could it be improved in some way? It's important to be easily call-able using anonymous classes. Thanks. (Example of use): Vector<Number> numbers = new Vector<Number>(); numbers.add(new Double(1.2)); numbers.add(new Float(-1.2)); numbers.add(new Double(4.8)); numbers.add(new Float(-3.4)); numbers.add(new Long(123456)); numbers.add(new Short("14")); For.each(numbers, new Visitor<Number>() { public void doIt(Double n) { System.out.println("doIt() for double: " + n); }