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

  • Home
  • SEARCH
  • 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 3283022
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:59:37+00:00 2026-05-17T19:59:37+00:00

Lifting this example from Wikipedia : // the Window interface interface Window { public

  • 0

Lifting this example from Wikipedia:

// the Window interface
interface Window {
    public void draw(); // draws the Window
    public String getDescription(); // returns a description of the Window
}

// implementation of a simple Window without any scrollbars
class SimpleWindow implements Window {
    public void draw() {
        // draw window
    }

    public String getDescription() {
        return "simple window";
    }
}

// abstract decorator class - note that it implements Window
abstract class WindowDecorator implements Window {
    protected Window decoratedWindow; // the Window being decorated

    public WindowDecorator (Window decoratedWindow) {
        this.decoratedWindow = decoratedWindow;
    }
   public void draw() {
        decoratedWindow.draw();
    }
}

How would I, for example, allow a user to implement a decorator that decorates draw but not getDescription? In my actual code, there are 5 possible methods they could decorate.

The way I see it, I have 3 options:

  • Put all 5 methods on the interface. The downside is this would force them to implement to 4 methods that would simply be calling the parent.
  • Create 5 interfaces, one for each method. A bit clumsy.
  • Don’t use an interface at all. Lose the design contract.

Are there any other options? If not, which of the above would be the best choice?

I’m using PHP.

  • 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-05-17T19:59:38+00:00Added an answer on May 17, 2026 at 7:59 pm

    You can make use of PHP’s constructor inheritance and create an abstract, default decorator implementation AbstractWindowDecorator, which just gets the decorated Window and forward all calls to it. And now, when implementing new decorators, just inherit from AbstractWindowDecorator and override only these methods you need.

    // the Window interface
    interface Window
    {
        public function draw(); // draws the Window
        public function getDescription(); // returns a description of the Window
    }
    
    // implementation of a simple Window without any scrollbars
    class SimpleWindow implements Window
    {
        public function draw()
        {
            // draw window
        }
    
        public function getDescription()
        {
            return "simple window";
        }
    }
    
    // abstract decorator class - note that it implements Window
    abstract class AbstractWindowDecorator implements Window
    {
        protected $decoratedWindow; // the Window being decorated
    
        public final function __construct ($decoratedWindow)
        {
            $this->decoratedWindow = $decoratedWindow;
        }
    
        public function draw()
        {
            return $this->decoratedWindow->draw();
        }
    
        public function getDescription()
        {
            return $this->decoratedWindow->getDescription();
        }
    }
    
    // all decorators can now
    class SampleWindowDecorator extends AbstractWindowDecorator
    {
       public function draw()
       {
           // do something other;
       }
    }
    

    AbstractWindowDecorator still implements Window, so you have your contract right and you don’t need to implement all methods. And moreover, all decorators created “old way”, without abstract class, will fit into this pattern, too.

    Decorator’s instantiation is just like it was in your example:

    $w = new SimpleWindow();
    $d = new SampleWindowDecorator($w);
    echo $d->getDescription(); // "simple window"
    

    (In fact, you can do it even without constructor inheritance, but you’ll need to explicitly create an constructor for each decorator.)

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

Sidebar

Related Questions

This is a stupid example but I'm trying to understand how thing get passed
hey guys, i manage to get this script working to pull data from my
I have an ASP.NET web service which does some heavy lifting, like say,some file
I need a short code snippet to get a directory listing from an HTTP
I have a master-detail scenario where I have 1 ComboBox listing companies from an
I come from the Open source world where I'm used to having Apache serve
I'm trying to extract the subdomain from the HTTP_HOST value. However I've stumbled into
I am trying to implement a function on jquery datatable, that returns the 1st
I've been reading about the this pointer on various sites (e.g. the MSDN manuals)
In another question - Getting directory listing from SVN for use in ANT dropdown

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.