When is it a good idea to use factory methods within an object instead of a Factory class?
相关问题
- Name for a method that has only side effects
- What is a correct approach to manage test data usi
- Can a [GoF]-ConcreteSubject override the notify me
- Can the builder pattern ever be doing too much?
- Carry STRef implicitly in an environment during co
相关文章
- Why is this factory returning a $$state object ins
- Builders in Java versus C++?
- DI container, factory, or new for ephemeral object
- “Adapter” or “adaptor”?
- Objective-C: Use singleton vs. use class as an obj
- Is copy-and-paste coding ever acceptable?
- How to Create a Custom tabBarController to simulat
- Dependency Injection Container
if you want to create a different object in terms of using. It is useful.
They're also useful when you need several "constructors" with the same parameter type but with different behavior.
Factory methods should be considered as an alternative to constructors - mostly when constructors aren't expressive enough, ie.
is not as expressive as:
Factory classes are useful when you need a complicated process for constructing the object, when the construction need a dependency that you do not want for the actual class, when you need to construct different objects etc.
Factory classes are useful for when the object type that they return has a private constructor, when different factory classes set different properties on the returning object, or when a specific factory type is coupled with its returning concrete type.
WCF uses ServiceHostFactory classes to retrieve ServiceHost objects in different situations. The standard ServiceHostFactory is used by IIS to retrieve ServiceHost instances for .svc files, but a WebScriptServiceHostFactory is used for services that return serializations to JavaScript clients. ADO.NET Data Services has its own special DataServiceHostFactory and ASP.NET has its ApplicationServicesHostFactory since its services have private constructors.
If you only have one class that's consuming the factory, then you can just use a factory method within that class.