Trying
Trying

Reputation: 14278

Abstract factory pattern relies on object composition?

While i was going through the book Head first design pattern i found that "Abstract factory relies on object composition: object creation is implemented in methods exposed in the factory interface" in the abstract factory chapter. My doubt here is how come Abstract factory relies on object composition?

if i will take the example in wikipedia then GUIFactory is an interface and WinFactory implements GUIFactory. Then how Abstract factory uses composition. Thanks in advance.

Upvotes: 3

Views: 1062

Answers (2)

Creynders
Creynders

Reputation: 4583

I think they mean that the abstract factory describes the composition for the client and in that sense relies on object composition.

To take another example, the abstract factory BodyCreator defines the methods createArm, createLeg, createHead etc. Concrete factories could be DinosaurCreator and HumanCreator. They produce all elements to create a composite "body", so that a client can call those various methods to compose one, but it doesn't need to know what type of body it exactly is composing. Just as in the wikipedia example GUIFactory describes what elements the GUI is composed of.

To make it more clear they should've added some more methods IMO: CheckBox createCheckBox(), TextField createTextField() etc.

But, yeah, maybe 'relies' wasn't the best choice of words.

Upvotes: 3

David Tansey
David Tansey

Reputation: 6023

IMHO, a blanket statement like

Abstract Factory relies on object composition

is not accurate. In this case, my sense is that this is a lazy or overly-broad use of the term object composition when defining Abstract Factory.

Wikipedia's entry for Object Composition: http://en.wikipedia.org/wiki/Object_composition is helpful here when it indicates:

In computer science, object composition [...] is a way to combine simple objects or data types into more complex ones.

Is there anything which indicates that an Abstract Factory / Factory / Factory Method cannot return simple objects or data types?

This is a rhetorical question (I think). I can't find anything.

Upvotes: 1

Related Questions