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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:26:01+00:00 2026-06-01T18:26:01+00:00

I am new to design patterns and I was asked to print numbers from

  • 0

I am new to design patterns and I was asked to print numbers from 1 to 10 using decorator pattern. I am sorry if this is trivial but I need to learn. This is what I have so far:

Interface

public interface NextNumber {
    public int getNextNumber(int n);
}

Abstract Class

abstract public class PrintNumbers implements NextNumber {
    protected final NextNumber next;
    protected int num;

    public PrintNumbers(NextNumber next, int num)
    {
        this.next = next;
        this.num = num;
    }

    public int getNextNumber(int num)
    {
        return num+1;
    }
}

DecoratorClass

public class DecoratorCount extends PrintNumbers {
    public DecoratorCount(NextNumber next, int num)
    {
        super(next, num);
    }

    public static void main(String[] args)
    {
        int i = 0;
    }
}

Not sure how to proceed or even if I am going the right way. Could someone shed some light?

  • 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-01T18:26:02+00:00Added an answer on June 1, 2026 at 6:26 pm

    First the decorator class does not have to extend the class that decorates but implements the same interface.

    Look at this Wikipedia page.

    So you can correct your decorator like this:

    // The interface
    public interface NextNumber {
    
         public int getNextNumber();
    }
    
    // The class to decorate
    public class PrintNumbers implements NextNumber {
    
        protected int num;
    
        public PrintNumbers(int startFrom)
        {
            this.num = startFrom;
        }
    
        public int getNextNumber()
        {
            return num++;
        }
    }
    
    // The abstract decorator
    public abstract class DecoratorCount implements NextNumber {
    
        private PrintNumbers pn;
    
        public DecoratorCount(PrintNumbers pn)
        {
           this.pn = pn;
        }
    }
    

    Then for example you can multiply number by 2.

    public class DoubleDecoratorCount extends DecoratorCount {
    
        public DecoratorCount(PrintNumbers pn)
        {
            super(pn);
        }
    
        public int getNextNumber()
        {    
            return pn.getNextNumber() * 2;
        }
    }
    

    And you can test decorator in this way

    public class Test {
    
        public static void main (String[] args) {
            PrintNumbers pn = new PrintNumbers(0);
            DoubleDecoratorCount decorator = new DoubleDecoratorCount(pn);
            for (int i = 0 ; i < 5 ; ++i)
                System.out.println("value: " + decorator.getNextNumber());
        }
    }
    

    At this point you can write all decorators you need:

    • To multiply by 3;
    • To write results in letter;
    • To write results in hex;
    • Etc…
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to WPF and using the MVVM design pattern. To help learn
I'm fairly new to design patterns. What is the best pattern when a developer
I am new to design patterns, but I have been trying hard to implement
I have been looking at the decorator design pattern (I am new to the
I'm new to design patterns, I have a use case where I need to
This is the extend function from the book Pro JavaScript Design Patterns function extend(subClass,
I'm pretty new to Design Patterns.I just came across Factory Design Pattern. I understood
I'm new to design patterns and I cant really see a difference between this
From Head First design patterns book, the singleton pattern with double checked locking has
I need new design patterns in JPA/ORM instead of POJO/JDBC/DAO patterns. Is there any

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.