Decorator MVC

In the im project working on us, we have a strict MVC structure. I am thinking of adding a decorator template to some modal windows (tiny pop-ups) for those implementations where I want some additional features.

So I basically have the following:

SimpleModalWindowController.class
SimpleModalWindowModel.class
SimpleModalWindowView.class

EDIT: Question: Is it possible to implement a decorator template for this for the new ModalWindows features or should I go with inheritance? I will have many different windows, and I would like to combine some of them in the future.

If I follow the decorator pattern, which class will be abstract?

Will it be a class that combines all of the above classes, such as SimpleModal.class, to set them as an abstract class, or do I have more than one abstract class?

I am obviously new to this pattern and only got average OOP skills, so please be patient.

Thanks for any help.

/ Marthin

+3
source share
2 answers

I do not think that this refers to the sample decorator. What you are trying to do is create a specialization inheritance hierarchy (Fancy).

You do not need to implement Decorator. The way you implement it looks great when it comes to design issues. In this case, you do not need to use a template.

Here's how a decorator that you don't need will be implemented. I am a C # guy, so the syntax may not be entirely correct.

abstract class ModalWindowModel
{
  protected ModalWindowModel modalWindowModel; //This can be any class implementing/derived from ModalWindowModel
}

class SimpleModalWindowModel extends ModalWindowModel
{
 SimpleModalWindowModel(ModalWindowModel modalWindowModel)
 {
  this.modalWindowModel = modalWindowModel;
 }


 // your other code goes here
}

class FancyModalWindowModel extends ModalWindowModel
{
 FancyModalWindowModel(ModalWindowModel modalWindowModel)
 {
  this.modalWindowModel = modalWindowModel;
 }

 // your other code goes here
}

...
// Usage
ModalWindowModel simpleModalWindowModel = new SimpleModalWindowModel(null);
ModalWindowModel fancyModalWindowModel = new FancyModalWindowModel(simpleModalWindowModel);
....
+1
source

. . - :

class ExtendedModalWindowModel {
   private ModalWindowModel model;
   public ExtendedModalWindowModel(ModalWindowModel model) {
      if (model == null) throw IllegalArgumentException("...");
      this.model = model;
   }

   // delegate common methods to the parent
   public int getSize() {
      // you could also put additional functionality here...
      return model.getSize();
   }

   // implement new functionality on the decorator
   public void doNewThings() {
      // ...
   }
}

tnterfaces, ( - ).

- java: http://download.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html

+1

All Articles