Jump to content

Recommended Posts

I have a Soup object in a Bowl object in a Microwave object.

 

The Microwave object has the methods:

 

receiveItem(Bowl $b)

cookItem(???)

 

I want the Microwave to be able to set the Soup->temperature but I'm not how to do that?

 

Make sense?

 

 

 

TomTees

 

We are going to need more details I think. Can we see some code?

 

Putting it simply though, the Soup object would need a method that sets its temperature.

 

I figured it out.

 

But do you wanna see my silly soup app?!  :shy:

 

I created it to *try* and get a better understanding of how I think OOP works.

 

(I can ZIP it up and post it if anyone wants to critique it?!  Although no one wanted to open my ZIP earlier?!)

 

 

 

TomTees

 

 

Iv'e only had a really quick look (I'm at work), but you seem to be on the right track.

 

One thing I would do however is make a base 'Food' class. Bowls can hold more than just soup so there is no point locking a bowl into only being able to hold soup.

 

From there you would make your Soup class extend food. Because a 'type' might not belong to all foods, you should keep that within Soup, just put the generic stuff in 'Food'. You might have a bowl of 'porridge' for instance (I know, you can get different flavours/types of porridge these day, I'm just trying to point out that you put the more generic stuff in the food class and extend it with the more specific stuff).

 

So your code might look more like....

class Food {
    
    private $type;
    private $temperature;

    public function __construct($t=null){
        $this->type = $t;
    }

    public function setTemperature($temp=null){
        $this->temperature = $temp;
    }

    public function getTemperature(){
        return $this->temperature;
    }
}
        
class Soup extends Food {

    public function getType() {
        return $this->type;
    }
}

 

While the FillBowl() method now excepts Food.

 

public function fillBowl(Food $s){
    $this->contents = $s;
}

 

You should be able to do much the same with the Bowl object given that, you can also put plates, lunch boxes, cups etc etc in a microwave.

 

Doing this (as I'm sure you understand) suddenly makes you classes allot more flexible.

Here's my take on the subject which I believe has strong relations with the Observer pattern.

 

interface Cookable {
    function changeTemperature(MicroWave $mw);
}

class BowlOfSoup implements Cookable {
    private $temperature = 0.0; // darn cold soup 
    
    function changeTemperature(MicroWave $mw) {
        $this->temperature = $mw->getTemperature() * 1.267;
    }
}

class MicroWave {
    private $item = null;
    private $config = null;
    private $temperature = 0.0;
    
    function __construct() {
        $this->config = new MicroWaveConfig($this);
    }
    
    function insert(Cookable $item) {
        $this->item = $item;
        return $this->config;
    }

    function start() {
        for($i = 0; $i < $this->config->getCookTime(); ++$i) {
            $this->temperature = $i * 1.027; // on each second the temperature increases 2.7%
            $this->notifyTemperatureChanged();
        }
    }
    
    function getCookedItem() {
        return $this->item;
    }
    
    function getTemperature() {
        return $this->temperature;
    }
    
    private function notifyTemperatureChanged() {
        $this->item->changeTemperature($this);
    }
}

class MicroWaveConfig {
    private $microWave = null;
    private $cookTime = 0;
    private $waveIntensity = 0.1;
    
    function __construct(MicroWave $mw) {
        $this->microWave = $mw;
    }
    
    function getCookTime() {
        return $this->microWave;
    }
    
    function setCookTime($cookTime) {
        $this->cookTime = $cookTime;
    }
    
    function getWaveIntensity() {
        return $this->waveIntensity;
    }
    
    function setWaveIntensity($intense) {
        $this->waveIntensity = $intense;
    }
    
    function start() {
        $this->microWave->start();
    }
}

 

$microWave = new MicroWave(); // spawn to World *MAGIC*
$microWave->insert(new BowlOfSoup('Exquisite'))
          ->setCookTime(180)->setWaveIntensity(0.6)->start();
$soup = $microWave->getCookedItem();

Thorpe,

 

Great feedback!!

 

I totally follow what you said, and agree, however realize this was just a simple, silly example I put together to get a better handle of how to work with Classes, Objects, and OOP.  (Technically, this was my first-ever attempt at a mini OOP app.  And short of a few tips from PHP Freaks, I basically did this entirely by myself yesterday, so I am feeling better about me surviving this whole OOP thing!)

 

If I was going to expand this app, I would totally do what you recommended.  And since this is a "cute" and easy to relate to example, maybe I will expand it in my free time, as it would make a great teaching device!!

 

Thanks,

 

 

TomTees

 

 

Oh, one more quick thing. I think your naming your methods too specifically.

 

eg; There is no point having a methods called getSoupTemperature() within a soup object, getTemperature() should suffice.

 

I agree 110%.  (I just did that to help me keep things straight as I was starting to get really mixed up with all of those objects late last night!!)

 

Thanks,

 

 

TomTees

 

 

Hang with me, Ignace, I'm going to get this eventually!!!  :shy:

 

See my comments/questions in RED below...

Here's my take on the subject which I believe has strong relations with the Observer pattern.

 

interface Cookable {

    function changeTemperature(MicroWave $mw);

}

 

class BowlOfSoup implements Cookable {

    private $temperature = 0.0; // darn cold soup ;)

   

    function changeTemperature(MicroWave $mw) {

        $this->temperature = $mw->getTemperature() * 1.267;

    }

}

 

class MicroWave {

    private $item = null;

    private $config = null;

    private $temperature = 0.0;

   

    function __construct() {

        $this->config = new MicroWaveConfig($this);

    }

 

Not sure I follow this?!  It looks recursive?!  You have two $this's...

   

    function insert(Cookable $item) {

        $this->item = $item;

        return $this->config;

    }

 

    function start() {

        for($i = 0; $i < $this->config->getCookTime(); ++$i) {

            $this->temperature = $i * 1.027; // on each second the temperature increases 2.7%

            $this->notifyTemperatureChanged();

        }

    }

   

    function getCookedItem() {

        return $this->item;

    }

   

    function getTemperature() {

        return $this->temperature;

    }

   

    private function notifyTemperatureChanged() {

        $this->item->changeTemperature($this);

    }

}

 

class MicroWaveConfig {

    private $microWave = null;

    private $cookTime = 0;

    private $waveIntensity = 0.1;

   

    function __construct(MicroWave $mw) {

        $this->microWave = $mw;

    }

   

    function getCookTime() {

        return $this->microWave;

    }

   

    function setCookTime($cookTime) {

        $this->cookTime = $cookTime;

    }

   

    function getWaveIntensity() {

        return $this->waveIntensity;

    }

   

    function setWaveIntensity($intense) {

        $this->waveIntensity = $intense;

    }

   

    function start() {

        $this->microWave->start();

    }

}

 

$microWave = new MicroWave(); // spawn to World *MAGIC*

$microWave->insert(new BowlOfSoup('Exquisite'))

          ->setCookTime(180)->setWaveIntensity(0.6)->start();

 

I think "BowlOfSoup" needs a "name" property if you are passing it 'Exquisite' in the construction phase, right?

 

I think you are missing a semi-colon after ('Exquisite'))

 

I am totally lost how you are "daisy-chaining together these methods above??  :confused:

          ->setCookTime(180)->setWaveIntensity(0.6)->start();

 

 

$soup = $microWave->getCookedItem();

 

 

Not to be too anal-retentive, but technically this should be called something like $myBowl and not $soup since you are really getting a heated up "Bowl of Soup", right?

[/code]

 

Thanks for your help!!!

 

 

 

TomTees

 

 

I think ignace might have simply got a little rushed at the end of his example. The design itself is allot better than mine and mimics the real world allot closer. I always have trouble finding the pattern top fit the problem, but I was also trying to keep it pretty simple.

 

I think "BowlOfSoup" needs a "name" property if you are passing it 'Exquisite' in the construction phase, right?

Your right, this arument being passed to the __construct() isn't being used in this example.

I think you are missing a semi-colon after ('Exquisite'))

