简体   繁体   中英

Showing Decorator Pattern

I'm studying for an exam, and this was on an old test:

Given:

public interface Interface1 {

    void method1();

    void method2().

}

Show the Decorator pattern class for the interface.

What is it, and does anyone have any resources on where I can learn more about this?

There is a good example from here .

What you essentially are doing is creating a simple version Interface1 and then adding more functionality to it by create decorations (additional classes) but always ensuring that the decorations have the same interface thus allowing them to be used in the same places as any undecorated items.

From the above link, I've annotated the example.

Take a simple class like window and then decorate it with scroll bars:

// the Window interface
interface Window {
    public void draw(); // draws the Window
    public String getDescription(); // returns a description of the Window
}

// implementation of a simple Window without any scrollbars
class SimpleWindow implements Window {
    public void draw() {
        // draw window
    }

    public String getDescription() {
        return "simple window";
    }
}

These are the decorators, first an abstract class to with all the common code for the decorator pattern. - note that it implements Window

// abstract decorator class - note that it implements Window
abstract class WindowDecorator implements Window {
    protected Window decoratedWindow; // the Window being decorated

    public WindowDecorator (Window decoratedWindow) {
        this.decoratedWindow = decoratedWindow;
    }
   public void draw() {
        decoratedWindow.draw();
    }
}

Now a decoration that adds a Vertical scroll bar to the window. Note it extends WindowDecorator and thus Window and thus the interface Window

// the first concrete decorator which adds vertical scrollbar functionality
class VerticalScrollBarDecorator extends WindowDecorator {
    public VerticalScrollBarDecorator (Window decoratedWindow) {
        super(decoratedWindow);
    }

    public void draw() {
        decoratedWindow.draw();
        drawVerticalScrollBar();
    }

    private void drawVerticalScrollBar() {
        // draw the vertical scrollbar
    }

    public String getDescription() {
        return decoratedWindow.getDescription() + ", including vertical scrollbars";
    }
}

Examples of GoF Design Patterns in Java's core libraries has links to lots of other patterns implemented in Java.

Preet post is answer for your question

if you want learn more about design patterns I strongly recommend this post and BalusC answer, it shows real life examples with design patterns

http://www.javacamp.org/designPattern/decorator.html

http://en.wikipedia.org/wiki/Decorator_pattern

The decorator pattern can be used to make it possible to extend (decorate) the functionality of a certain object at runtime, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new decorator class that wraps the original class. This wrapping could be achieved by the following sequence of steps:

  1. Subclass the original "Component" class into a "Decorator" class (see UML diagram);
  2. In the Decorator class, add a Component pointer as a field;
  3. Pass a Component to the Decorator constructor to initialize the Component pointer;
  4. In the Decorator class, redirect all "Component" methods to the "Component" pointer; and
  5. In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.

for your example

public interface Interface1 {

    void method1();

    void method2().

}

public SimpleInterface implements Interface1 {

    void method1() {
        //method1 actions
    }

    void method2() {
        //method2 actions
    }

}

abstract decorator class

abstract class InterfaceDecorator implements Interface {
    protected InterfaceDecorator decoratedInterface;

    public InterfaceDecorator (Interface decoratedInterface) {
        this.decoratedInterface = decoratedInterface;
    }

    void method1() {
        decoratedInterface.method1()
    }

    void method2() {
        decoratedInterface.method2()
    }
}

concrete decorator class

class Method1InterfaceDecorator extends InterfaceDecorator {
    public Method1InterfaceDecorator(Interface decoratedInterface) {
        super(decoratedInterface);
    }

    void method1() {
        decoratedInterface.method1();
        method3()
    }

    void method3() {
        //method3 actions
    }
}

usage:

public static void main(String[] args) {
    Interface simpleInterface = new SimpleInterface();
    Interface decoratedInterface = new Method1DecoratedInterface (new SimpleInterface());

    // These two method1 calls will behave differently
    simpleInterface.method1();
    decoratedInterface.method1();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM