Re: Abstract factory pattern



Responding to Johansson...

Assume you have three concrete factories cadillac, Volvo and Fiat.
We also assume that each one only consist of Engine, Chassis and Steering.
So in the abstract factory and the concreate factories you will have createEngine, createChassis and createSteering.


Normally a single instance of a Concretefactory class is created at run-time.

Why? Typically one instantiates an instance of each concrete factory at startup. One then instantiates a relationship to the correct concrete instance based on the local context (i.e., whether the client needing instances should have Cadillac, Volvo, or Fiat stuff).


         *  instantiates thru 1
[Client] ---------------------- [CarFactory]
               R1               + createEngine
                                + createChassis
                                + createSteering
                                     A
                                     | R2
                         +-----------+------------+
                         |           |            |
                   [VolvoFact]   [FiatFact]   [CadillacFact]

The Client that needs to create products asks the CarFactory for engines, chassises, or steering assemblies. As Malik points out, when it asks for those things via creatEngine, etc. it does not know which manufacturer's version it is getting.

The specific manufacturer's version is determined by instantiating the R1 relationship to the correct subclass context for the collaboration. Once that context is defined, Client will always get the "right" manufacturer's stuff thereafter. Generally the rules and policies that determine which concrete factory a given Client should be associated with will be distinct from those the determine /when/ a Client should create instances. So they are likely to be encapsulated in some other object that understands that context. IOW, the Client understand When to collaborate but someone else understands Who it should collaborate with.


************* There is nothing wrong with me that could not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions  -- Put MDA to Work
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
(888)OOA-PATH



.