A Better Example
- I have made some changes in the newest edition Step3.
-
- The BuildingT
- Stores a reference to a warehouse.
- We don't want to destroy the warehouse before the buildings go out of scope.
- But they will not need the warehouse to be passed to them.
- Initialization requires attention to detail.
- Make sure that the warehouse is passed by reference to all instances of the constructors all the way up the line.
- Make sure that the parent constructor is called in the ctor-initalizer.
- I moved the warehouse to the protected area to allow all derived classes to have access to it.
- I removed
CanProduce
and Produce
- These are not common to all buidling types.
- I made
WorkersNeeded
pure virtual.
- This requires that all buildings that can be built have a WorkersNeeded function.
- But I don't know how they will be implmeneted.
- I added a non-vitrual
Type
- This should probably be virtual, but at this point I don't expect anyone to override it.
- I added two more abstract classes.
- Looking at the rules, there are two types of buldings
- Production buildings remove goods from the warehouse and produce other goods.
- Bonus buildings add a bonus to the sector's natural resource production.
- So I made a class for each.
- In Production Buildings
- There is only a header file.
-
Produce
and CanProduce
are pure virtual
- Again providing an intefrace but no implementation.
- BonusBuildingT
- Actually implments the
*Bonus
functions.
- Each returns a 0
- But that way I don't need to implement them unless needed in the future.
- I added a FarmT and a SawmillT as concrete classes.
- The driver has changed quite a bit.
- Everything (well nearly) is done with unique_ptr
- There is one side trip where I do a cast.
- I access the underlying raw pointer.
- But this is only for function calls
- Do not delet the temp pointer.
-
MakeBuilding
is a primitive step to a new design pattern.
- In this case, you might call it a factory function.
- There are two design patterns that encapsulate this.
- The Concrete Factory (or just factory_
- The Abstract Factory
- But for now we will not worry about these.
- They are ideal for this situation.
- Note
- The creation of the second sawmill.
- Store in a tmp pointer
- Note the use of
- The vector function
back.
- The
move
operator to move a unique_pointer