Abstract Factory, Factory Method, Builder

2019-03-08 02:15发布

It may seem as if this is question is a dupe, but please bear with me - I promise I've read the related posts (and the GOF book).

After everything I've read, I still don't have it clear when to use an Abstract Factory, a Factory Method, or a Builder. I believe it will finally sink in after I see a simple example of a problem which is best approached by, say, a builder and it would be clearly silly to use, say, an abstract factory.

Can you provide a simple example where you would clearly use one pattern and not the others?

I understand it may boil down to a matter of opinion if the example is too simple, but I'm hopeful that if anybody can, that person is in SO.

Thanks.

7条回答
我想做一个坏孩纸
2楼-- · 2019-03-08 02:54

I wrote an article the other day with a goal to compare the Factory Method pattern and the Builder pattern. You can find it here: http://www.shenghua.co.uk/factory-method-vs-builder/. Hope it can offer some helps too.

From my own experience, I found the Head First Design Pattern book is also a really good book talking about Design Patterns. Different to the one written by Erich Gamma, it approached every Design Pattern by introducing an issue caused by when design patterns aren't used and then stepped up to how a design pattern can solve that problem. It was a very enjoyable reading for me. It also made the one written by Erich Gamma much easier to understand after finish reading it.

查看更多
登录 后发表回答