ramoh
ramoh

Reputation: 153

A singleton class creation with factory method

My singleton class implements a interface and in future I can expect many concrete implementation to come out . I am thinking of creating an instance of this class through factory method. factory method may be overloaded . My question is how good or bad this idea is ?

Upvotes: 1

Views: 753

Answers (2)

Bohemian
Bohemian

Reputation: 424983

It is irrelevant if the instance returned from your factory method is a singleton. That is an implementation choice - do what is correct for your need.

Also, if your method signature is returning an interface, then technically it's an abstract factory method, not a factory method.

Upvotes: 0

Deepak Bala
Deepak Bala

Reputation: 11185

Based on your updated comment, sure you can do that. A factory method dishes out singleton implementations of your persistence classes based on the overloaded parameters or an enum / integer value in the parameter. There are many frameworks out there that use this pattern, say to give you instances of a client that communicate with a server based on different protocols.

MyFactory.pbClient("host", port);
MyFactory.httpClient("host", port);

Upvotes: 1

Related Questions