嗨,您好!
今天,我将展示装饰设计模式的实际应用。 装饰器设计模式是一种广泛使用的设计模式,同时在运行期间处理图形,树木和动态更改。
如果您正在寻找或尝试进行递归,这也是一个不错的选择。 我喜欢它。 在这篇文章中,我们将实现学生装饰。 我们将用学位和医生的头衔装饰它。 看看它如何在现实世界中工作的一个很好的例子。
首先,让我们看一下它最简单的版本的UML图。 之后,我们将以此类推为例。
最简单的UML
请密切注意,因为一旦您了解了所有内容,一切将变得清晰易懂。 这就是我将最简单的模型放在第一位的原因。
了解细节
装饰性是通用接口Decorable的每个具体实现。 装饰器将是抽象类Decorator的每个实现。 定义装饰者的合同,将一个实例包含到可装饰对象中。 让我们深入研究一些代码来修复它:
// 1. COMMON INTERFACE FOR DECORABLES
public interface Decorable {public String getDescription();
}
// 2. THE ABSTRACT DECORADOR WHICH HOLDS A REFERENCE TO DECORABLES
public abstract class Decorator implements Decorable {protected Decorable component;public Decorator(Decorable component){super();this.component=component;}
}
类比我们的学生的例子
让我们首先从UML图开始:
普通可装饰女孩
从类比开始。 界面Girl是可装饰的。 GirlDecorator用下面的具体装饰器定义抽象装饰器的合同。
// 1. COMMON INTERFACE FOR DECORABLES
public interface Girl {public String getDescription();
}
// 2. THE ABSTRACT DECORADOR WHICH HOLDS A REFERENCE TO DECORABLES
public abstract class GirlDecorator implements Girl {protected Girl girl;public GirlDecorator(Girl girl){super();this.girl=girl;}
}
// 3. DEFINING CONCRETE DECORATORS
public class Science extends GirlDecorator {public Science(Girl girl) {super(girl);}@Overridepublic String getDescription() {// DECORATES WITH A SCIENCE'S DEGREEreturn girl.getDescription() + "+Like Science";}public void caltulateStuff() {// ADDS NEW FEATURES (METHOD) TO ITSystem.out.println("scientific calculation!");}
}
public class Art extends GirlDecorator {public Art(Girl girl) {super(girl);}@Override public String getDescription() {return girl.getDescription() + "+Like Art";}public void draw() {System.out.println("draw pictures!");}
}
public class Doctor extends GirlDecorator {public Doctor(Girl girl) {super(girl);}@Override public String getDescription() {return girl.getDescription() + "+Like Doctor";}public void calculateStuff() {System.out.println("doctor calculation!");}public void doctorTitle() {System.out.println("doctor title");}
}
装饰物
AmericanGirl和EuropeanGirl是将在运行时以学位和医生名义装饰的装饰,以增强其课程和能力。
// 4. AN AMERICAN GIRL WILL BE DEFINED AS A DECORABLE
public class AmericanGirl implements Girl {private String description="";// NORMAL AMERICAN GIRLpublic AmericanGirl(){super();description = "+American";}@Override public String getDescription() {return description;}
}
public class EuropeanGirl implements Girl {private String description="";public EuropeanGirl() {super();description = "+European";}@Override public String getDescription() {return description;}
}
测试它
现在让我们在实践中看看它的外观。 我们如何在运行时装饰和增强其功能:
public class Client {public static void main(String[] args) {// COMMOM GIRLGirl girl;// CREATING NORMAL AMERICAN GIRLgirl = new AmericanGirl();System.out.println(girl.getDescription());// DECORANTING AMERICANA GIRL WITH SCIENCE'S DEGREEgirl = new Science(girl);System.out.println(girl.getDescription());// DECORANTING AMERICANA GIRL WITH ART'S DEGREEgirl = new Art(girl);System.out.println(girl.getDescription());// EUROPEAN GIRL HAS ALREADY ALL DEGREES Girl europeia = new Science(new Art(new EuropeanGirl()));System.out.println(europeia.getDescription());// DOCTOR HAS NEW FUNCTIONS girl = new Doctor(girl);System.out.println(girl.getDescription());// BECAUSE DOCTOR EXTENDS FROM COMMON GIRL, IT CAN DO A DOWNCAST((Doctor)girl).doctorTitle();((Doctor)girl).calculateStuff();// PAY ATTENTION THAT WE USE THE SAME INSTANCE, BUT THEY BEHAVIOR DIFFERENT// AT DIFFERENT TIME SLOTS. THE CLIENT HAS THE IMPRESSION THAT WE HAVE// CHANGED THE IMPLEMENTATION, BUT IN FACT NOT.}
}
就这样! 希望你喜欢!
翻译自: https://www.javacodegeeks.com/2014/08/decorator-design-pattern-applied.html