Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very Simple Use of decorator pattern to generate numbers

Tags:

java

decorator

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?

like image 745
noMAD Avatar asked Apr 13 '12 06:04

noMAD


People also ask

What is the decorator pattern used for?

The Decorator Pattern allows class behavior to the decorated dynamically. It's a structural design pattern as it's used to form large object structures across many disparate objects. The concept of decorator is that it adds additional attributes to an object dynamically.

What is Decorator design pattern example?

The decorator design pattern is a structural pattern, which provides a wrapper to the existing class. Decorator design pattern uses abstract classes or interfaces with the composition to implement the wrapper.

What do decorator patterns solve?

Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.

What is the decorator pattern also known as?

A Decorator Pattern says that just "attach a flexible additional responsibilities to an object dynamically". In other words, The Decorator Pattern uses composition instead of inheritance to extend the functionality of an object at runtime. The Decorator Pattern is also known as Wrapper.


2 Answers

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...
like image 134
dash1e Avatar answered Oct 29 '22 17:10

dash1e


First, you need something that could need some decoration, then you need a decorator for that something. Both classes, the base class and the decorator are concrete classes and can be used directly.

Decorating a counter from 1 to 10 doesn't make much sense (numbers are always beautiful ;) ). But you could implement a basic class that prints the numbers unformatted and then implement a decorator, that adds some beauty to the printed result.

like image 28
Andreas Dolk Avatar answered Oct 29 '22 17:10

Andreas Dolk