visitor

Java visitor pattern instead of instanceof switch

≡放荡痞女 提交于 2019-11-27 21:31:46
In this question it is said I can use visitor pattern instead of a bunch of instanceof s. Jmg said "If you are not free to change A, B, and C, you could apply the visitor pattern to achieve the same." As far as I understand I still have to make A, B and C support visitor (have an accept() method, for example). My problem is I have absolutely no possibility to change A, B and C. I just get the Car object from the foreign library and have to call wash() method specific for trucks, race cars and buses. I think I still need an if-else-if construction with instanceof s. Am I right? Yes, to

How do I create a Magento session outside of Magento?

会有一股神秘感。 提交于 2019-11-27 20:57:16
I am able to access an existing session outside of Magento perfectly fine using the popular method below. require 'app/Mage.php'; $mageRunCode = isset ( $_SERVER ['MAGE_RUN_CODE'] ) ? $_SERVER ['MAGE_RUN_CODE'] : ''; $mageRunType = isset ( $_SERVER ['MAGE_RUN_TYPE'] ) ? $_SERVER ['MAGE_RUN_TYPE'] : 'store'; $app = Mage::app ( $mageRunCode, $mageRunType ); Mage::getSingleton ( 'core/session', array ('name' => 'frontend' ) ); This works great, but how do I actually create a Magento session outside of Magento that would populate the log_url, log_visitor, etc. tables as well as assigning the

How Visitor Pattern avoid downcasting

女生的网名这么多〃 提交于 2019-11-27 20:48:35
can anyone show example code before and after to avoid down casting for visitor pattern code ? Thanks. A bare, minimalistic example. Before class Base {}; class Derived1 : public Base {}; class Derived2 : public Base {}; // Some arbitrary function that handles Base. void Handle(Base& obj) { if (...type is Derived1...) { Derived1& d1 = static_cast<Derived1&>(base); std::printf("Handling Derived1\n"); } else if (...type is Derived2...) { Derived2& d2 = static_cast<Derived2&>(base); std::printf("Handling Derived2\n"); } } This means Base must have some type tag field, or you will be using dynamic

Implementing the visitor pattern using C++ Templates

谁都会走 提交于 2019-11-27 18:49:47
I've been trying to reduce the amount of boilerplate in my code, by using C++ Templates to implement the visitor pattern. So far I've come up with this: class BaseVisitor { public: virtual ~BaseVisitor() {} }; template<typename T> class Visitor : public BaseVisitor { public: virtual void visit(T& /* visitable */) = 0; }; template<typename Derived> class Visitable { public: void accept(Visitor<Derived>& visitor) { visitor.visit(static_cast<Derived&>(*this)); } }; And each subclass of Visitable looks like this: class Mesh : public Object, public Visitable<Mesh> {}; class Text : public Object,

best way to do variant visitation with lambdas

点点圈 提交于 2019-11-27 18:10:39
I want to inline visitation of variant types with lambdas. At the moment i have the following code: struct Foo { boost::variant< boost::blank , int , string , vector< int > > var; template <typename T, typename IL , typename SL , typename VL> void ApplyOptionals( T& ref, IL&& intOption , SL&& stringOption , VL&& vectorOption ) { if (var.which() == 1) { intOption( ref , boost::get< int >(var) ); } else if (var.which() ==2) { stringOption( ref , boost::get< string>(var) ); } else if (var.which() == 3) { vectorOption( ref , boost::get< vector< int > >(var) ); } }; }; // ... myFooV.ApplyOptionals(

What is the point of accept() method in Visitor pattern?

被刻印的时光 ゝ 提交于 2019-11-27 10:08:29
There is a lot of talk on decoupling the algorithms from the classes. But, one thing stays aside not explained. They use visitor like this abstract class Expr { public <T> T accept(Visitor<T> visitor) {visitor.visit(this);} } class ExprVisitor extends Visitor{ public Integer visit(Num num) { return num.value; } public Integer visit(Sum sum) { return sum.getLeft().accept(this) + sum.getRight().accept(this); } public Integer visit(Prod prod) { return prod.getLeft().accept(this) * prod.getRight().accept(this); } Instead of calling visit(element) directly, Visitor asks the element to call its

best way to do variant visitation with lambdas

蹲街弑〆低调 提交于 2019-11-27 04:15:58
问题 I want to inline visitation of variant types with lambdas. At the moment i have the following code: struct Foo { boost::variant< boost::blank , int , string , vector< int > > var; template <typename T, typename IL , typename SL , typename VL> void ApplyOptionals( T& ref, IL&& intOption , SL&& stringOption , VL&& vectorOption ) { if (var.which() == 1) { intOption( ref , boost::get< int >(var) ); } else if (var.which() ==2) { stringOption( ref , boost::get< string>(var) ); } else if (var.which(

Refactoring Code to avoid Type Casting

半城伤御伤魂 提交于 2019-11-27 02:22:42
问题 I have following C# code in .Net 4.0. It requires a type casting of IBusiness to IRetailBusiness. //Type checking if (bus is IRetailBusiness) { //Type casting investmentReturns.Add(new RetailInvestmentReturn((IRetailBusiness)bus)); } if (bus is IIntellectualRights) { investmentReturns.Add(new IntellectualRightsInvestmentReturn((IIntellectualRights)bus)); } Business Scenario: I am designing a software system for and Investment Holding Company. The company has Retail business and

How to write the Visitor Pattern for Abstract Syntax Tree in Python?

て烟熏妆下的殇ゞ 提交于 2019-11-27 00:29:15
问题 My collegue suggested me to write a visitor pattern to navigate the AST. Can anyone tell me more how would I start writing it? As far as I understand, each Node in AST would have visit() method (?) that would somehow get called (from where?). That about concludes my understanding. To simplify everything, suppose I have nodes Root , Expression , Number , Op and the tree looks like this: Root | Op(+) / \ / \ Number(5) \ Op(*) / \ / \ / \ Number(2) Number(444) Can anyone think of how the visitor

Java visitor pattern instead of instanceof switch

天涯浪子 提交于 2019-11-26 23:05:10
问题 In this question it is said I can use visitor pattern instead of a bunch of instanceof s. Jmg said "If you are not free to change A, B, and C, you could apply the visitor pattern to achieve the same." As far as I understand I still have to make A, B and C support visitor (have an accept() method, for example). My problem is I have absolutely no possibility to change A, B and C. I just get the Car object from the foreign library and have to call wash() method specific for trucks, race cars and