![]() Each class implements this method differently: trucks deliver cargo by land, ships deliver cargo by sea. Also, the factory method in the base class should have its return type declared as this interface.Īll products must follow the same interface.įor example, both Truck and Ship classes should implement the Transport interface, which declares a method called deliver. There’s a slight limitation though: subclasses may return different types of products only if these products have a common base class or interface. However, consider this: now you can override the factory method in a subclass and change the class of products being created by the method. Subclasses can alter the class of objects being returned by the factory method.Īt first glance, this change may look pointless: we just moved the constructor call from one part of the program to another. Objects returned by a factory method are often referred to as products. ![]() Don’t worry: the objects are still created via the new operator, but it’s being called from within the factory method. ![]() ![]() The Factory Method pattern suggests that you replace direct object construction calls (using the new operator) with calls to a special factory method. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |