Note that the factory method doesn’t have to create new instances all the time. However, the primary function of the company as a whole is still writing code, not producing programmers.Ĭoncrete Creators override the base factory method so it returns a different type of product. #WEBOOK NATIVE COMPONENTS SOFTWARE#Here is an analogy: a large software development company can have a training department for programmers. The factory method helps to decouple this logic from the concrete product classes. Usually, the creator class already has some core business logic related to products. Note, despite its name, product creation is not the primary responsibility of the creator. As an alternative, the base factory method can return some default product type. You can declare the factory method as abstract to force all subclasses to implement their own versions of the method. It’s important that the return type of this method matches the product interface. The Creator class declares the factory method that returns new product objects. The Product declares the interface, which is common to all objects that can be produced by the creator and its subclasses.Ĭoncrete Products are different implementations of the product interface. The client knows that all transport objects are supposed to have the deliver method, but exactly how it works isn’t important to the client. The client treats all the products as abstract Transport. #WEBOOK NATIVE COMPONENTS CODE#The code that uses the factory method (often called the client code) doesn’t see a difference between the actual products returned by various subclasses. The factory method in the RoadLogistics class returns truck objects, whereas the factory method in the SeaLogistics class returns ships.Īs long as all product classes implement a common interface, you can pass their objects to the client code without breaking it. 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. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |