Is the Factory method template more flexible than Simple Factory?

I read the book Head First: Design Patterns , which I found a good introduction to design patterns. However, I have a question about the claim they make in chapter 4:

They define the Simple Factory pattern as follows (Java pseudo-code):

public abstract class Product { // Product characteristics // Concrete Products should subclass this } public class SimpleFactory { public Product createProduct(){ // Return an instance of some subclass of Product } } public class Store { SimpleFactory factory; public Product orderProduct(){ Product product = factory.createProduct(); // Do some manipulation on product return product; } } 

The "Factory Method" is defined as follows (the Product class remains the same and is omitted):

 public abstract class Store { //Concrete Stores must subclass this and override createProduct() public abstract Product createProduct(); public Product orderProduct(){ Product product = createProduct(); // Do some manipulation on product return product; } } 

The authors then argue that the Factory method template is much more flexible than Simple Factory, because while Simple Factory is “a one-shot deal, using the Factory method you create a structure that allows subclasses to decide which implementation should be used” (p. . 135).

Now I do not understand why this is so. The way I see it, Simple Factory is in some ways a little more flexible than Factory Method: you can subclass Simple Factory (instead of subclass Store) to get essentially the same behavior. You can even change the runtime behavior if you want! The only drawback of Simple Factory that I could think of is when the creation of the product depends on the state variables of the Store class: is it that the authors are calling for flexibility, or am I missing something?

+5
source share
1 answer

You are absolutely right: the author suggested that you are not going to subclass SimpleFactory , which is not a fair assumption (if SimpleFactory not marked final ).

Since SimpleFactory not final, you can subclass it with more flexibility than the factory method, because SimpleFactory replaces inheritance with composition.

An even better approach would be to create a SimpleFactory interface. This will allow you to choose a composition or inheritance according to your preferences, as the interface will not limit you when your Store class already inherits the class.

 public interface SimpleFactory { Product createProduct(); } 

Then you can use either composition

 public class FactoryImpl implements SimpleFactory { public Product createProduct(){ // Return an instance of some subclass of Product } } public class StoreComposition { SimpleFactory factory = new FactoryImpl(); } 

or a combination of inheritance / composition

 public class StoreInheritance implements SimpleFactory { SimpleFactory factory = this; public Product createProduct(){ // Return an instance of some subclass of Product } } 
+2
source

Source: https://habr.com/ru/post/1205291/


All Articles