比如,我们已经有一个付款的方法 payMoney(long count) ,这个方法只负责将钱付给对方,不做其他操作,但是这时我们需要记录下这个付款的操作,并把这个操作记录到日志文件中以方便日后的查看,比如这个方法叫做 insertLog(String str) ,有一种办法就是使用继承,并复写相应的 payMoney(long count) 方法,这样也可以满足条件,但是问题出来了,如果我们又有其他的操作,例如通知付款人已经将款额付出 notic(String username) ,这时候这么办,当然我们仍然可以通过继承来实现,可是如果我现在有不想将付款操作记录到日志文件中了,那我又该这么办?
再将问题简化,我们要在控制台输出字符,但是需要在字符前后加上“*”和“+”,也就是说,我可能需要在我输入的字符前后加上“*”,也可能过了会儿又想加“+”而不是“*”,或者又想“*”和“+”都加上,有或者都不想加。
其中,MyText 类为我们要显示的字符类,而 DecoratorTextConsole 及其子类为我们的 装饰类 。
具体代码如下:
/**********************************************************************/
package decorator.interfaces;
public interface TextConsole {
public void printWords();
public void readWords();
}
/**********************************************************************/
package decorator.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import decorator.interfaces.TextConsole;
public class MyText implements TextConsole {
private String words;
public void printWords() {
System.out.print(words);
}
public void readWords() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.print("please enter some word to display:");
words = br.readLine();
}catch(IOException e){
e.printStackTrace();
words = "There are some errors occured during your entering!";
}
}
}
/**********************************************************************/
package decorator.interfaces.impl;
import decorator.interfaces.TextConsole;
public class DecoratorTextConsole implements TextConsole {
private TextConsole text;
public DecoratorTextConsole(TextConsole text){
this.text = text;
}
public void printWords() {
text.printWords();
}
public void readWords() {
text.readWords();
}
}
/**********************************************************************/
package decorator.interfaces.impl;
import decorator.interfaces.TextConsole;
public class AsteriskTextConsole extends DecoratorTextConsole {
public AsteriskTextConsole(TextConsole text){
super(text);
}
public void printWords(){
addAsterisk();
super.printWords();
addAsterisk();
}
private void addAsterisk(){
System.out.print("*");
}
}
/**********************************************************************/
package decorator.interfaces.impl;
import decorator.interfaces.TextConsole;
public class PlusTextConsole extends DecoratorTextConsole {
public PlusTextConsole(TextConsole text){
super(text);
}
public void printWords(){
addPlus();
super.printWords();
addPlus();
}
private void addPlus(){
System.out.print("+");
}
}
/**********************************************************************/
package decorator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import decorator.interfaces.TextConsole;
import decorator.interfaces.impl.AsteriskTextConsole;
import decorator.interfaces.impl.PlusTextConsole;
import decorator.service.MyText;
public class TestMain {
public static void main(String[] args){
TextConsole mt = new MyText(); // mt 实例在整个被调用的过程中都不会改变,但是它的其他装饰器类却是动态生成的,而且是可自由“拆卸”的,这样就比单一的继承灵活的多。
mt.readWords();
mt.printWords();
System.out.println();
String commond = new String();
System.out.println("please enter commond: (add ?(-plus | -astr | -all) | exit");
while(!commond.equals("exit")){
try{
commond = new BufferedReader(new InputStreamReader(System.in)).readLine();
}catch(IOException e){
e.printStackTrace();
commond = "exit";
}
if(commond.equals("add -plus")){
TextConsole ptc = new PlusTextConsole(mt);
ptc.printWords();
System.out.println();
}else if(commond.equals("add -astr")){
TextConsole atc = new AsteriskTextConsole(mt);
atc.printWords();
System.out.println();
}else if(commond.equals("add -all")){
TextConsole tc = new AsteriskTextConsole(new PlusTextConsole(mt));
tc.printWords();
System.out.println();
}else{
mt.printWords();
System.out.println();
}
}
}
}
/**********************************************************************/
相关推荐
主要介绍了PHP设计模式:装饰器模式Decorator,结合实例形式分析了PHP装饰器模式Decorator相关概念、功能、原理、用法及操作注意事项,需要的朋友可以参考下
装饰器(Decorator)模式 详细教程,简明易懂,非常清晰的,附带有代码示例。
demo python使用装饰器(Decorator)的方式实现单例模式 functools.wraps 则可以将原函数对象的指定属性复制给包装函数对象, 默认有 __module__、__name__、__doc__,或者通过参数选择
主要为大家详细介绍了java设计模式之装饰器模式Decorator,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
c++设计模式-结构型模式-装饰器模式;QT工程;c++简单源码; 装饰器(Decorator)模式的定义:指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。
主要为大家详细介绍了java实现装饰器模式Decorator Pattern,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。 这种模式创建了一个装饰类,用来包装原有的类,并在...
主要介绍了C#装饰器模式(Decorator Pattern),以一个完整实例形式讲述了C#装饰器模式的实现过程,有助于深入理解C#程序设计思想,需要的朋友可以参考下
主要介绍了Java设计模式之装饰模式(Decorator模式)介绍,本文讲解了为什么使用Decorator、如何使用装饰模式、Jive中的Decorator实现等内容,需要的朋友可以参考下
控制访问,装饰器设计的四个名词,装饰器模式与代理模式的区别,
“就增加功能来说,Decorator 模式相比生成子类更为灵活” 这句话的含义是,组合比继承更灵活,当可拓展的功能很多时,继承方案会产生大量的子类,而组合可以提
一、装饰器decorator decorator设计模式允许动态地对现有的对象或函数包装以至于修改现有的职责和行为,简单地讲用来动态地扩展现有的功能。其实也就是其他语言中的AOP的概念,将对象或函数的真正功能也其他辅助的...
原文地址 ... 介绍 我写这篇文章的主要目的是...装饰器 vs 装饰器模式 Decorator模式是一个面向对象的设计模式,它允许动态地往现有的对象添加行为。当你装饰了一个对象,在某种程度上,你是在独立于同一个类的其他实例
装饰器模式(Decorator Pattern)是结构型设计模式之一,用于动态地向对象添加额外的职责,而不影响其他对象。这种模式通过使用装饰器函数,允许你在保持原始功能的基础上,灵活地扩展代码的行为。
主要介绍了Python中设计模式之Decorator装饰器模式模式,文中详细地讲解了装饰对象的相关加锁问题,需要的朋友可以参考下
什么是装饰器模式 作为一种结构型模式, 装饰器(Decorator)模式就是对一个已有结构增加”装饰”. 适配器模式, 是为现在有结构增加的是一个适配器类,.将一个类的接口,转换成客户期望的另外一个接口.适配器让原本接口...
就增加功能来说,Decorator模式相比生成子类更为灵活【GOF95】 装饰模式是以对客户透明的方式动态地给一个对象附加上更多的职责。这也就是说,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不...