Designing classes and functions for the interaction of objects

I tried to learn OOP methods and design patterns for a while, and my approach has certainly improved, but I don’t quite click when we talk about objects of interacting classes. I mainly work in PHP, but I would accept a general answer for the sake of learning. And to apologize in advance, if I use the conditions incorrectly, I'm self-taught here. Here is an example:

Suppose I want to simulate people moving in a car. There are 3 classes:

class Car {}
class Driver {}
class Road {}

and I want the Car class to have a function

public function Drive($time) {}

in which driving should update the driver’s location, depending on the speed limit of the road. My question is the best way to structure Drive so that the car interacts with other objects. I see 3 possibilities from the head:

public function Drive($time, $Driver, $Road) {}

, , . , , , , , ..

$Car->setDriver($Driver);
$Car->setRoad($Road);
$Car->Drive($time);

, Drive , , . , , , .

class Car() {
  public function Drive($time) {
    $Driver = getDriver($this->DriverID);
    $Road = getRoad($this->RoadID);
  }
}
function getDriver($DriverID) {
  static $DriverArray;
  // check isset(), create object and place in array if not
  return $DriverArray[$DriverID]; 
}
function getRoad($RoadID) {} // assume similar

, , DriverID RoadID, db. ( , , .) , getDriver, .

SELECT <columns> FROM Car_tbl INNER JOIN Driver_tbl on DriverID INNER JOIN Road_tbl on RoadID, , , OO, , , .

3, , - . PHP , . .


edit: , . , , , UseCase , UseCase . , , getRoadSpeedLimit(), getMaxCarSpeed() getMaxDriverSpeed(), . , -, , ..

UMLCat, , ( ), , ?

class RoadTrip_aka_Universe {
  // Operates on instances of Car, Driver, Route (which may contain roads)
}

: $Car, $Driver $Route , , ? , , , ?

, , ? , , , .

.

+5
3

-, , , , , "" "". .

class CarClass
{

} // class CarClass

class DriverClass
{

} // class DriverClass

class RoadClass
{

} // class RoadClass

class WorldClass
{
  /* public CarClass */ $Car;
  /* public DriverClass */ $Driver;
  /* public RoadClass */ $Road;
} // class WorldClass

. - :

 /* void */ function Example()
 {
    WorldClass $MyWorld = new WorldClass();

    // ...
 } // void function Example

. "", "", ""? , " " .

class RoadClass
{
   /* public int */ $SpeedLimit; 
} // class RoadClass

/* void */ function Example()
{
   WorldClass $MyWorld = new WorldClass();

   $MyWorld->Road->SpeedLimit = "50" // ouch, "cow country"
    // ...
} // void function Example

. , . , , , , .

"", . , .

class WorldClass
{
  /* public depends CarClass */ $Car;
  /* public depends DriverClass */ $Driver;
  /* public depends RoadClass */ $Road;

  function __construct() {
     // parent::__construct();

     // "World" "contains" these objects
     $this->Car = new CarClass();
     $this->Driver = new DriverClass();
     $this->Road = new RoadClass();
  } // function __construct()

} // class WorldClass

, , , , . , .

class CarClass
{
   /* public int */ $Distance; 
} // class RoadClass

"" :

class CarClass
{
   /* public int */ $Distance; 
   /* public DriverClass related */ $Driver; 
   /* public RoadClass related */ $Road; 


  function __construct() {
     // parent::__construct();

     // "Car" only relates these objects
     $this->Driver = null;
     $this->Road = null;
  } // function __construct()
} // class RoadClass

/* void */ function Example()
{
   WorldClass $MyWorld = new WorldClass();
   $MyWorld->Road->SpeedLimit = "50"; // ouch, "cow country"

   $MyWorld->Car->Road = $MyWorld->Road; // a reference only
   $MyWorld->Car->Driver = $MyWorld->Driver; // a reference only

    // ...
} // void function Example

, :

class CarClass
{
   /* public int */ $Distance; 
   /* public DriverClass related */ $Driver; 
   /* public RoadClass related */ $Road; 

  function __construct() {
     // parent::__construct();

     // "Car" only relates these objects
     $this->Driver = null;
     $this->Road = null;
  } // function __construct()


  /* void */ function Drive($time) {

    // general idea of formula:

    $this->distance = ($this->SpeedLimit * $time * $this->Road->SpeedLimit);
  } // void function Drive(...)

} // class RoadClass

/* void */ function Example()
{
   WorldClass $MyWorld = new WorldClass();
   MyWorld->Road->SpeedLimit = "50"; // ouch, "cow country"

   $MyWorld->Car->Road = $MyWorld->Road; // a reference only
   $MyWorld->Car->Driver = $MyWorld->Driver; // a reference only

   /* int */ $time = 60; // 1 hours in minutes

   $MyWorld->Car->Drive($time);
} // void function Example

.

+3

, . , . , , .

. GRASP (object-oriented_design)

UseCase, , . , , . :

  • , .
  • .
  • .

, () UseCase :

UseCase UC1:

  • : DriveSim
  • :
  • :
  • :
  • :
  • :
  • :
    • Road to travel

, , , UseCase . , - Location. , . GPSCoordindates? StreetAdresses? DistanceTraveled?

, Driver, , . , , : Roadtrip. Car Road , , Route. StartingLocation many Roads, SpeedLimit.

UseCase , , , , . , Roadtrip. ? , , , , .

+2

, , - S.O.L.I.D

, , , , . SOLID - , , , .

. http://en.wikipedia.org/wiki/SOLID_ (object-oriented_design)

+1
source

All Articles