Factory Pattern – CreateInstance static or not?

I’m very hesitant to categorize “instance versus static” as a matter of taste. This sort of implies that it’s aesthetic like a favorite color or, more appropos, camelCase versus PascalCase. Instance versus static is more a question of tradeoffs. With instance members of any kind, you get all of the benefits of polymorphism, since you … Read more

Real world examples of Factory Method pattern

A class implementing factory design pattern works as bridge between multiple classes. Consider an example of using multiple database servers like SQL Server and Oracle. If you are developing an application using SQL Server database as backend, but in future need to change backend database to oracle, you will need to modify all your code, … 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

Is this Factory Method creation pattern?

I would agree to call the method a “Factory Method”, though the design is not strictly a “Factory Method Pattern”. Here is a key point (from Wikipedia): …The Factory method lets a class defer instantiation to subclasses.” Since your class is static and method static (hence non-virtual), there is no “deferring” possible. Conceptually, notice also, … 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

instance factory methods Vs Static factory methods

Assuming that by “instance factory method” you’re actually saying about the GoF “factory method”, the term “static factory method” is described by Joshua Bloch in his book “Effective Java”. Googling around I reached at these sites: Factory Method: http://sourcemaking.com/design_patterns/factory_method Static Factory Method: http://www.informit.com/articles/article.aspx?p=1216151 Hope that it helped to make the difference a little bit clearer. … 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

Implement a simple factory pattern with Spring 3 annotations

The following worked for me: The interface consist of you logic methods plus additional identity method: public interface MyService { String getType(); void checkStatus(); } Some implementations: @Component public class MyServiceOne implements MyService { @Override public String getType() { return “one”; } @Override public void checkStatus() { // Your code } } @Component public class … Read more

How do I pass values to the constructor on my wcf service?

You’ll need to implement a combination of custom ServiceHostFactory, ServiceHost and IInstanceProvider. Given a service with this constructor signature: public MyService(IDependency dep) Here’s an example that can spin up MyService: public class MyServiceHostFactory : ServiceHostFactory { private readonly IDependency dep; public MyServiceHostFactory() { this.dep = new MyClass(); } protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) … Read more