标签:建立 设计模式 mat style exce hit throw -- 微软
要求对代码进行随意的加数字,封号,引号等输出
BufferedLineNum2.java(行号)
package com.DesignPatterns.ac.decorator1; import java.io.BufferedReader; import java.io.IOException; public class BufferedLineNum2 extends BufferedReader { // 在内部维护一个被装饰类的引用。 BufferedReader bufferedReader; int count = 1; public BufferedLineNum2(BufferedReader bufferedReader) { super(bufferedReader);// 注意: 该语句没有任何的作用,只不过是为了让代码不报错。 this.bufferedReader = bufferedReader; } public String readLine() throws IOException { String line = bufferedReader.readLine(); if (line == null) { return null; } line = count + " " + line; count++; return line; } } |
BufferedQuto2.java(引号)
package com.DesignPatterns.ac.decorator1; import java.io.BufferedReader; import java.io.IOException; public class BufferedQuto2 extends BufferedReader { // 在内部维护一个被装饰的类 BufferedReader bufferedReader; public BufferedQuto2(BufferedReader bufferedReader) { // new BufferedSemi2(); super(bufferedReader); // 只是为了让代码不报错.. |
this.bufferedReader = bufferedReader;
}
public String readLine() throws IOException {
String line = bufferedReader.readLine(); // 如果这里的ReadLine方法是调用了 buffereLineNum的readLine方法,问题马上解决。
if (line == null) {
return null;
}
line = "\"" + line + "\"";
return line; }
}
BufferedSemi2.java(封号)
package com.DesignPatterns.ac.decorator1; import java.io.BufferedReader; import java.io.IOException; class BufferedSemi2 extends BufferedReader { // 为什么要继承? 是为了让这些装饰类的对象可以作为参数进行传递,达到互相装饰 的效果。 // 在内部维护一个被装饰类的引用。 BufferedReader bufferedReader; public BufferedSemi2(BufferedReader bufferedReader) { // new BuffereLineNum(); super(bufferedReader);// 注意: 该语句没有任何的作用,只不过是为了让代码不报错。 this.bufferedReader = bufferedReader; } public String readLine() throws IOException { String line = bufferedReader.readLine(); // 如果这里的ReadLine方法是调用了 buffereLineNum的readLine方法,问题马上解决。 if (line == null) { return null; } line = line + ";"; return line; } } |
Demo2(测试)
package com.DesignPatterns.ac.decorator1; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class Demo2 { public static void main(String[] args) throws IOException { File file = new File("F:\\Demo1.java"); FileReader fileReader = new FileReader(file); // 建立缓冲输入字符流 BufferedReader bufferedReader = new BufferedReader(fileReader); // 建立带行号的缓冲输入字符流 BufferedLineNum2 bufferedLineNum = new BufferedLineNum2(bufferedReader); // 带分号的缓冲输入字符流 BufferedSemi2 bufferedSemi2 = new BufferedSemi2(bufferedLineNum); // 带双引号的缓冲输入字符流 BufferedQuto2 bufferedQuto2 = new BufferedQuto2(bufferedSemi2); String line = null; while ((line = bufferedQuto2.readLine()) != null) { System.out.println(line); } } } |
结果:
"1 package com.DesignPatterns.ac.decorator1;;"
"2 ;"
"3 import java.io.File;;"
"4 import java.io.FileReader;;"
"5 import java.io.IOException;;"
"6 ;"
"7 public class Demo1 {;"
"8 public static void main(String[] args) throws IOException {;"
"9 File file = new File("F:\\Demo1.java");;"
"10 //建立数据的输入通道;"
"11 FileReader fileReader = new FileReader(file);;"
"12 //建立带行号的缓冲输入字符流;"
"13 BufferedLineNum bufferedLineNum = new BufferedLineNum(fileReader);;"
"14 //带有分号的缓冲输入字符流;"
"15 BufferedSemi bufferedSemi = new BufferedSemi(fileReader);;"
"16 | //带有双引号的缓冲输入字符流;" |
"17 | BufferedQuto bufferedQuto = new BufferedQuto(fileReader);;" |
"18 | String line = null;;" |
"19 | while((line = bufferedQuto.readLine())!=null){;" |
"20 | System.out.println(line);;" |
"21 "22 };" "23 };" | };" |
一家三口每个人都会工作,儿子的工作就是画画,母亲的工作就是在儿子的基础上做一个增强,不单止可以画画,还可以上涂料。爸爸的工作就是在妈妈基础上做了增强,就是上画框。
Work.java
package com.DesignPatterns.ac.decorator2;
public interface Work {
public void work();
}
Son.java
package com.DesignPatterns.ac.decorator2;
public class Son implements Work {
public void work() {
System.out.println("画画...");
}
}
Mather.java
package com.DesignPatterns.ac.decorator2;
public class Mather implements Work { Work worker;
public Mather(Work worker) {
this.worker = worker; }
public void work() {
worker.work();
System.out.println("给画上颜色..");
} | } |
Father.java
package com.DesignPatterns.ac.decorator2;
public class Father implements Work { Work worker;
public Father(Work worker) {
this.worker = worker; }
@Override
public void work() {
worker.work();
System.out.println("上画框...");
}
}
Demo3.java
package com.DesignPatterns.ac.decorator2; public class Demo3 { public static void main(String[] args) { Son s = new Son(); Mather m = new Mather(s); Father f = new Father(m); f.work(); } } 画画... 给画上颜色.. 上画框... |
标签:建立 设计模式 mat style exce hit throw -- 微软
原文地址:https://www.cnblogs.com/qingruihappy/p/9693870.html