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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:33:55+00:00 2026-06-17T10:33:55+00:00

I tried to implement a simple Decorator Pattern in Java. The main idea is

  • 0

I tried to implement a simple Decorator Pattern in Java. The main idea is that a concrete decorator have to add something to the basic list. However, my implementation doesnt work correctly and I dont know why.

The output is as you see below:

ING -1,ING 0,ING 1.

but it should be:

ING -1,ING 0,ING 1, ING 2.

Heres my code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage;

import java.util.ArrayList;
import java.util.List;

abstract class Tester {

    protected List<String> ingridients = new ArrayList();
    protected String description;

    public String getDescription() {
        description = "";
        for (String i : ingridients) {
            description += i;
            description += ",";
        }
        description = description.substring(0, description.length() - 1);
        description += ".";
        return description;
    }
}

abstract class Decorator extends Tester {

    @Override
    public abstract String getDescription();
}

class Test1 extends Tester {

    public Test1() {
        this.ingridients.add("ING -1");
        this.ingridients.add("ING 0");
    }
}

class Ing1 extends Decorator {

    private Tester t;

    public Ing1(Tester t) {
        this.t = t;
    }

    @Override
    public String getDescription() {
        this.t.ingridients.add("ING 1");
        return this.t.getDescription();
    }
}

class Ing2 extends Decorator {

    private Tester t;

    public Ing2(Tester t) {
        this.t = t;
    }

    @Override
    public String getDescription() {
        this.t.ingridients.add("ING 2");
        return this.t.getDescription();
    }
}

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Tester t = new Test1();
        t = new Ing1(t);
        t = new Ing2(t);

        System.out.println(t.getDescription());
    }
}

Edited code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage;

import java.util.ArrayList;
import java.util.List;

interface Tester {

    List<String> ingridients = new ArrayList();
    public String getDescription();
}

abstract class Decorator implements Tester {

    @Override
    public abstract String getDescription();
}


class Test1 implements Tester {

    public Test1() {
        ingridients.add("ING -1");
        ingridients.add("ING 0");
    }

    @Override
    public String getDescription() {
        String description = "";
        for (String i : ingridients) {
            description += i;
            description += ",";
        }
        description = description.substring(0, description.length() - 1);
        description += ".";
        return description;
    }
}

class Ing1 extends Decorator {

    private Tester t;

    public Ing1(Tester t) {
        this.t = t;
    }

    @Override
    public String getDescription() {
        this.t.ingridients.add("ING 1");
        return this.t.getDescription();
    }
}

class Ing2 extends Decorator {

    private Tester t;

    public Ing2(Tester t) {
        this.t = t;
    }

    @Override
    public String getDescription() {
        this.t.ingridients.add("ING 2");
        return this.t.getDescription();
    }
}

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Tester t = new Test1();
        t = new Ing1(t);
        t = new Ing2(t);

        System.out.println(t.getDescription());
    }
}
  • 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-17T10:33:56+00:00Added an answer on June 17, 2026 at 10:33 am

    I run this in a debugger and I could see that your Decorators are not just decorators as they have state of their own. Make your Tester an interface and have you decorators only wrap the concrete instances and not have state of their own.

    // adds to the list in t1
    this.t.ingridients.add("ING 2");
    
    // add to the list in t
    this.t.ingridients.add("ING 1");
    
    // returns the contents of t.
    return this.t.getDescription();
    

    At the end t.ingredients has three items, t1.ingredients has 1 element and t2.ingredients has none.


    You could write it this way

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    
    public class Test {
        public static void main(String[] ignored) {
            Tester t012 = new Ing2(new Ing1(new Ing0(new None())));
            System.out.println(t012.getDescription());
    
            Tester t210 = new Ing0(new Ing1(new Ing2(new None())));
            System.out.println(t210.getDescription());
        }
    }
    
    abstract class Tester {
        public List<String> getIngredients() {
            return Collections.emptyList();
        }
    
        public String getDescription() {
            StringBuilder sb = new StringBuilder();
            String sep = "";
            for (String s : getIngredients()) {
                sb.append(sep).append(s);
                sep=", ";
            }
            sb.append(".");
            return sb.toString();
        }
    }
    
    class None extends Tester {
    }
    
    class Ing0 extends Tester {
        private final Tester wrapped;
        Ing0(Tester wrapped) {
            this.wrapped = wrapped;
        }
    
        @Override
        public List<String> getIngredients() {
            List<String> list = new ArrayList<>(wrapped.getIngredients());
            list.add("ING -1");
            list.add("ING 0");
            return Collections.unmodifiableList(list);
        }
    }
    
    class Ing1 extends Tester {
        private final Tester wrapped;
        Ing1(Tester wrapped) {
            this.wrapped = wrapped;
        }
    
        @Override
        public List<String> getIngredients() {
            List<String> list = new ArrayList<>(wrapped.getIngredients());
            list.add("ING 1");
            return Collections.unmodifiableList(list);
        }
    }
    
    class Ing2 extends Tester {
        private final Tester wrapped;
        Ing2(Tester wrapped) {
            this.wrapped = wrapped;
        }
    
        @Override
        public List<String> getIngredients() {
            List<String> list = new ArrayList<>(wrapped.getIngredients());
            list.add("ING 2");
            return Collections.unmodifiableList(list);
        }
    }
    

    prints

    ING -1, ING 0, ING 1, ING 2.
    ING 2, ING 1, ING -1, ING 0.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to implement a simple treemap to count the occurances of integers,
I am trying to implement simple program in Java that will be used to
I tried to implement a simple yes/no input with java.util.Scanner. My code looks like
I once tried to implement Comet in PHP. Soon, I found that PHP is
I have tried to implement css sticky footer on my page but it doesn't
I've an WPF application where tried to implement MVVM pattern and Prism 2. I
I tried to implement rtmps with red5 but was having an error that the
I have tried to implement a template template template - template class to fullfill
I'd like to implement a simple AJAX function locally that allows me to autocomplete
Well, i am at new at Socket programming in java. I tried to implement

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.