Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8019903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:31:56+00:00 2026-06-04T21:31:56+00:00

The book says: The decorator pattern can be used to extend (decorate) the functionality

  • 0

The book says:

The decorator pattern can be used to extend (decorate) the
functionality of a certain object

I have a rabbit animal. And I want my rabbit to have, for example, reptile skin. Just want to decorate a common rabbit with reptile skin.

I have the code. First I have abstract class Animal with everythig that is common to any animal:

abstract class Animal {
    abstract public function setSleep($hours);
    abstract public function setEat($food);
    abstract public function getSkinType();

    /* and more methods which for sure will be implemented in any concrete animal */
} 

I create class for my rabbit:

class Rabbit extends Animal {
    private $rest;
    private $stomach;
    private $skinType = "hair";

    public function setSleep($hours) {
        $this->rest    = $hours;
    }

    public function setFood($food) {
        $this->stomach = $food;
    }

    public function getSkinType() {
        return $this->$skinType;
    }

}

Up to now everything is OK. Then I create abstract AnimalDecorator class which extends Animal:

abstract class AnimalDecorator extends Animal {
    protected $animal;

    public function __construct(Animal $animal) {
        $this->animal = $animal;
    }
}

And here the problem comes. Pay attention that AnimalDecorator also gets all the abstract methods from the Animal class (in this example just two but in real can have many more).

Then I create concrete ReptileSkinDecorator class which extends AnimalDecorator. It also has those the same two abstract methods from Animal:

class ReptileSkinDecorator extends AnimalDecorator {
    public function getSkinColor() {
        $skin = $this->animal->getSkinType();
        $skin = "reptile";
        return $skin;
    }
}

And finaly I want to decorate my rabbit with reptile skin:

$reptileSkinRabbit = ReptileSkinDecorator(new Rabbit());

But I can’t do this because I have two abstract methods in ReptileSkinDecorator class. They are:

abstract public function setSleep($hours);
abstract public function setEat($food);

So, instead of just re-decorating only skin I also have to re-decorate setSleep() and setEat(); methods. But I don’t need to.

In all the book examples there is always ONLY ONE abstract method in Animal class. And of course it works then. But here I just made very simple real life example and tried to use the Decorator pattern and it doesn’t work without implementing those abstract methods in ReptileSkinDecorator class.

It means that if I want to use my example I have to create a brand new rabbit and implement for it its own setSleep() and setEat() methods. OK, let it be. But then this brand new rabbit has the instance of commont Rabbit I passed to ReptileSkinDecorator:

$reptileSkinRabbit = ReptileSkinDecorator(new Rabbit());

I have one common rabbit instance with its own methods in the reptileSkinRabbit instance which in its turn has its own reptileSkinRabbit methods. I have rabbit in rabbit. But I think I don’t have to have such possibility.

I don’t understand the Decarator pattern right way. Kindly ask you to point on any mistakes in my example, in my understanding of this pattern.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:31:58+00:00Added an answer on June 4, 2026 at 9:31 pm

    Sounds like you’re trying to force the use of a particular pattern that doesnt fit the problem. A decorator usually aggregates something additional (add braids to the hair), instead of completely changing it, like you’re trying to do with the skin (hair to scales).

    A Builder pattern, (whereby you specify how you want the object built) may fit the problem better. In your case, you want to build a rabbit built with a reptile skin. (where Im from, instead of reptile skin, it would have been funny to make a rabbit with horns and call it a jackalope 🙂

    I think the use of a Builder (or any pattern) here may actually be overkill. Do you absolutely have to use a pattern for this particular problem? How about just defining the code as follows and leave patterns out of it for now:

    class Animal {
        private $rest;
        private $stomach;
        private $skinType;
    
        public function setSleep($hours) {
            $this->rest    = $hours;
        }
    
        public function setFood($food) {
            $this->stomach = $food;
        }
    
        public function setSkinType($skin) {
            $this->skinType = $skin;
        }
    
        // define the other getters too
    
        public function getSkinType() {
            return $this->$skinType;
        }
    }
    
    class Rabbit extends Animal {
        public function __construct() {
            $this->rest = "rabbitRest";        // put whatever a rabbit rest is
            $this->stomach = "rabbitStomach";  // put whatever a rabbit stomach is
            $this->skinType = "hair";
        }
    }
    

    Disclaimer: Im a c++ guy and my php is really bad, Im sure this code has several problems, but I hope you get the idea.

    So a Rabbit would just extend an Animal and set its properties accordingly. Then if somebody wanted a RabbitReptile, they could either extend Rabbit (probably overkill) or just make a Rabbit and set the skin type accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a) Can Object.GetType also be used for late binding ( Book I’m reading says
Okay, so the book says: when creating a new object, you can pass a
the book says that reintegrated branch should not be used as it can't accept
I have read in a Java book that says: Because a String is immutable,
One Book(Object Oriented Programming with C++ by E.Balagurusamy) says that const size = 10;
The Subversion SVN book says : ...Another way of thinking about this pattern is
My os-book says that if you want to add a system call to the
An old Direct3D book says ...you can achieve an acceptable frame rate with hardware
The book says about a small Windows.Forms program The Windows Forms classes are in
In the book The C Programming Language it says: Many of the functions in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.