开发者

Which pattern am I looking for?

开发者 https://www.devze.com 2023-03-04 19:51 出处:网络
Say I have the following interface: ICarProvider 开发者_如何学编程And the following classes implementing it:

Say I have the following interface:

ICarProvider
开发者_如何学编程

And the following classes implementing it:

RedCarProvider:ICarProvider
BlueCarProvider:ICarProvider
GreenCarProvider:ICarProvider

Now, what I would like to do is this:

Depending on the settings of the user, create a single instance of one (only one) of these classes and use that throughout the entire session of the application. So my logic should only know the interface and where to get the instance.

Is there a pattern for this?

Thanks!


It's called the factory pattern.


Singleton Factory

http://www.wikijava.org/wiki/Singleton_Factory_patterns_example

Singleton Factory method

Creating singleton factory


Abstract Factory pattern. Read more on wikipedia.org

In software development, a factory is the location in the code at which objects are constructed. The intent in employing the pattern is to insulate the creation of objects from their usage. This allows for new derived types to be introduced with no change to the code that uses the base class.


That sounds like the some sort of factory pattern.

I can't refrain myself from being sceptical about the need for a pattern to describe something so trivial.


That what you have presented seems to be a factory patter, but you should not create a class only for describing the properties of an object, sometime is better to use an map for those.

0

精彩评论

暂无评论...
验证码 换一张
取 消