通过继承增强一个类的功能 代码结果清晰,通俗易懂 缺点:不灵活,导致继承体系过于庞大 但是:
装饰者设计模式:增强一个类的功能,还可以让这些类相互装饰
package com.day31;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
//BufferedWriter拓展了FileReader的功能
//编写一个类拓展BufferedReader的功能,增强readLine方法返回的方法带有字符串
class BufferedLineNum extends BufferedReader {
public BufferedLineNum(Reader in) {
super(in);
// TODO Auto-generated constructor stub
}
int count = 1; // 放到这里才不至于每次调用readLine()都给count重新赋值
public String readLine() throws IOException {
String line = super.readLine();
if(line==null){
return null;
}
line = count + " " + line;
count++;
return line;
}
}
public class demo {
public static void main(String[] args) throws IOException {
File file=new File("F:\\code\\users.txt");
FileReader filereader=new FileReader(file);
BufferedLineNum bufferedline=new BufferedLineNum(filereader);
String line=null;
while((line=bufferedline.readLine())!=null){
System.out.println(line);
}
}
}
package com.day31;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
//BufferedWriter拓展了FileReader的功能
//编写一个类拓展BufferedReader的功能,增强readLine方法返回带有“”
class BufferedLineNum extends BufferedReader {
public BufferedLineNum(Reader in) {
super(in);
// TODO Auto-generated constructor stub
}
public String readLine() throws IOException {
String line = super.readLine();
if(line==null){
return null;
}
line ="\"" + line+"\"";
return line;
}
}
public class demo {
public static void main(String[] args) throws IOException {
File file=new File("F:\\code\\users.txt");
FileReader filereader=new FileReader(file);
BufferedLineNum bufferedline=new BufferedLineNum(filereader);
String line=null;
while((line=bufferedline.readLine())!=null){
System.out.println(line);
}
}
}
//装饰者设计模式让这些装饰类拥有一个共同的父类的目的是为了让它们构成一个装饰链,达到互相装饰的效果
package com.day31;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//装饰者设计模式步骤:
//1:在装饰类的内部维护一个被装饰类的引用
//2:让装饰类有一个共同的父类或者父接口
//带序号的缓冲输入字符流
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;
}
}
// 带双引号的缓冲输入字符流
class BufferQuto extends BufferedReader {
// 在内部维护一个被装饰的类
BufferedReader bufferedreader;
public BufferQuto(BufferedReader bufferedreader) {
super(bufferedreader); // 该语句没有任何作用,只是为了不让代码报错
this.bufferedreader = bufferedreader;
}
public String readLine() throws IOException {
String line = bufferedreader.readLine(); // 如果这里的readLine()是调用的BufferedLineNum2的就可以实现两个功能
if (line == null) {
return null;
}
line = "\"" + line + "\"";
return line;
}
}
// 带分号的缓冲输入字符流
class BufferedSemi2 extends BufferedReader {
BufferedReader bufferedreader;
public BufferedSemi2(BufferedReader bufferedreader) {
super(bufferedreader); // 该语句没有任何作用,只是为了不让代码报错
this.bufferedreader = bufferedreader;
}
public String readLine() throws IOException {
String line = bufferedreader.readLine(); // 如果这里的readLine()是调用的BufferedLineNum2的就可以实现两个功能
if (line == null) {
return null;
}
line = line + ";";
return line;
}
}
public class demo {
public static void main(String[] args) throws IOException {
FileReader filereader = new FileReader("F://code//users.txt");
BufferedReader bufferreader = new BufferedReader(filereader);
BufferedLineNum2 buffered2 = new BufferedLineNum2(bufferreader);
BufferedSemi2 bufferedsemi2 = new BufferedSemi2(buffered2);
BufferQuto bufferedquto=new BufferQuto(bufferedsemi2);
String line = null;
while ((line = bufferedquto.readLine()) != null) {
System.out.println(line);
}
}
}
运行结果可以三个功能任意组合:
例如:同时实现三个功能
例题:
package com.day31;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
interface Work {
public void work();
}
class Son implements Work {
public void work() {
System.out.println("画画");
}
}
class Mather implements Work {
Work work1;
public Mather(Work work1) {
this.work1 = work1;
}
public void work() {
work1.work();
System.out.println("上颜色");
}
}
class Father implements Work {
Work work1;
public Father(Work work1) {
this.work1 = work1;
}
public void work() {
work1.work();
System.out.println("上画框");
}
}
public class demo {
public static void main(String[] args) {
Son s = new Son();
Mather m = new Mather(s);
Father f = new Father(m);
m.work();
}
}