Note that there are some explanatory texts on larger screens.

plurals
  1. POAbstract Factory, Dependency Injection and good Design
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI don't know how useful this is, but I'd really start by finding a realistic use case. All those made-up examples like modelling cars and animals just encourage you to attempt to model **everything**. The only way to achieve good design is to model only that which needs modelling. In real-world cases, that's fairly easy. But when modelling make-pretend cars, where do you stop? Should I model each wheel separately? What about the doors? Engine power? Car radio? And so on.
      singulars
    2. COThere are a million details in a car that you *could* model explicitly, or you could pretend they don't exist. And making *that* decision is what gives you a good design. Correctly determining what *needs to be in your model at all*, and what should just be left out. And that is why made-up examples like this always just lead to a big soup of inheritance hierarchies, rather than a (good) design.
      singulars
    3. COWhy do so many functions need to be virtual? Could some of the polymorphism benefit from being implemented with templates? Do you need separate classes for the different engine types? Could they be implemented as a single class that is just initialized differently? Probably, but it is impossible to tell, because it's a made-up example and so it is missing all the context you need to answer these questions. Until you know what these car classes are *for*, there's no reason to write more than this: `struct Car {}; Car vwGolf;` to define and instantiate a car. Beyond that it depends on context
      singulars
 

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