Thursday, 19 September 2013

Is there a name for this creation pattern?

Is there a name for this creation pattern?

What should I be calling the "BFactory" below. Its not really a Factory
since there is no selection of a concrete class happening, and its not
necessarily creating an object each time. Its kind of a Pool but the users
do not return the Bs they get to the pool after they are done with them.
It could be called a Cache but performance is not the primary intention.
The intention is that everyone who is using the same BFactory will get the
same B when they pass the same A which starts to sound kind of like a
singleton-ish.
public class A
{
public int MyProperty { get; set; }
}
public class B
{
public B(A wrapped)
{
Wrapped = wrapped;
}
public A Wrapped { get; set; }
}
public class BFactory
{
private Dictionary<A,B> _created = new Dictionary<A,B>();
public B GetB(A a)
{
if (_created.ContainsKey(a) == false)
{
_created[a] = new B(a);
}
return _created[a];
}
}

No comments:

Post a Comment