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 127935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:33:20+00:00 2026-05-11T05:33:20+00:00

I am just starting to learn design patterns and I have two questions related

  • 0

I am just starting to learn design patterns and I have two questions related to the Decorator…

I was wondering why the decorator pattern suggests that the decorator implement all public methods of the component of which it decorates?

Can’t the decorator class just be used to provide the additional behaviors, and then the concrete component (which is passed into it) just be used to call everything else?

And secondly, what if the concrete component you want to decorate doesn’t have a base class which the abstract decorator can also derive from?

Thanks in advance!

  • 1 1 Answer
  • 2 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. 2026-05-11T05:33:20+00:00Added an answer on May 11, 2026 at 5:33 am

    I think you have have misunderstood Decorator. You’re thinking of a simple case of extending a concrete class with additional functionality. In this case, yes in most OO languages the derived class can simply allow its superclass to handle any unimplemented methods.

    class Base {    function foo() {     return 'foo';   }    function bar() {     return 'bar';   }  }  // Not what we think of as a Decorator, // really just a subclass. class Decorator extends Base {    // foo() inherits behavior from parent Base class     function bar() {     return parent::bar() . '!'; // add something   }  } 

    A Decorator class does not extend the base class of its ‘decorated’ class. It is a different type, which has a member object of the decorated class. Thus it must implement the same interface, if only to call the respective method of the decorated object.

    class Decorator { // extends nothing   protected $base;    function __construct(Base $base) {     $this->base = $base;   }    function foo() {     return $base->foo();   }    function bar() {     return $base->foo() . '!'; // add something   }  } 

    It might be worthwhile to define an interface (if your language supports such a thing) for both the decorated class and the Decorator class. That way you can check at compile time that the Decorator implements the same interface.

    interface IBase {   function foo();   function bar(); }  class Base implements IBase {   . . . }  class Decorator implements IBase {   . . . } 

    Re: @Yossi Dahan’s comment: I see the ambiguity in the wikipedia article, but if you read carefully it does say that the component being decorated is a field in the decorator object, and that the component is passed as an argument to the decorator constructor. This is different from inheritance.

    Though the wikipedia article does say the decorator inherits from the component, you should think of this as implementing an interface, as I showed in the PHP example above. The decorator still has to proxy for the component object, which it wouldn’t if it had inherited. This allows the decorator to decorate an object of any class that implements that interface.

    Here are some excerpts from ‘Design Patterns: Elements of Reusable Object-Oriented Software’ by Gamma, Helm, Johnson, and Vlissides:

    Decorator

    Intent

    Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

    Motivation

    … A decorator conforms to the interface of the component it decorates so that its presence is transparent to the component’s clients.

    Participants

    • Decorator maintains a reference to a Component object and defines an interface that conforms to Component’s interface.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just starting to learn how to use RMI, and I have a
I'm just starting to learn AspectJ, and I have use-case for say, User login.
I'm just starting to learn haskell. I have custom class class MyClass a where
i'm just starting to learn about database design. For my first project I made
I am just starting to learn Python, but I have already run into some
Just starting to learn scala for a new project. Have got to the point
I am just starting to learn jQuery and have gotten some of the basic
I'm just starting to learn Python and have heard about the GIL and how
I am just starting to learn Ruby on Rails and have a newbie question
Just starting to learn Ruby on Rails. I'm using RoR 3. I have read

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.