When is a factory method better than a simple factory and vice versa?

I work through the book "First Design Samples".

I believe that I understand the simple factory and factory method, but it's hard for me to understand what advantages the simple factory method brings.

If Object A uses a simple factory to create its B-objects, clients can create it as follows:

A a = new A(new BFactory());

whereas if the object uses the factory method, the client can create it as follows:

A a = new ConcreteA(); // ConcreteA contains a method for instantiating 
                       // the same Bs that the BFactory above creates, with 
                       // the method hardwired into the subclass of A, ConcreteA.

So, in the case of a simple factory, clients make up A with B factory, while using the factory method, the client selects the appropriate subclass for the types of B that he wants.

It seems that there really is not much choice between them. Either you need to choose which BFactory you want to build A with, or you need to choose the correct subclass of A to give you Bs.

?

!

: . - , Head First, factory factory, ( .119), " [] factory , : -". -, , -, - .

factory, bake() . factory, , .

, , Head First factory factory, .

+5
3

UML . 117 131. , Factory , PizzaStore. Factory PizzaFactory , . . , , . Factory, , . Factory, ( ) , . Factory, ( ) , , , . , , , .

, Design Pattern GoF, , .

+1

: A A. , , .

A myA = new SomeConcreteA()

A , Bs . - , Bs A. A A, , , , , B, .

A myA = new A(myBFactory())

Bs , factory . hs , , , - . A , B.

, B factory, mocks Bs, . , , .

+1

, , Simple Factory Factory Method, , Simple Factory Factory, . Factory Factory; . , , . , - . , , , .

0

All Articles