Re: abstract factory -- a doubt ?



vivekaseeja@xxxxxxxxx wrote:
Daniel T. wrote:

"Prevent" is a pretty strong word in my book. I don't think Abstract
Factory does prevent violations of OCP. For example say I have a set of
related types that the factory is supposed to create, and I want to add
another type to that set... I've got to open every sub-class of the
Abstract Factory (as well as the Abstract Factory itself)...

m not too sure that i understand .. esp the part which says "open ..
the Abstract Factory itself" .. could you explain with an example ?

[Note: when I use a word with a capital first letter, I'm speaking of
the class. A lowercase first letter means an object of that class.]

Let's say we are making a computer strategy game. It has several Army
classes which use Tanks and APCs. Each Army is supposed to start with
kinds of Tanks and APCs that are unique to that army, but once the game
starts, they can capture each-others tank and apc objects and use them
the same way they use their own.

One way to set that up is to have an Army class with ArmyA, ArmyB, &c
subclasses. A Tank class, with TankA, TankB, &c. and an APC class with
APCA, APCB, &c. The Army subclasses always use tanks and apcs through
their parent abstract type so they can use captured objects without
needing any special code.

How do we set-up each army with its own tanks and apcs? One way would
be to create an AbstractFactory with subclasses FactoryA, FactoryB,
&c... each army is given a factory that creates its tanks and apcs for
it. This allows the various armies to create tanks and apcs all the
same way allowing us to move much of the creation logic into the base
Army class.

Now that the set-up is done, I'll explain what I mean by "open up every
sub-class of the Factory and even the Abstract Factory itself."...
Suppose word from on high says our game now includes Artillery objects.
Now we also have Artillery, ArtilleryA, ArtilleryB, &c. This means the
Abstract Factory, and every sub-class of the factory needs a new method
"createArtillery"... Every sub-class of Factory needs to be modified,
as well as the AbstractFactory itself to accommodate this change. A
clear violation of the open-closed principle.

Are there ways to get around such a problem? Only if you use a
different creational pattern., however if you don't expect that such a
thing will happen, the AbstractFactory may be the best solution.

.



Relevant Pages

  • Re: using jMock with components
    ... >> chance to mock it. ... Whilst running normally the factory class returns ... > but don't worry about Abstract factory it is a valid approach. ... >> I created a factory interface which exposed the components I needed to ...
    (comp.lang.java.programmer)
  • Re: Difference between Factory and Abstract Factory
    ... abstract factory. ... these are like Abstract Factory and ... Factory Method respectively. ... Override methods like createThing() *in the factory*. ...
    (comp.object)
  • Re: using jMock with components
    ... Whilst running normally the factory class returns ... Usually its fine to have the createXXXComponent method on the abstract factory itself, which internally just delegates to the concrete factory. ... The reason I created the two factories is so I can omit the mock factory when I make a distribution. ... with the interface and merge the abstract and real factory classes together and create a getComponentmethod which creates a newInstance of the class using reflection, and have the test factory override that method. ...
    (comp.lang.java.programmer)
  • Re: Difference between Factory and Abstract Factory
    ... This is my take on the Abstract Factory. ... I was too lazy to create a concrete factory for every day of the week, ... public string GetDescription() ... class SaturdayMenu: IMenu ...
    (comp.object)
  • ARM: Review - DML 1/35 scale T-34 Factory No. 112 - Late Production
    ... release of popular T-34 variant as a mainline kit good ... The “home” or lead factory was Factory No. 183 in Kharkov, ... production of more than 2,000 tanks per year.After the war began, on ... hatch for gun barrel replacement access; without it the turret had to ...
    (rec.models.scale)