读书人

装点器模式[Decorator]

发布时间: 2012-09-14 11:53:44 作者: rapoo

装饰器模式[Decorator]

其中,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();
??? }
?? }
}
}
/**********************************************************************/

读书人网 >软件架构设计

热点推荐