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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:31:12+00:00 2026-05-14T03:31:12+00:00

I’m trying to apply the Decorator Design Pattern to the following situation: I’ve 3

  • 0

I’m trying to apply the Decorator Design Pattern to the following situation:

I’ve 3 different kind of forms: Green, Yellow, Red.

Now, each of those forms can have different set of attributes. They can have a minimize box disabled, a maximized box disabled and they can be always on top.

I tried to model this the following way:

              Form <---------------------------------------FormDecorator
              /\                                                  /\
     |---------|-----------|               |----------------------|-----------------|
GreenForm  YellowForm   RedForm  MinimizeButtonDisabled MaximizedButtonDisabled AlwaysOnTop

Here is my GreenForm code:

public class GreenForm : Form {
    public GreenForm() {
        this.BackColor = Color.GreenYellow;
    }

    public override sealed Color BackColor {
        get { return base.BackColor; }
        set { base.BackColor = value; }
    }
}

FormDecorator:

public abstract class FormDecorator : Form {
    private Form _decoratorForm;

    protected FormDecorator(Form decoratorForm) {
        this._decoratorForm = decoratorForm;
    }
}

and finally NoMaximizeDecorator:

public class NoMaximizeDecorator : FormDecorator
{
    public NoMaximizeDecorator(Form decoratorForm) : base(decoratorForm) {
        this.MaximizeBox = false;
    }
}

So here is the running code:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(CreateForm());
}

static Form CreateForm() {
    Form form = new GreenForm();
    form = new NoMaximizeDecorator(form);
    form = new NoMinimizeDecorator(form);

    return form;
}

The problem is that I get a form that isn’t green and that still allows me to maximize it. It is only taking in consideration the NoMinimizeDecorator form. I do comprehend why this happens but I’m having trouble understanding how to make this work with this Pattern.

I know probably there are better ways of achieving what I want. I made this example as an attempt to apply the Decorator Pattern to something. Maybe this wasn’t the best pattern I could have used(if one, at all) to this kind of scenario. Is there any other pattern more suitable than the Decorator to accomplish this? Am I doing something wrong when trying to implement the Decorator Pattern?

  • 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-14T03:31:13+00:00Added an answer on May 14, 2026 at 3:31 am

    The problem here is that you’re not actually implementing the decorator pattern. For a proper implementation of the pattern, you need to subclass Form to create your decorator, and then intercept all operations taken on your decorator and forward them to your private Form instance. You sort of do that, except that aside from assigning a reference in the FormDecorator constructor, you never again use that private Form instance. The net result is that you create a GreenForm, then wrap it in a NoMaximizeDecorator, and then you wrap that in a NoMinimizeDecorator. But because you never forward operations taken against the NoMinimizeDecorator to the wrapped Form instance, only the NoMinimizeDecorator instance actually applies any behavior to the instance that’s used. This fits with what you observe when you run your code: a standard window with a disabled Minimize button.

    Form is a really bad example for creating decorators in C#, because most of its properties and methods are non-virtual, meaning if you’re accessing the decorated form via a Form reference, you have no way to intercept the base class’s properties – you can’t effectively “wrap” Form.

    EDIT

    It occurs to me that the statement “Form is a really bad example for creating decorators in C#” really begs the question of what is a good example. Typically, you’ll use the decorator pattern to provide a custom interface implementation without implementing the entire implementation from scratch. A very common example is generic collections. Most everything that wants list functionality doesn’t depend on, e.g., List<String>, but rather on IList<String>. So, if you for example want a custom collection that won’t accept strings shorter than 5 characters, you would use something like the following:

    public class MinLengthList : IList<String>
    {
        private IList<string> _list;
        private int _minLength;
    
        public MinLengthList(int min_length, IList<String> inner_list)
        {
            _list = inner_list;
            _minLength = min_length;
        }
    
        protected virtual void ValidateLength(String item)
        {
            if (item.Length < _minLength)
                throw new ArgumentException("Item is too short");
        }
    
        #region IList<string> Members
    
        public int IndexOf(string item)
        {
            return _list.IndexOf(item);
        }
    
        public void Insert(int index, string item)
        {
            ValidateLength(item);
            _list.Insert(index, item);
        }
    
        public void RemoveAt(int index)
        {
            _list.RemoveAt(index);
        }
    
        public string this[int index]
        {
            get
            {
                return _list[index];
            }
            set
            {
                ValidateLength(value);
                _list[index] = value;
            }
        }
    
        #endregion
    
        #region ICollection<string> Members
    
        public void Add(string item)
        {
            ValidateLength(item);
            _list.Add(item);
        }
    
        public void Clear()
        {
            _list.Clear();
        }
    
        public bool Contains(string item)
        {
            return _list.Contains(item);
        }
    
        public void CopyTo(string[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }
    
        public int Count
        {
            get { return _list.Count; }
        }
    
        public bool IsReadOnly
        {
            get { return _list.IsReadOnly; }
        }
    
        public bool Remove(string item)
        {
            return _list.Remove(item);
        }
    
        #endregion
    
        #region IEnumerable<string> Members
    
        public IEnumerator<string> GetEnumerator()
        {
            return _list.GetEnumerator();
        }
    
        #endregion
    
        #region IEnumerable Members
    
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)_list).GetEnumerator();
        }
    
        #endregion
    }
    
    public class Program
    {
    
        static void Main()
        {
            IList<String> custom_list = new MinLengthList(5, new List<String>());
            custom_list.Add("hi");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.