对象分析类

时间:2018-01-18 10:27:00

标签: php oop

目前设计用于分析车辆路线的课程。

例如,我需要计算各个部分的燃料价格,然后将它们总结为例如每月路线。

整个月都有一种燃油价格。

我这样计划:

class FullRoute{
   public function setFuelCost($cost){}

   public function addRoute(Route $route){}

   public function getDistance(){
    // sum routes distance
   }
}

class Route{
   public function setFuelCost($cost){}

   public function setDistance($distance){}

   public function getDistance(){}

   public function getCost(){
        //returns cost of route;
   }

}

我现在想知道这是一个好方法,还有更多方法 - 这只是一个例子。

从FullRoute课程中我想总结一下,这就是我添加setFuelCost方法的原因 - 摘要是返回从所有剧集计算的距离以及标准(作为燃料费率 - 这就是为什么这种方法)。

也许有人解决了类似的问题并对如何设计提出了一些建议? 谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

也许你可以尝试这样的事情:

class Route {

   public function setDistance($distance_miles) {

   }

   public function getDistance() {

   }

   public function getCost($price_per_galon, $miles_per_galon) {

   }

   public function getFuelUsed($miles_per_galon) {

   }
}

class RoutesSummary {

   public function __construct($price_per_galon, $miles_per_galon) {

   }

   public function addRoute(Route $route) {

   }

   public function getTotalDistance(){
     // sum routes distances
   }

   public function getTotalCost() {
     $cost = 0;

     foreach($this->routes as $route) {
       $cost += $route->getCost(
          $this->price_per_galon, 
          $this->miles_per_galon
       );
     }

     return $cost;
   }

   public function getTotalFuelUsed() {
     // sum routes fuel used
   }

   public function getDetail() {
     // return list of route costs
   }
}

答案 1 :(得分:0)

此外,您可以使用SplSubjectSplObserver设计模式。

这将允许您将RouteSection(SplObserver)注入Route(SplSubject)并一次更新报告,而无需在两个类中都使用getter和setter。

您需要一个实例$route来获取您的报告/数据。

这是一个例子,我认为它涵盖了你的事后:

<?php

/**
 * Route
 */
class Route implements \SplSubject {
    private $data;
    private $observers = array();
    private $content;

    public function __construct($data) {
        $this->data = $data;
        $this->report = [];
        $this->total_cost = 0;
        $this->total_distance = 0;
    }

    // getters
    public function getRoute() {
        return $this->data['name'];
    } 

    public function getDistance() {
        return $this->data['distance'];
    }

    public function getReport() {
        return $this->report;
    }    

    public function getTotalCost() {
        return number_format($this->total_cost, 2);
    }

    public function getTotalDistance() {
        return $this->total_distance;
    }

    // run
    public function run() {
        $this->notify();
    }

    // spl pub/sub methods
    public function attach(\SplObserver $observer) {
        $this->observers[] = $observer;
    }

    public function detach(\SplObserver $observer) {
        $key = array_search($observer, $this->observers, true);
        if ($key) {
            unset($this->observers[$key]);
        }
    }

    public function notify() {
        foreach ($this->observers as $value) {
            $value->update($this);
        }
    }
}

/**
* RouteSection
*/
class RouteSection implements SplObserver {
    private $vehicle;
    private $driver;

    public function __construct($vehicle, $driver) {
        $this->vehicle = $vehicle;
        $this->driver = $driver;
    }

    public function update(\SplSubject $subject) {
        $subject->report[$this->vehicle['id']] = [
            'route'    => $subject->getRoute(),
            'vehicle'  => $this->vehicle,
            'driver'   => $this->driver,
            'distance' => $subject->getDistance(),
            'cost'     => $subject->getDistance() * $this->vehicle['ppm']
        ];

        $subject->total_distance += $subject->report[$this->vehicle['id']]['distance'];
        $subject->total_cost += $subject->report[$this->vehicle['id']]['cost'];
    }
}

// define some drivers
$drivers[] = ['id' => 1, 'name' => 'Lawrence'];
$drivers[] = ['id' => 2, 'name' => 'Paul'];
$drivers[] = ['id' => 3, 'name' => 'Simon'];

// define some vehicles (ppm could be defined single value)
$vehicles[] = ['id' => 1, 'name' => 'Truck A', 'ppm' => 0.001];
$vehicles[] = ['id' => 2, 'name' => 'Truck B', 'ppm' => 0.003];
$vehicles[] = ['id' => 3, 'name' => 'Truck C', 'ppm' => 0.006];

// define route
$route = new Route([
    'id' => 1,
    'name' => 'San José',
    'distance' => 220
]);

// create & attach RouteSection with vehicle and driver
foreach ($drivers as $key => $driver) {
    $route->attach(new RouteSection(
        $vehicles[$key], // vehicle
        $driver          // driver
    ));
}

// run route
$route->run();

//
echo 'Total Distance: '.$route->getTotalDistance().PHP_EOL;

//
echo 'Total Cost: '.$route->getTotalCost().PHP_EOL;

echo PHP_EOL;

// get report
print_r($route->getReport());

https://3v4l.org/siekY

<强>结果:

Total Distance: 660
Total Cost: 2.20

Array
(
    [1] => Array
        (
            [route] => San José
            [vehicle] => Array
                (
                    [id] => 1
                    [name] => Truck A
                    [ppm] => 0.001
                )

            [driver] => Array
                (
                    [id] => 1
                    [name] => Lawrence
                )

            [distance] => 220
            [cost] => 0.22
        )

    [2] => Array
        (
            [route] => San José
            [vehicle] => Array
                (
                    [id] => 2
                    [name] => Truck B
                    [ppm] => 0.003
                )

            [driver] => Array
                (
                    [id] => 2
                    [name] => Paul
                )

            [distance] => 220
            [cost] => 0.66
        )

    [3] => Array
        (
            [route] => San José
            [vehicle] => Array
                (
                    [id] => 3
                    [name] => Truck C
                    [ppm] => 0.006
                )

            [driver] => Array
                (
                    [id] => 3
                    [name] => Simon
                )

            [distance] => 220
            [cost] => 1.32
        )

)