What are two benefits of a Factory design pattern?

What are two benefits of a Factory design pattern?

What are two benefits of a Factory design pattern?

A.
Eliminates direct constructor calls in favor of invoking a method

B.
Provides a mechanism to monitor objects for changes

C.
Eliminates the need to overload constructors in a class implementation

D.
Prevents the compile from complaining about abstract method signatures

E.
Prevents tight coupling between your application and a class implementation

Explanation:
Factory methods are static methods that return an instance of the native class.
Factory methods :
* have names, unlike constructors, which can clarify code.
* do not need to create a new object upon each invocation – objects can be cached and reused, if necessary.
* can return a subtype of their return type – in particular, can return an object whose implementation class is unknown to the caller. This is a very valuable and widely used feature in many frameworks which use interfaces as the return type of static factory methods.
Note: The factory pattern (also known as the factory method pattern) is a creational design pattern. A factory is a JavaSW class that is used to encapsulate object creation code. A factory class instantiates and returns a particular type of object based on data passed to the factory. The different types of objects that are returned from a factory typically are subclasses of a common parent class.
The data passed from the calling code to the factory can be passed either when the factory is created or when the method on the factory is called to create an object. This creational method is often called something such as getInstance or getClass .



Leave a Reply 0

Your email address will not be published. Required fields are marked *