开发者

MVC with Decorator pattern

开发者 https://www.devze.com 2023-03-03 16:24 出处:网络
In a project im working on we hold a strict MVC structure. Im thinking of adding a decorator pattern to some of the modal windows (tiny popup windows) for those implementations where i whant some extr

In a project im working on we hold a strict MVC structure. Im thinking of adding a decorator pattern to some of the modal windows (tiny popup windows) for those implementations where i whant some extra feat开发者_Go百科ures.

So I basicly have the following:

SimpleModalWindowController.class
SimpleModalWindowModel.class
SimpleModalWindowView.class

EDIT: Question: Is it possible to implement a decorator pattern upon this for new ModalWindows implemenations or should i go with inheritance? I will have many different windows and I would like to combine some of there functionalities in the future.

If I go by decorator pattern, what class whould be the abstract one?

Would it be a class that combines all the above classes like SimpleModal.class to set them up as an abstract class or do I have more then one abstract class?

Im obviously new to this pattern and only got average OOP-skills, so please have some patience.

Thanks for any help.

/Marthin


I don't think this qualifies as a decorator pattern. What you are trying to do is create inheritance hierarchy with specialization (Fancy).

You don't need to implement Decorator. The way you are implementing looks fine if that is addressing the design issue. You don't have to use a pattern in this case.

This is how a decorator would be implemented which you don't need. I am a C# guy, hence syntax may not be fully 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);
....


Personally i would implement it as a decorator. I generally prefer composition over inheritance for a variety of reasons. Thereby i would do something like:

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() {
      // ...
   }
}

Should your decorated classes implement tnterfaces, you should also implement them in the decorator (but still delegate to the decorated instance for their implementation-unless you want to extend or override the base functionality).

A good example of composition is the implementation of I/O in java: http://download.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号