visitor-pattern

Design patterns for PHP - visitor pattern vs servant pattern

天涯浪子 提交于 2019-12-10 11:23:05
问题 I find these two patterns are similar (and the most of other behavioral patterns) visitor pattern, interface Visitor { public function visit(Visitable $Visitable); } interface Visitable { public function accept(Visitor $Vsitor); } class ConcreteVisitable implements Visitable { public $items = array(); public function addItem($item) { $this->items[] = $item; } public function accept(Visitor $Vsitor) { $Vsitor->visit($this); } } class ConcreteVisitor implements Visitor { protected $info; public

Visitor Pattern for two arguments

ぃ、小莉子 提交于 2019-12-10 03:32:36
问题 Here is a problem statement: We have interfaces/super classes Student and Teacher Student has two implementations/sub clasees, ScienceStudent and PhysicalEducationStudent Teacher has ScienceTeacher and PhysicalEducationTeacher. We want to implement a method getMeetingPoint(Student s, Teacher t) which returns a place where they meet based on the type of Student and Teacher. For example, if its a ScienceStudent and ScienceTeacher they meet at Lab if PEStudent and PETeacher they meet on the

Visitor pattern with Java 8 default methods

戏子无情 提交于 2019-12-08 19:18:41
问题 Visitor pattern (double dispatch) is a very useful pattern in its own rights, but it has often been scrutinized of breaking interfaces if any new member is added to the inheritance hierarchy, which is a valid point. But after the introduction of default methods in Java 8, now that we can define default implementation in interfaces, the client interfaces will not break and clients can gracefully adopt the changed interface as appropriate. interface Visitor{ public void visit(Type1 type);

Using Visitor Pattern to detect intersection between two shapes

房东的猫 提交于 2019-12-08 13:09:02
问题 I realize this is a very specific question so it would be helpful if the answer people give includes explicit codes on how to do this. Thanks. I have an abstract base class Shape: class Shape { ..... virtual bool GetIntersection(Shape* _shape) = 0; } class Circle : public Shape {...} class Triangle : public Shape {...} Both these derived classes overrides GetIntersection; I have: //main.cpp .... Shape* shape; Shape* circle = new Circle; if(input == 0) shape = new Circle; else shape = new

Anonymous or real class definition when using visitor pattern?

帅比萌擦擦* 提交于 2019-12-08 01:46:32
问题 When you use the Visitor pattern and you need to get a variable inside visitor method, how to you proceed ? I see two approaches. The first one uses anonymous class : // need a wrapper to get the result (which is just a String) final StringBuild result = new StringBuilder(); final String concat = "Hello "; myObject.accept(new MyVisitor() { @Override public void visit(ClassA o) { // this concatenation is expected here because I've simplified the example // normally, the concat var is a complex

Generics and the visitor pattern

馋奶兔 提交于 2019-12-07 15:24:40
问题 I am having a problem with the Visitor pattern and generics. I have some abstract class whose children are to be visited. Look at this code: public abstract class Element extends SomeSuperClass { public void accept(Visitor<? extends Element> v) { v.visit(this); } } public interface Visitor<T extends SomeSuperClass> { void visit(T element); } So the idea is: I have some class hierarchy (e.g. Element is a subclass of SomeSuperClass ). I have got some generic Visitor interface to visit this

Command Pattern vs. Visitor Pattern

て烟熏妆下的殇ゞ 提交于 2019-12-07 00:37:06
问题 Is it generally acceptable to allow a Visitor to modify state of the Receiver, or should that be a Command pattern instead? 回答1: The purpose of the visitor pattern is to allow new operations to be added to a class heirarchy without modification to that heirarchy. I've never seen anyone suggesting that only read-only operations are acceptable. The only limitation is that the added operations should only use the public interface of the class heirarchy. 回答2: I don't think you can make a blanket

What are the actual advantages of the visitor pattern? What are the alternatives?

假装没事ソ 提交于 2019-12-06 19:21:02
问题 I read quite a lot about the visitor pattern and its supposed advantages. To me however it seems they are not that much advantages when applied in practice: "Convenient" and "elegant" seems to mean lots and lots of boilerplate code Therefore, the code is hard to follow. Also 'accept'/'visit' is not very descriptive Even uglier boilerplate code if your programming language has no method overloading (i.e. Vala) You cannot in general add new operations to an existing type hierarchy without

Anonymous or real class definition when using visitor pattern?

▼魔方 西西 提交于 2019-12-06 06:34:16
When you use the Visitor pattern and you need to get a variable inside visitor method, how to you proceed ? I see two approaches. The first one uses anonymous class : // need a wrapper to get the result (which is just a String) final StringBuild result = new StringBuilder(); final String concat = "Hello "; myObject.accept(new MyVisitor() { @Override public void visit(ClassA o) { // this concatenation is expected here because I've simplified the example // normally, the concat var is a complex object (like hashtable) // used to create the result variable // (I know that concatenation using

Generics and the visitor pattern

我是研究僧i 提交于 2019-12-06 03:54:19
I am having a problem with the Visitor pattern and generics. I have some abstract class whose children are to be visited. Look at this code: public abstract class Element extends SomeSuperClass { public void accept(Visitor<? extends Element> v) { v.visit(this); } } public interface Visitor<T extends SomeSuperClass> { void visit(T element); } So the idea is: I have some class hierarchy (e.g. Element is a subclass of SomeSuperClass ). I have got some generic Visitor interface to visit this hierarchy. Now in the middle of this hierarchy is the Element class, which is abstract and has it's own