cms网站是什么意思,公司建设网站费用如何入帐,郑州社交网站开发,精美化妆品网站模板装饰器模式应用场景嗨#xff0c;您好#xff01; 今天#xff0c;我将展示装饰设计模式的实际应用。 装饰器设计模式是一种广泛使用的设计模式#xff0c;同时在运行期间处理图形#xff0c;树木和动态更改。 如果您正在寻找或尝试进行递归#xff0c;这也是一个不错的… 装饰器模式应用场景 嗨您好 今天我将展示装饰设计模式的实际应用。 装饰器设计模式是一种广泛使用的设计模式同时在运行期间处理图形树木和动态更改。 如果您正在寻找或尝试进行递归这也是一个不错的选择。 我喜欢它。 在这篇文章中我们将实现学生装饰。 我们将用学位和医生的头衔装饰它。 看看它如何在现实世界中工作的一个很好的例子。 首先让我们看一下其最简单的版本的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.componentcomponent;}
}类比我们的学生的例子 让我们首先从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.girlgirl;}
}
// 3. DEFINING CONCRETE DECORATORS
public class Science extends GirlDecorator {public Science(Girl girl) {super(girl);}Overridepublic String getDescription() {// DECORATES WITH A SCIENCES 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 SCIENCES DEGREEgirl new Science(girl);System.out.println(girl.getDescription());// DECORANTING AMERICANA GIRL WITH ARTS 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装饰器模式应用场景