NAME: FACTORY METHOD
CONTEXT: It allows the creation of objects without specifying the type of object that is to be created. A factory class contains a method that allows determination of the created type at run time.
PROBLEM: Consider a framework where the application at runtime cannot anticipate the class of object that is must create. The Application may know that it has to instantiate classes, but it may only know about abstract classes or interfaces, which it cannot instantiate. Thus the application class may only know when it has to instantiate a new Object of a class, not what kind of subclass to create.
SOLUTION: The factory method is a kind of creational design pattern to implement the concept of factories. This pattern helps to model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate. We call this a factory pattern since it is responsible for "Manufacturing" an Object. It helps instantiate the appropriate Subclass by creating the right Object from a group of related classes. Obviously, a factory is not needed to make an object. A simple call to “new” will do it for you. However, the use of factories gives the programmer the opportunity to abstract the specific attributes of an Object into specific subclasses which create them.
No comments:
Post a Comment