Re: abstract factory -- a doubt ?
- From: daniel_t@xxxxxxxxxxxxx
- Date: 25 Apr 2006 15:54:17 -0700
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.
.
- Follow-Ups:
- Re: abstract factory -- a doubt ?
- From: viveklinux
- Re: abstract factory -- a doubt ?
- References:
- abstract factory -- a doubt ?
- From: viveklinux
- Re: abstract factory -- a doubt ?
- From: Daniel T.
- Re: abstract factory -- a doubt ?
- From: vivekaseeja
- abstract factory -- a doubt ?
- Prev by Date: Re: abstract factory -- a doubt ?
- Next by Date: Re: abstract factory -- a doubt ?
- Previous by thread: Re: abstract factory -- a doubt ?
- Next by thread: Re: abstract factory -- a doubt ?
- Index(es):
Relevant Pages
|