Not really, he is chaining the methods together. See my next response though....

I am totally lost how you are "daisy-chaining together these methods above??  :confused:

          ->setCookTime(180)->setWaveIntensity(0.6)->start();

 

$soup = $microWave->getCookedItem();

You can chain methods together like that. However,  each of the methods need to actually return $this. In this case they don't so it wouldn't work, but the idea is good.

I think ignace might have simply got a little rushed at the end of his example. The design itself is allot better than mine and mimics the real world allot closer. I always have trouble finding the pattern top fit the problem, but I was also trying to keep it pretty simple.

 

Yeah, the more read his and other people's examples, the more things click a little bit.

 

 

 

I think "BowlOfSoup" needs a "name" property if you are passing it 'Exquisite' in the construction phase, right?

 

Your right, this arument being passed to the __construct() isn't being used in this example.

 

 

Okay, good.

 

 

 

I think you are missing a semi-colon after ('Exquisite'))

 

Not really, he is chaining the methods together. See my next response though....

 

I am totally lost how you are "daisy-chaining together these methods above??  :confused:

          ->setCookTime(180)->setWaveIntensity(0.6)->start();

 

$soup = $microWave->getCookedItem();

 

You can chain methods together like that. However,  each of the methods need to actually return $this. In this case they don't so it wouldn't work, but the idea is good.

 

 

So he was trying to say this?

 

 

