visitor

When should you really use the visitor pattern

久未见 提交于 2019-12-21 02:02:40
问题 Ok before marking this as a duplicate let me clarify myself. I'm reading about the visitor pattern and its applicable uses. I've stumbled upon this post: When should I use the Visitor Design Pattern? and the user who wrote the first answer says as follow : Now we want to add a new operation to the hierarchy, namely we want each animal to make its sound. As far as the hierarchy is this simple, you can do it with straight polymorphism: ... But proceeding in this way, each time you want to add

Using the visitor pattern with generics in C#

南笙酒味 提交于 2019-12-20 13:31:09
问题 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

Using a LINQ ExpressionVisitor to replace primitive parameters with property references in a lambda expression

好久不见. 提交于 2019-12-18 10:54:49
问题 I'm in the process of writing a data layer for a part of our system which logs information about automated jobs that run every day - name of the job, how long it ran, what the result was, etc. I'm talking to the database using Entity Framework, but I'm trying to keep those details hidden from higher-level modules and I don't want the entity objects themselves to be exposed. However, I would like to make my interface very flexible in the criteria it uses to look up job information. For example

Alternative to the visitor pattern?

放肆的年华 提交于 2019-12-18 09:59:14
问题 I am looking for an alternative to the visitor pattern. Let me just focus on a couple of pertinent aspects of the pattern, while skipping over unimportant details. I'll use a Shape example (sorry!): You have a hierarchy of objects that implement the IShape interface You have a number of global operations that are to be performed on all objects in the hierarchy, e.g. Draw, WriteToXml etc... It is tempting to dive straight in and add a Draw() and WriteToXml() method to the IShape interface.

QVariant's Visitor pattern (without manual type testing and casting)

耗尽温柔 提交于 2019-12-18 05:20:32
问题 Does Qt's QVariant class has any existing (and convenient) Visitor pattern implementation? If not, is it possible to achieve something similar to boost::apply_visitor() , i.e. minimize the the duplication in regard to testing the type and casting? I want to achieve something along the following lines: /* I have a QVariant that can contain anything, including user types */ QVariant variant; /* But in my Visitor I'm interested only in ints and QStrings (for the sake of the example) */ struct

need a virtual template member workaround

旧城冷巷雨未停 提交于 2019-12-17 18:18:44
问题 I need to write a program implementing the visitor design pattern. The problem is that the base visitor class is a template class. This means that BaseVisited::accept() takes a template class as a parameter and since it uses 'this' and i need 'this' to point to the correct runtime instance of the object, it also needs to be virtual. I'd like to know if there's any way around this problem. template <typename T> class BaseVisitor { public: BaseVisitor(); T visit(BaseVisited *visited); virtual

Counting Unique Visitor

ⅰ亾dé卋堺 提交于 2019-12-13 17:39:21
问题 I want to count unique visitors and show them to visitors. I don't want to use any 3rd party tool (like analytics or something else) What is a unique visitor exactly? Does the REAL unique visitor changes with IP, cookie or MAC? I've though this way: Get visitors IP adress Search it from database If exists, don't do anything If not, insert IP adress and server time to database and add this to count Is this way right? Should I use cookies or get MAC adresses too? BTW all these things -getting

Symfony: How to make JMS Serializer works with strict types?

痴心易碎 提交于 2019-12-13 13:23:08
问题 This is my situation: I'm trying to write a Symfony REST API that works with "strict" types (integer, boolean and float), because default Symfony behaviour doesn't support it and I want to avoid force cast types (e.g: JMS Serializer converts string value into integer field type) To do this, I have created a custom handler which implements the JMS\Serializer\Handler\SubscribingHandlerInterface (e.g a StrictIntegerHandler ): <?php namespace AppBundle\Serializer; use JMS\Serializer\Context; use

Dynamic method binding with inheritance in Python

醉酒当歌 提交于 2019-12-12 03:45:03
问题 I am new to python (~ a month), and I wish I had switched to it sooner (after years of perl). Problem : I want objects to have different functionality to the same method call based on their type. The methods are assigned at runtime, based on the module loaded (which composes all objects). Question : I wanted to know if there was a popular design pattern that I could use instead of the below, or if this already has a design pattern name (I sadly have no formal CS background, and knowing this

boost::variant - get vector property of members

眉间皱痕 提交于 2019-12-12 03:39:08
问题 I am stuck :). I have two-level hierarchy, each level has child nodes. The goal is to use this structure to populate gui tree. I am trying to access child nodes of variant members in a generic manner. Following code does not compile, using vs2013. I'll appreciate the helping hand and/or advise on the design changes. #include "stdafx.h" #include <memory> #include <string> #include <vector> #include <boost/variant.hpp> class base {}; class A : public base { public: std::vector<std::shared_ptr