Is there a pattern that allows a constructor to be called only from a specific factory and from nowh

Posted by willem on Stack Overflow See other posts from Stack Overflow or by willem
Published on 2010-03-17T07:24:35Z Indexed on 2010/03/17 7:31 UTC
Read the original article Hit count: 266

Filed under:

We have a class, say LegacyUserSettingsService. LegacyUserSettingsService implements an interface, IUserSettingsService.

You can get an instance of the IUserSettingsService by calling our ApplicationServicesFactory. The factory uses Spring.NET to construct the concrete LegacyUserSettingsService.

The trouble is that new developers sometimes do their own thing and construct new instances of the LegacyUserSettingsService directly (instead of going via the factory).

Is there a way to protect the constructor of the concrete class so it can only be called from the factory? A well-known pattern perhaps?

Note that the concrete class resides in a different assembly (separate from the Factory's assembly, so the internal keyword is not a solution). The factory assembly references the other assembly that contains the concrete class.

Any ideas?

© Stack Overflow or respective owner

Related posts about design-patterns