$microWave->insert(new BowlOfSoup('Exquisite'))

$microWave->setCookTime(180)

$microWave->setWaveIntensity(0.6)

$microWave->start();

 

 

 

TomTees

 

 

So he was trying to say this?

 

Quote

 

    $microWave->insert(new BowlOfSoup('Exquisite'))

    $microWave->setCookTime(180)

    $microWave->setWaveIntensity(0.6)

    $microWave->start();

 

TomTees

If we fix ignaces example a little.

 

class MicroWaveConfig {
    private $microWave = null;
    private $cookTime = 0;
    private $waveIntensity = 0.1;
    
    function __construct(MicroWave $mw) {
        $this->microWave = $mw;
    }
    
    function getCookTime() {
        return $this->microWave;
    }
    
    function setCookTime($cookTime) {
        $this->cookTime = $cookTime;
        return $this;
    }
    
    function getWaveIntensity() {
        return $this->waveIntensity;
    }
    
    function setWaveIntensity($intense) {
        $this->waveIntensity = $intense;
        return $this;
    }
    
    function start() {
        $this->microWave->start();
    }
}

 

You can now chain these methods together....

 

$microWave = new MicroWave(); // spawn to World *MAGIC*
$microWave->insert(new BowlOfSoup())
          ->setCookTime(180)->setWaveIntensity(0.6)->start();

 

because each of the methods returns the MicroWaveConfig object.

I should mention new lines don't matter, so...

 

$microWave = new MicroWave(); // spawn to World *MAGIC*
$microWave->insert(new BowlOfSoup())
          ->setCookTime(180)->setWaveIntensity(0.6)->start();

 

could be....

 

$microWave = new MicroWave(); // spawn to World *MAGIC*
$microWave
  ->insert(new BowlOfSoup())
  ->setCookTime(180)
  ->setWaveIntensity(0.6)
  ->start();

 

or...

 

$microWave = new MicroWave(); // spawn to World *MAGIC*
$microWave->insert(new BowlOfSoup())->setCookTime(180)->setWaveIntensity(0.6)->start();

 

You can also so still call them the more traditional way....

 

$microWave->insert(new BowlOfSoup('Exquisite'))
$microWave->setCookTime(180)
$microWave->setWaveIntensity(0.6)
$microWave->start();

 

but as you can see, chaining is a simpler (shorter) syntax.

 

This stuff is just candy though, but allot of frameworks use it.

 

 

class MicroWaveConfig {    private $microWave = null;    private $cookTime = 0;    private $waveIntensity = 0.1;        function __construct(MicroWave $mw) {        $this->microWave = $mw;    }        function getCookTime() {        return $this->microWave;    }        function setCookTime($cookTime) {        $this->cookTime = $cookTime;        return $this;    }        function getWaveIntensity() {        return $this->waveIntensity;    }        function setWaveIntensity($intense) {        $this->waveIntensity = $intense;        return $this;    }        function start() {        $this->microWave->start();    }}

 

 

You can now chain these methods together because each of the methods returns the MicroWaveConfig object.

 

 

So return $this; returns the object MicroWaveConfig ??

 

 

 

TomTees

 

P.S.  Thorpe, so it looks like you're pretty handy with OOP yourself?!

 

 

P.S.  Thorpe, so it looks like you're pretty handy with OOP yourself?!

 

Not in particularly, theres guys around here that are allot better practiced than I. Ignace would be one of them.

 

I do use it though. I've written my own framework, and have worked with a few of the already existing ones. It is something that takes practice though, recognizing what design pattern will solve the problem at hand.

When I posted my example I was actually seeking for someone to concur or argument against my design - as there are quite some intelligent people around here (thorpe, CV, Daniel, Mchl, PFMaBisMad, ..) - not out to impress anyone. OO design is difficult, even more so if you have a low domain knowledge (like I have on micro-waves :P).

 

For example I doubt you would express the created energy in a micro-wave as temperature (the object inside heats up due to the generated energy) or that the wave intensity would be able to correlate to a simple float but being an example I thought "/care" :D

OO design is difficult,

 

That's why I'm asking for your (and anyone else's) help!!!!!

 

 

even more so if you have a low domain knowledge (like I have on micro-waves :P).

 

Ha ha.

 

 

 

For example I doubt you would express the created energy in a micro-wave as temperature (the object inside heats up due to the generated energy) or that the wave intensity would be able to correlate to a simple float but being an example I thought "/care" :D

 

I just chose this example because everyone can understand Soup, Bowl, Microwave.  And it was something easy for me to put my head around as far as Object relationships, properties, and methods.

 

Now on to a "real" example in the next few days!

 

Thanks for all of your help, and I'll be back to haunt you again soon!

 

 

 

TomTees

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.