www.webdeveloper.com
Results 1 to 3 of 3

Thread: [Pattern] (Prototype) There is an error in this exemple that i read on a book?

  1. #1
    Join Date
    Jan 2013
    Posts
    84

    Question [Pattern] (Prototype) There is an error in this exemple that i read on a book?

    Hi, i'm readin PHP Objects Pattern and Practice 2nd Edition.
    In the section reserved to the PROTOTYPE PATTERN, i see an UML diagram that do not correspond to the final code.

    It's very simple
    abse.jpg

    And this is the implementation
    PHP Code:
    class Sea {}
    class 
    EarthSea extends Sea {}
    class 
    MarsSea extends Sea {}

    class 
    Plains {}
    class 
    EarthPlains extends Plains {}
    class 
    MarsPlains extends Plains {}

    class 
    Forest {}
    class 
    EarthForest extends Forest {}
    class 
    MarsForest extends Forest {}

    class 
    TerrainFactory {
       private 
    $sea;
       private 
    $forest;
       private 
    $plains;
       function 
    __constructSea $seaPlains $plainsForest $forest ) {
          
    $this->sea $sea;
          
    $this->plains $plains;
          
    $this->forest $forest;
       }
       function 
    getSea( ) {
          return clone 
    $this->sea;
       }
       function 
    getPlains( ) {
          return clone 
    $this->plains;
       }
       function 
    getForest( ) {
          return clone 
    $this->forest;
       }
    }

    $factory = new TerrainFactory( new EarthSea(),
                                    new 
    EarthPlains(),
                                    new 
    EarthForest() );
    print_r$factory->getSea() );
    print_r$factory->getPlains() );
    print_r$factory->getForest() ); 
    As you can see, all methods getSea, getPlains, getForest are fully implemented inside the main class (TerrainFactory), and at runtime they are the only that are used. For what reason in UML diagram the same methods are override in each sublclass (Earth and Mars) ?
    Last edited by American horizo; 04-11-2014 at 01:45 PM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,392
    I would say that either there is, in fact, an editorial error there, or possibly just poor layout making it appear that the code sample and diagram go together when they actually don't, since based on the diagram, TerrainFactory should be abstract with two concrete factory classes as its children.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2013
    Posts
    84
    Yes, in fact it's abstract, or not (it's have italic name)?
    I wouldn't say that the implementation is wrong, the UML concept is.
    Because assuming that the UML is right, i should implement the same methods for each subclass, causing of course redundant code where it's not necessary.
    Assuming that the methods equally have the same beavhior indipendent from the subclass, i think it's right to refer to this behaviors encapsluing them into the main class. No override is needed
    Last edited by American horizo; 04-11-2014 at 02:38 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles