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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:10:41+00:00 2026-06-10T06:10:41+00:00

I am using a decorator pattern to add style and options to an output

  • 0

I am using a decorator pattern to add style and options to an output stream (console).

It’s an console app that adds color to the command line output. The
decorator adds the color. The condition in the ClientClass is that
if an error occurred then decorate with red text and if no error occurred then decorate with green text. The color
decorator therefore has optional calls that only the ClientClass
can know about.

* Copy of the OP’s comment on one of the answers

The problem I have is this: I am passing in my decorator to a class via the constructor. But the decorated object requries that I call two methods that are not in the base class i.e. that being decorated.

Is this bad? Shouldnt i only be calling those methods which are available in all decorated classes?

As an example:

<?php

abstract class MyAceBaseClass
{ 
    abstract function doYourThing();
}


class MyAceBaseClassDecoratorA extends MyAceBaseClass
{ 
    protected $aceBaseClass;

    protected $amount = 0;

    public function __construct($aceBaseClass)
    { 
         $this->_aceBaseClass = $aceBaseClass;
    }

    public function doYourThing()
    {
        $result = $aceBaseClass + $this->amount;
        return $result;
    }

    public function decoratorFunctionA()
    {
        $this->amount = 10;
    }

    public function decoratorFunctionB()
    {
        //----
    }
}

class ClientClass
{ 
    private $aceObject;

    public function __construct(MyAceBaseClass $aceObject) 
    {
         $this->aceObject = $aceObject;       
    }

    public function run()
    {
         if ($someCondition) {
             $this->aceObject->decoratorFunctionA();
             $this->aceObject->decoratorFunctionB();
         }

         $result = $this->aceObject->doYourThing();

         echo $result;
    }
}

So as you can see, the client class needs to call decoratorFunctionA and decoratorFunctionB (which are only availble in the decorator class) before it can call the abstract method doYourThing() available to all classes extending MyAceBaseClass. This feels wrong.

I think the decorator implementation is correct but now i am stuck with this other problem

How can i resolve this?

The client class might not need both decoratorFunctionA() and decoratorFunctionB() called, it might only need 1 or none so I cant simply call these automatically in the doYourThing call.

A possibility is to have an interface for MyAceBaseClassDecoratorA but then i am kind of defeating the purpose of using the decorator in the first place.

  • 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-10T06:10:43+00:00Added an answer on June 10, 2026 at 6:10 am

    You can’t call methods not specified by the interface you’re expecting: MyAceBaseClass (as @ctrahey pointed out).

    Also, if I read it correctly, you seem to be mixing the things up. Who’s decorating who in this case?

    If the purpose of the Decorator Pattern is to add functionality to an existing object without subclassing (in other words, without saying that an object “is a”), the decorator must have a protected property with the decorated object. It seems like that it’s your ClientClass that’s decorating what you called MyAceBaseClassDecoratorA.

    IMO, it should be something along this lines:

    // Concrete object
    class MyObject implements MyObjectInterface {
       public function doSomething() {
           return 1;
       }
    }
    
    // Your abstract decorator
    abstract class MyObjectDecorator implements MyObjectInterface {
        protected $myObject;
    
        public function __construct(MyObjectInterface $object) {
            $this->myObject = $object;
        }
    
        public function doSomething() {
            return $this->myObject->doSomething();
        }
    }
    
    // Your concrete decorator
    class MyConcreteObjectDecorator extends MyObjectDecorator {
        public function doSomething() {
            $value = $this->myObject->doSomething();
            return $value + 10;
        }
    }
    

    EDITED:

    As for your explanation in the comments, here’s what you should do:

    class ClientClass {
        // ...
        public function run() {
            $this->aceObject->doYourThing(); // and that's all
        }
        // ...
    }
    

    So, you’ll not call $this->aceObject->decoratorFunctionA(); or $this->aceObject->decoratorFunctionB(); outside of the decorator as you have done in the ClientClass.

    Instead, you should provide the decorated object with the functionality injected into the doYourThing() method before calling the $ClientClass->aceObject->doYourThing() method.

    If there’s an error, then you’ll decorate it with the RedTextDecorator, if everything went well, than you’ll, instead, decorate it with the GreenTextDecorator.

    public function run()
    {
         if ($error) {
             $this->aceObject = new RedTextDecorator($this->aceObject);
         } elseif($success) {
             $this->aceObject = new GreenTextDecorator($this->aceObject);
         }
    
         $result = $this->aceObject->doYourThing();
    
         echo $result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I need to write an example code using the decorator dessign pattern and
I recently solved one of my problems using the decorator pattern. Everything works fine
No doubt that decorator pattern is good and easy standard to enhance some method
I'm creating an app in which many urls are using a pattern like /foo/bar/<object_id>/<object_name_slug>/
I am using the decorator pattern and am decorating a class with a constructor
As I am working on a piece of code that needs the decorator pattern,
Using simple injector with the command pattern described here . Most commands have companion
Using simple injector with the command pattern described here and the query pattern described
I have added checkbox in display table using decorator class as shown in below
I have been using this excellent decorator for memoization, which I found on the

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.