当前位置:主页 > 资料 >

Decorator Design Pattern in Java
栏目分类:资料   发布日期:2018-08-03   浏览次数:

导读:本文为去找网小编(www.7zhao.net)为您推荐的Decorator Design Pattern in Java,希望对您有所帮助,谢谢! One way to do that is by using the traditional inheritance. We will create a base abstract gadget class and multip

本文为去找网小编(www.7zhao.net)为您推荐的Decorator Design Pattern in Java,希望对您有所帮助,谢谢!

本文来自去找www.7zhao.net



One way to do that is by using the traditional inheritance. We will create a base abstract gadget class and multiple subclasses for different gadget combination.

去找(www.7zhao.net欢迎您

But, wait! I have n numbers of gadgets, so, in my case, I have to create n subclasses.

欢迎访问www.7zhao.net

If n = 5, then, for 5 powers, the number of subclasses will be 5! = 120. For 5 gadgets, we have to create 120 subclasses. Is there any other simple way to do that? 去找(www.7zhao.net欢迎您

Yes, we can use the decorator design pattern to solve this problem.

copyright www.7zhao.net

The decorator design pattern allows behavior to be added to an individual , either statically or dynamically, without affecting the behavior of other objects from the same .

内容来自www.7zhao.net

The decorator design pattern   comes under the structural pattern. This pattern creates a decorator class that wraps around the original class and provides additional functionality, keeping class methods' signature intact.

去找(www.7zhao.net欢迎您

We will create a   Gadget   interface and concrete classes implementing the  Gadget   interface. We will, then, create an abstract decorator class Gadgate  Decorator implementing the   Gadget   interface and having   Gadget   object as its instance variable.

FlightGadget and   UnibeamChestProjectorGadget   are concrete classes implementing the   GadgateDecorator . 欢迎访问www.7zhao.net

The suit class will implement the Gadget   interface, and it will be our base gadget or first gadget. 本文来自去找www.7zhao.net

Gadget.java
package com.genericclass.decorator.design.pattern;

public interface Gadget {
     Gadget addGadget();
    void showGadget();
} 欢迎访问www.7zhao.net 

GadgetDecorator.java

www.7zhao.net

package com.genericclass.decorator.design.pattern;

public abstract class GadgetDecorator implements Gadget{ 
  Gadget nextGadget;
  public GadgetDecorator(Gadget nextGadget) { 
    this.nextGadget = nextGadget; 
  } 
  @Override public
  Gadget addGadget() { 
     return this; 
   }
   @Override 
   public void showGadget() { 
     nextGadget.showGadget(); 
   }
} 

本文来自去找www.7zhao.net

FlightGadget.java copyright www.7zhao.net

package com.genericclass.decorator.design.pattern;
public class FlightGadget extends GadgetDecorator {
 public FlightGadget(Gadget nextGadget) { 
   super(nextGadget);
 } 
  @Override 
  public void showGadget() { 
   super.showGadget(); 
   System.out.println(" Flying Gadget..."); 
  }
} 本文来自去找www.7zhao.net 

UnibeamChestProjectorGadget.java

本文来自去找www.7zhao.net

package com.genericclass.decorator.design.pattern;
public class UnibeamChestProjectorGadget extends GadgetDecorator {
  public UnibeamChestProjectorGadget(Gadget nextGadget) { 
    super(nextGadget); 
  } 
  @Override 
  public void showGadget() { 
    super.showGadget(); 
    System.out.println(" Unibeam Chest Projector..."); 
  }
} 

欢迎访问www.7zhao.net

Suit.java 内容来自www.7zhao.net

package com.genericclass.decorator.design.pattern;
public class Suit implements Gadget{
 @Override 
public Gadget addGadget() {
 return this; }
 @Override
 public void showGadget() {
  System.out.println(" Iron Man Suit...");
 } 
} 

www.7zhao.net

IronMan.java 去找(www.7zhao.net欢迎您

package com.genericclass.decorator.design.pattern;
public class IronMan { 
 Gadget gadget = new Suit(); 
  public  void addNewGadget(String gadgetName) { 
    if(gadgetName.equals("Flight")) 
       this.gadget = new FlightGadget(gadget); 
    else 
      this.gadget = new UnibeamChestProjectorGadget(gadget);
   } 

   public void showGadget() { 
      System.out.println("My Gadgets:");
      gadget.showGadget(); 
    }
 } 欢迎访问www.7zhao.net 

Main.java www.7zhao.net

package com.genericclass.decorator.design.pattern;
public class Main {
 public static void main(String[] args) { 
  IronMan ironMan = new IronMan(); 
  ironMan.addNewGadget("Unibeam Chest Projector"); 
  ironMan.addNewGadget("Flight"); 
  ironMan.showGadget(); }
} 

内容来自www.7zhao.net

Output:

内容来自www.7zhao.net

My Gadgets:
  Iron Man Suit... 
  Unibeam Chest Projector... 
  Flying Gadget... 本文来自去找www.7zhao.net 

In conclusion, the decorator pattern provides greater flexibility than static inheritance. It enhances the extensibility of the object because changes are made by coding new classes. 内容来自www.7zhao.net

The decorator pattern can be used when we want to add responsibilities dynamically at runtime. copyright www.7zhao.net

本文来自去找www.7zhao.net


本文原文地址:https://dzone.com/articles/decorator-design-pattern-1

以上为Decorator Design Pattern in Java文章的全部内容,若您也有好的文章,欢迎与我们分享!

去找(www.7zhao.net欢迎您

Copyright ©2008-2017去找网版权所有   皖ICP备12002049号-2 皖公网安备 34088102000435号   关于我们|联系我们| 免责声明|友情链接|网站地图|手机版