visitor

what's the difference between the patterns Strategy, Visitor and Template Method?

萝らか妹 提交于 2019-12-03 03:19:54
问题 I'm in a class where we just learned about these design patterns. However I couldn't see any difference between them. They sound just like the same, creating concrete classes over the abstract one. Could somebody help me kill this doubt? thanks (: 回答1: Both the visitor, the strategy, and the template pattern encompass the application of an algorithm. The biggest difference is in how they are evoked and how they are used in practice. While it may seem like they have the same use case, look at

Using the visitor pattern with generics in C#

我是研究僧i 提交于 2019-12-03 03:10:18
I want to know whether the below is an acceptable use of the visitor pattern. I feel a little uncomfortable returning from an Accept() or Visit() call - is this an appropriate usage of this pattern and if not, why not? Note: Apologies for the long code sample, seems necessary to get across what I'm doing as visitor always seems to be a little involved... interface IAnimalElement<T> { T Accept(IAnimalVisitor<T> visitor); } interface IAnimalVisitor<T> { T Visit(Lion lion); T Visit(Peacock peacock); T VisitZoo(List<Animal> animals); } abstract class Animal { public int Age { get; protected set; }

Building own C# compiler using ANTLR: Compilation Unit

佐手、 提交于 2019-12-03 02:59:54
// Create a scanner that reads from the input stream passed to us CSLexer lexer = new CSLexer(new ANTLRFileStream(f)); tokens.TokenSource = lexer; // Create a parser that reads from the scanner CSParser parser = new CSParser(tokens); // start parsing at the compilationUnit rule CSParser.compilation_unit_return x = parser.compilation_unit(); object ast = x.Tree; What can I do with the x which is of compilation_unit_return type, to extract its root, its classes, its methods etc? Do I have to extract its Adaptor out? How do I do that? Note that the compilation_unit_return is defined as such in my

how to use antlr4 visitor

感情迁移 提交于 2019-12-03 02:37:31
I am a beginner of antlr. I was trying to use visitor in my code and following the instruction on the net. However, I found out that the visitor was not entering the method I create at all. May anyone tell me what I did wrong? This is my visitor: import java.util.LinkedList; import org.antlr.v4.runtime.misc.NotNull; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Sherwood */ public class ExtractMicroBaseVisitor extends MicroBaseVisitor<Integer> { //LinkedList<IR> ll = new LinkedList<IR>(); //MicroParser parser; //System.out

How to make a safer C++ variant visitor, similar to switch statements?

时光怂恿深爱的人放手 提交于 2019-12-02 20:14:50
The pattern that a lot of people use with C++17 / boost variants looks very similar to switch statements. For example: ( snippet from cppreference.com ) std::variant<int, long, double, std::string> v = ...; std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, }, v); The problem is when you put the wrong type in the visitor or change the variant signature, but forget to change the visitor. Instead of getting a compile error, you will have the wrong

what's the difference between the patterns Strategy, Visitor and Template Method?

风格不统一 提交于 2019-12-02 17:46:46
I'm in a class where we just learned about these design patterns. However I couldn't see any difference between them. They sound just like the same, creating concrete classes over the abstract one. Could somebody help me kill this doubt? thanks (: Both the visitor, the strategy, and the template pattern encompass the application of an algorithm. The biggest difference is in how they are evoked and how they are used in practice. While it may seem like they have the same use case, look at the construction of the objects to see the difference. The strategy pattern is often used when we don't have

Implementing Visitor Pattern while allowing different return types of functions

余生长醉 提交于 2019-12-01 13:56:58
问题 I am trying to implement the Visitor Pattern for an object structure which has methods with different return types (string, signed int, unsigned int, etc). Now, in the object hierarchy I have added an Accept method with the following signature (using C++): void Accept(Visitor *); I am unable to figure out how I can use the same interface (with void return type ) while at the same time allowing my concrete methods to have different return types. 回答1: The Accept method in the type hierarchy is

Variable 'x.Sub' of type 'SubType' referenced from scope '' but it is not defined error

淺唱寂寞╮ 提交于 2019-12-01 08:37:10
Check this fiddle for the error: https://dotnetfiddle.net/tlz4Qg I have two classes like this: public class ParentType{ private ParentType(){} public int Id { get; protected set; } public SubType Sub { get; protected set; } } public class SubType{ private SubType(){} public int Id { get; protected set; } } I am going to transform a multilevel anonymous expression to a multilevel non-anonymous expression. To achieve this I have an expression like the below-mentioned one: x => new { x.Id, Sub = new { x.Sub.Id } } To achieve that goal, I have transformed it to an expression like this: x => new

Variable 'x.Sub' of type 'SubType' referenced from scope '' but it is not defined error

和自甴很熟 提交于 2019-12-01 07:39:46
问题 Check this fiddle for the error: https://dotnetfiddle.net/tlz4Qg I have two classes like this: public class ParentType{ private ParentType(){} public int Id { get; protected set; } public SubType Sub { get; protected set; } } public class SubType{ private SubType(){} public int Id { get; protected set; } } I am going to transform a multilevel anonymous expression to a multilevel non-anonymous expression. To achieve this I have an expression like the below-mentioned one: x => new { x.Id, Sub =

Java field type for a value of a generically recursive self-type?

穿精又带淫゛_ 提交于 2019-12-01 06:32:43
Given a class hierarchy where the base class defines a recursive self-type: abstract class A<T extends A<T>> { } How can I declare another class (which should not be generic in T, because such a T could vary over the lifetime of the object) with a field that can hold any subclass of A? The following does not work: public class B { //fails to compile, because the capture of ? is not sufficiently narrow private A<?> a; public <T extends A<T>> setA(T a) { this.a = a; } } -- END OF QUESTION -- I've noticed a tendency of a number of StackOverflow members to approach certain difficult questions with