Note that there are some explanatory texts on larger screens.

plurals
  1. POAbstract Factory, Dependency Injection and good Design
    text
    copied!<p>I am trying to get the knack of good system design. As there is no hard and fast rules for good system design, I request you to give me some valuable suggestions. I prepared an imaginary system and prepared a design for that. Please let me know if you think if this design is good or bad. Is there a better way? I used Abstract Factory and Dependency Injection in the solution.</p> <p>Problem:</p> <p>Design a system which runs in cars and controls it. The requirement is to make a system for Volkswagen Golf controlling components like engine, electrical, gearbox etc.</p> <p>There can be different versions of Golf like BlueMotion, Twist, GT etc which uses different components. For example if GT uses engine a and electrical b, BlueMotion can use engine c and electrical d. </p> <p>The engines, electricals and other components in different cars could be similiar, or could be completely different. For example a "Flat 4 CRDI" Engine uses CRDI technology for fuel injection and have a certain behaviour to reduce vibration. A "PumpeDuse V6" engine uses PD injection and its own method for reducing vibration which is completely different from "Flat4 CRDI". The difference is not only in the handling the vibrations but also most of the aspects of Engine behaviour.</p> <p>As of now, the system model should handle the starting of Engine. Starting the engine means use power from the electrical system and crank the engine and keep idling. To start with, make a Golf "GT" with "Pumpe Duse E250" Engine and "BoschR10" Electrical system. However, many different series could be launched in the future and system should be able to extend the functionality and car models with minimal trouble.</p> <p>Update: Code edited as per suggestions.</p> <p>Code:</p> <pre><code>class Electrical { public: virtual void OpenCircuit() = 0; }; class Engine { public: virtual void Crank(Electrical *) = 0; }; class CarComponentFactory { public: virtual Engine* CreateEngine()=0; virtual Electrical* CreateElectrical()=0; }; class PumpeDuseE250:public Engine { virtual void Crank(Electrical *pEle) { pEle-&gt;OpenCircuit(); //Do the crank } }; class BoschR10: public Electrical { virtual void OpenCircuit() { //Open the Circuit } }; class GTFactory:public CarComponentFactory { public: virtual Engine* CreateEngine() { return new PumpeDuseE250(); } virtual Electrical* CreateElectrical() { return new BoschR10(); } }; class VWGolf { auto_ptr&lt;Engine&gt; mpEngine; auto_ptr&lt;Electrical&gt; mpElectrical; public: VWGolf(Engine *pEngine, Electrical *pElectrical):mpEngine(pEngine), mpElectrical(pElectrical) { } void Start() { mpEngine-&gt;Crank(mpElectrical.get()); } ~VWGolf() { } }; </code></pre> <p>Example for Creating Objects:</p> <pre><code>void main() { GTFactory Factory; VWGolf golfGT(Factory.CreateEngine(), Factory.CreateElectrical()); golfGT.Start(); } </code></pre> <p>Note that the names like "BoschR10" are imaginary. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload