Reputation: 655
I have a program that is a homework assignment. First we made a flight simulation, where we parsed xml files to load airplanes, cities, and flights. All the planes have things like cruise speeds, cruise altitudes etc. Each flight had a start time. We created a timer function and kept track of time to see when to make a plane take off and land.
Now, the instructor has asked us to implement the abstract factory pattern for creating the airplanes, and I generally understand how it works...but I can't seem to implement it.
Also, the instructor has told us to implement the factories as singletons.
What I don't understand is this:
Where would I instantiate the factories? I have a loader that loads data from the xml to a container class. I assume I want to create one factory at a time, then get all airplanes of that type from the xml...and do that for all factories.
Since there is really no difference in different 'TYPES' of planes, what the heck kind of method interface would I make other than just creating the objects outright?
Here is some sample code:
Example of an XML aircraft
<PLANE>
<MAKE>
Gulfstream 150
</MAKE>
<TYPE>
BUSINESSJET
</TYPE>
<DESCRIPTION>
Business Jet
</DESCRIPTION>
<RATEOFCLIMB>
1500
</RATEOFCLIMB>
<WINGSPAN>
55
</WINGSPAN>
<LENGTH>
56
</LENGTH>
<CRUISESPEED>
528
</CRUISESPEED>
<CRUISEALTITUDE>
41000
</CRUISEALTITUDE>
</PLANE>
The enum to use for types
enum AircraftType {NOTYPE, PASSENGERJET, BUSINESSJET, SINGLEENGINE};
The aircraft class:
class Aircraft
{
private:
char make[32];
char model[32];
double cruiseSpeed;
double cruiseAltitude;
double climbRate;
double wingspan;
double fuselageLength;
public:
Aircraft(void);
Aircraft(AircraftFactory*);
~Aircraft(void);
};
Abstract Factory class
class AircraftFactory
{
public:
AircraftFactory();
~AircraftFactory();
};
Two subclasses for example
class BusinessJetFactory: public AircraftFactory
{
private:
BusinessJetFactory();
public:
~BusinessJetFactory();
static BusinessJetFactory* getInstance();
};
class PassengerJetFactory: public AircraftFactory
{
private:
PassengerJetFactory();
public:
~PassengerJetFactory();
static PassengerJetFactory* getInstance();
};
Upvotes: 0
Views: 1034
Reputation: 6775
Normally a factory is used because you have a map that can match between a string (holding a type name or type id) -which is a string you find in your data source-, and a C++ type. You hardcode this correspondance in the main function at the initialization moment of your factory. You use one "abstract" base factory class, and one template factory that derives from the base one. The global (singleton) container, is just the map of factories (bind strings to factories) and this is where the hardcoding matters, you will instantiate (C++ term for "realizing a concrete class from a template") a factory for each necessary types. One factory has a make
function that just returns a T*
(with T
the template type of the factory, and coincidentally the type of the objects it can create). All those factories are stored in the map, which is then known as a polymorphic container. the make function being virtual, the map contains only pointers to the base factory, but since the actual objects contained in there each knows its true make
function (through virtual table) when you call auto newobject = factorymap["passenger"]->make()
you get a new instance of the type that you specified in your hardcoded init of the factory that is in the "value" (side of the key_value) of the map for this element.
Now the exercice seems ill suited for that, because like you said, there is only one type of planes, so you need no factory whatsoever. Except if your xml holds something else than planes, like airports, passengers, runways, control towers, airline companies...
Upvotes: 1