c++ automatic factory registration of derived types

I use a singleton with a member for registration, basically: template< typename KeyType, typename ProductCreatorType > class Factory { typedef boost::unordered_map< KeyType, ProductCreatorType > CreatorMap; … }; Using Loki I then have something along these lines: typedef Loki::SingletonHolder< Factory< StringHash, boost::function< boost::shared_ptr< SomeBase >( const SomeSource& ) > >, Loki::CreateStatic > SomeFactory; Registration is usually … Read more

Using a Strategy and Factory Pattern with Dependency Injection

There are a few ways of doing this, but the way I prefer is to inject a list of available strategies into your factory, and then filtering them to return the one(s) you’re interested in. Working with your example, I’d modify IShippingStrategy to add a new property: public interface IShippingStrategy { int CalculateShippingCost(Order order); string … Read more

Factory, Abstract Factory and Factory Method

The Gang Of Four “Design Patterns; Elements of Reusable Object-Oriented Software” book contains two entries, “Abstract Factory” (aka ‘Virtual Constructor’) and “Factory Method”. I don’t know about “Concrete Factory.” I’ve heard the term, but never given it too much thought. Factory Method In “Factory Method” an object has a method which is responsible for the … Read more

What is the difference between Builder Design pattern and Factory Design pattern? [closed]

With design patterns, there usually is no “more advantageous” solution that works for all cases. It depends on what you need to implement. From Wikipedia: Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, … Read more

How to avoid ‘instanceof’ when implementing factory design pattern?

You could implement the Visitor pattern. Detailed Answer The idea is to use polymorphism to perform the type-checking. Each subclass overrides the accept(Visitor) method, which should be declared in the superclass. When we have a situation like: void add(Vehicle vehicle) { //what type is vehicle?? } We can pass an object into a method declared … Read more

“Downcasting” unique_ptr to unique_ptr

I’d create a couple of function templates, static_unique_ptr_cast and dynamic_unique_ptr_cast. Use the former in cases where you’re absolutely certain the pointer is actually a Derived *, otherwise use the latter. template<typename Derived, typename Base, typename Del> std::unique_ptr<Derived, Del> static_unique_ptr_cast( std::unique_ptr<Base, Del>&& p ) { auto d = static_cast<Derived *>(p.release()); return std::unique_ptr<Derived, Del>(d, std::move(p.get_deleter())); } template<typename … Read more

Why does Hibernate require no argument constructor?

Hibernate, and code in general that creates objects via reflection use Class<T>.newInstance() to create a new instance of your classes. This method requires a public no-arg constructor to be able to instantiate the object. For most use cases, providing a no-arg constructor is not a problem. There are hacks based on serialization that can work … Read more

tech