OOP Design: Car - Connection between reservoir and engine

I am not sure that I am violating OOP, unsuccessfully.

Suppose there is a class Car in which < has " Engine and Tank .

When Engine running, it will consume oil from Tank (for example, unit per unit per cycle, although oil is uncountable)

How does Engine get oil from Tank ? (When are both Car fields?)

In fact, the Engine should be constantly “delivered”, and by what “oil” is received.
There must be an "OilCirculatingSystem" that gets the oil from the Tank
and delivers to Engine .
How to simulate this system in classes?
Is there a suitable design template? enter image description here

** * Edit: Just how to have an “Oil Flow” from Tank to Engine ? (Responsibility of Oil from Tank flow to Engine when opening valve?

+7
source share
4 answers

I'm sorry if he fries the brain. There is no implementation of the methods, but you get the idea, I hope.

 class Tank { Oil oil; public Tank(OilPipe pipe) { pipe.OilNeeded += new EventHandler<OilNeededEventArgs>(pipe_OilNeeded); } public void FillOil(Oil oil) { } void pipe_OilNeeded(object sender, OilNeededEventArgs e) { e.Oil = oil.SubtractLiters(5); } } class OilPipe { public event EventHandler<OilNeededEventArgs> OilNeeded; public Oil SuckOil(); } class Piston { public void Move() { } } class Oil { public Energy Burn() { } } class Energy { public void Push(Piston piston) { } } class Engine { OilPipe oilPipe; public Engine(OilPipe oilPipe, Piston piston) { this.oilPipe = oilPipe; } public void MovePiston() { Oil oil = oilPipe.SuckOil(); Energy energy = Burn(oil); energy.Push(piston); } } class Car { Engine engine; public Car(Engine engine, Tank tank) { } public void Accelerate() { engine.MovePiston(); } } 
+4
source

Automotive counterparts are never perfect, because cars and engines are actually very complex systems. You have to ignore a lot of things just to simulate them. Your problem is that you do not seem to understand how the engine works.

The oil pan is part of the engine, not the car. The gas tank is part of the car, but not the engine. You have an oil pump (also part of the engine) that pumps oil into the engine cylinders. Most cars (perhaps all) do not “check the oil level” and refuse to start. The engine will just catch if it does not get enough oil. Similarly, if he does not receive enough gas, he does not check the gas tank level. It just runs out of fuel.

It will be something like this:

 class Car { Engine engine; GasTank gasTank; StartEngine() { engine.Start(); } } class Engine { Timer timer; OilPan oilPan; OilPump oilPump; public Engine() { oilPump = new OilPump(oilPan, this); } Start() { timer.Start(oilPump); } InjectOil() {} } class Timer { OilPump op; // This is a reference public Timer(OilPump op) { _op = op; } Revolve() { op.Pump(); } } class OilPump { OilPan _oilPan; // Reference Engine _engine; // Reference OilPump(OilPan oilPan, Engine engine) { _oilPan = oilPan; _engine = engine; } Pump() { _engine.InjectOil(_oilPan.GetOil); } } 

The timer informs about the rotation of the engine, when it rotates, it drives the oil pump, which pumps oil into the cylinders. Oil is not usually consumed as fuel. It is being recycled. It can break down over time, and in some engines that are in poor condition, it can burn.

The reference to the oil pump is a mechanical connection between the engine and the oil pump (as a rule, gears rotate it). The timer does not have an oil pump; it has a link to an oil pump.

A gas tank would work in a similar way.

Again, all this is imperfect (very imperfect), because so many are lacking.

+3
source

I would say that Car itself is an OilCirculatingSystem .

 class Car { Tank tank; Engine engine; start() { //whatever } feedEngine() { while ( tank.hasOil() ) { tank.getOil(); engine.giveOil(); } } } 

Car itself is already a class connecting all your components together, why do you need another?

+2
source

The fact that the supply is continuous means that it will need to be checked after a certain period of time. If this is not what you are looking for, you may need to clarify your question.

The most logical approach to this problem (provided that you do not have programming based on events or signals / slots) is for the car to check the state of the engine, each time interval of your choice, and if the engine needs more oil, the car should read some data from tank and transfer it to the engine.

Here is a simple pseudo code example to demonstrate what I mean:

 class Engine { void start() { while(true) { // do engine stuff here } } void getOil(int amount) { // do stuff with oil } } class Tank { int oilAmount boolean hasOil int giveOil() { return oilAmount } } class Car { Tank tank Engine engine void start() { engine.start() while(true) { engine.getOil(tank.giveOil()) sleep(100) } } } 
+1
source

All Articles