裝飾者模式(設計模式)
思想:可以通過裝飾對原來的功能進行升級
例如:BufferedOutputstream
需要Outputstream構造
可以使FileOutputStream(被裝飾者)功能得到提升 相當於裝飾後,寫入效率提高了
ObjectOutputstream
需要Outputstream構造
可以使FileOutputStream被裝飾後功能提升,即可以寫對象
裝飾者模式流程
1.把被裝飾者寫成成員變量
2.構造方法中給該成員變量賦值
3.對原有功能的升級
裝飾者模式的好處
高內聚:類中方法之間的聯繫 聯繫最好緊密
低耦合(解耦):類與類之間儘量不要產生聯繫
代碼示例:
public class Demo01 {
public static void main(String[] args) {
LOStudent student = new LOStudent(new Student());
student.code();
LXStudent student2 = new LXStudent(new Student());
student2.code();
}
}
interface Coder{
public abstract void code();
}
class Student implements Coder{
@Override
public void code() {
System.out.println("c");
System.out.println("c++");
System.out.println("c#");
}
}
class LOStudent implements Coder{
private Student student;
public LOStudent() {}
public LOStudent(Student student) {
super();
this.student = student;
}
@Override
public void code() {
student.code();
System.out.println("java");
System.out.println("html");
System.out.println("js");
}
}
class LXStudent implements Coder{
private Student student;
public LXStudent() { }
public LXStudent(Student student) {
super();
this.student = student;
}
@Override
public void code() {
student.code();
System.out.println("電競");
System.out.println("汽修");
System.out.println("挖掘機");
}
}
LineNumberReader
特點:能獲取行號
代碼示例:
FileReader fr = new FileReader("/Users/lanou/Desktop/test/ppp.txt");
LineNumberReader lnr = new LineNumberReader(fr);
String len = "";
while ((len = lnr.readLine()) != null) {
System.out.println(lnr.getLineNumber() + ":" + len);
}
lnr.close();
利用裝飾者實現LineNumberReader
class MyLineNumberReader {
private Reader reader;
private int num = 0;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public MyLineNumberReader() {
}
public MyLineNumberReader(Reader reader) {
super();
this.reader = reader;
}
public String myRead() throws IOException {
num++;
StringBuilder stringBuilder = new StringBuilder();
int len = 0;
while ((len = reader.read()) != -1) {
if (len == 10) {
return stringBuilder.toString();
} else {
stringBuilder.append((char)len);
}
}
if (stringBuilder.length() == 0) {
return null;
}
return stringBuilder.toString();
}
public void myClose() throws IOException {
reader.close();
}
}
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/lanou/Desktop/test/ppp.txt");
MyLineNumberReader mnr = new MyLineNumberReader(fr);
mnr.setNum(100);
String len = "";
while ((len = mnr.myRead())!=null) {
System.out.println(mnr.getNum() + len);
}
mnr.myClose();
}
打印流
PrintStream(字節)
構造方法可接受的參數:
1.文件(File)
2.字節輸出流(OutputStream)
3.文件路徑
PrintWriter(字符)
1.只能作爲數據的目的地
2.可以把數據原樣輸出
3.永遠不會拋出IO異常 但是可以拋出其他異常 例如空指針
4.打印流相當於可以給其他的流對象裝飾一個原樣輸出的功能
System.in 是BufferedInputStream 緩衝輸入字節流
作用:接受鍵盤輸入的內容
System.out 是PrintStream 打印輸出字節流
作用:輸出內容到控制檯
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test/ppp.txt");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintStream ps = new PrintStream(bos);
ps.println("hahaha");
ps.println(true);
ps.print(100);
ps.print(3.5);
ps.write(100);
ps.println('p');
ps.close();
}
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test/ppp.txt");
PrintWriter pw = new PrintWriter(file);
pw.println("hah");
pw.println(100);
pw.print(true);
pw.flush();
pw.close();
}
public static void main(String[] args) throws IOException {
System.out.println("請輸入");
InputStream in = System.in;
StringBuilder stringBuilder = new StringBuilder();
while (true) {
if (stringBuilder.toString().endsWith("quit")) {
break;
}
int len = in.read();
if (len == 10) {
System.out.println(stringBuilder.toString());
stringBuilder.delete(0, stringBuilder.length());
} else {
stringBuilder.append((char)len);
}
}
}