faif / python-patterns

A collection of design patterns/idioms in Python
40.2k stars 6.93k forks source link

Update abstract_factory more elaborate #370

Closed yhay81 closed 3 years ago

yhay81 commented 3 years ago

After reading #366, I think it is not necessary to add more types of things but it can be more clear.

2 Points.

1. I think abstract factory can offer a concrete factory which can provide some different things from input/choice. Then how about change show_pet() into something which can accept args like name?

2. abstract factory should have abstract_factory, concrete_factory and products. And in former abstract_factory.py

abstract_factory: Class<PetShop>
concrete_factory: Class<Dog/Cat>
products: Pet Object?, string? or also output like print("woof")?

products is not clear. So I returned Pet Object to make more clear what product is.

yhay81 commented 3 years ago

And one question, why don't you use parent class? It can force to have same methods, so we can do polymorphism in a safe way.

I know delegation is better than inheritance in many cases, but in this case I think the parent class Pet is good to be here.