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?
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:
Then for example you can multiply number by 2.
And you can test decorator in this way
At this point you can write all decorators you need: