OOP (beneficial use)

for my question about how to use OOP in a profitable way I assume, as an example, CORSIA, to which its owner (Volume), having a certain ADDRESS (NY), can add ARTICLES (Bike, Car). Finally, BILL is printed with all the information.

My problem: how to handle the collection of the desired information (here: owner, city, number of items) from several objects? Because I think it’s stupid to do it manually, as shown below (see 4.), right? (even more as the amount of information increases in reality)

So, what is the “clean way” to create the account / gather information needed in this example?

<?php
$a = new basket('Tom','NY');
$a->add_item("Bike",1.99);
$a->add_item("Car",2.99);

$b = new bill( $a );
$b->do_print();

1.

class basket {

    private $owner = "";
    private $addr = "";
    private $articles = array();

    function basket( $name, $city ) {
        // Constructor
        $this->owner = $name;
        $this->addr = new addresse( $city );

    }

    function add_item( $name, $price ) {
        $this->articles[] = new article( $name, $price );
    }

    function item_count() {
        return count($this->articles);
    }

    function get_owner() {
        return $this->owner;
    }

    function get_addr() {
        return $this->addr;
    }

}

2.

class addresse {

    private $city;

    function addresse( $city ) {
        // Constructor
        $this->city = $city;
    }

    function get_city() {
        return $this->city;
    }

}

3.

class article {

    private $name = "";
    private $price = "";

    function article( $n, $p ) {
        // Constructor
        $this->name = $n;
        $this->price = $p;
    }   

}

4.

class bill {

    private $recipient = "";
    private $city = "";
    private $amount = "";

    function bill( $basket_object ) {

        $this->recipient = $basket_object->get_owner();
        $this->city = $basket_object->get_addr()->get_city();
        $this->amount = $basket_object->item_count();

    }

    function do_print () {
        echo "Bill for " . $this->recipient . " living in " . $this->city . " for a total of " . $this->amount . " Items.";
    }

}
+1
source share
3 answers

Tell Dont Ask, , BillRenderer. , . InformationExpert High Cohesion, .

class Bill
{
    public function renderAs(BillRenderer $billRenderer)
    {
        $billRenderer->setRecipient($this->owner);
        $billRenderer->setAddress($this->address);
        return $billRenderer->render();
    }
}

BillRenderer () , . PlainText HTML PDF:

class TxtBillRenderer implements BillRenderer
{
    public function render()
    {
        return sprintf('Invoice for %s, %s', $this->name, $this->address);
    }
}

echo $bill->renderAs(new TxtBillRenderer);

, renderAs. .

+2

, - , .

, :

$bill = new Bill($buyer, $address, $basket->getPositions());

BillPrinter, :

$billPrinter = new BillPrinter($bill, $printerDevice);
$billPrinter->print();
+1

, PHP5 public function __construct(). , , PHP4. :

  • Basket ( Cart?), .
  • , , , .
  • Articles ( Items?) , . , +, . . .

:

  • . , .
  • Bill .

- :

class Basket
{
    // -- other code 

    public function handleInvoice( Bill $invoice )
    {
        $invoice->chargeFor( $this->items );
        $invoice->chargeTo( $this->account );
        return $invoice->process();
    }
}

..

$cart = new Basket(..);
// some operation with it

$invoice = new Bill;
$cart->handleInvoice($invoice);

$printer = new PDFPrinter;
// OR new JpegPrinter; OR new FakePrinter OR anything else
$printer->print( $invoice );

Bill , -.

, :

+1
source

All Articles