Skip to content

Commit 5346f51

Browse files
committed
Polish Stage 8 Lesson 1 & 2
1 parent 549b640 commit 5346f51

File tree

22 files changed

+644
-0
lines changed

22 files changed

+644
-0
lines changed

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>stage-4</module>
1717
<module>stage-5</module>
1818
<module>stage-7</module>
19+
<module>stage-8</module>
1920
</modules>
2021

2122
<build>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>deep-in-java</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-8</artifactId>
13+
<packaging>pom</packaging>
14+
<name>「一入 Java 深似海 」系列 :: 第八期</name>
15+
<modules>
16+
<module>stage-8-lesson-1</module>
17+
<module>stage-8-lesson-2</module>
18+
</modules>
19+
20+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>stage-8</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-8-lesson-1</artifactId>
13+
<name>「一入 Java 深似海 」系列 :: 第八期 :: 第一节</name>
14+
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.segementfalut.deep.in.java.io;
2+
3+
/**
4+
* 代理对象和被代理对象不一定存在层次关系或者不在同一个继承层次
5+
* 被代理的功能是被代理对象的子集
6+
*/
7+
public class CharSequenceProxy {
8+
9+
private final CharSequence delegate;
10+
11+
public CharSequenceProxy(CharSequence delegate) {
12+
this.delegate = delegate;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return delegate.toString();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.segementfalut.deep.in.java.io;
2+
3+
4+
import java.util.stream.IntStream;
5+
6+
/**
7+
* 装饰器模式/包装模式
8+
*
9+
*/
10+
public class CharSequenceWrapper implements CharSequence {
11+
12+
private final CharSequence delegate;
13+
14+
public CharSequenceWrapper(CharSequence delegate) {
15+
this.delegate = delegate;
16+
}
17+
18+
public int length() {
19+
return delegate.length();
20+
}
21+
22+
public char charAt(int index) {
23+
return delegate.charAt(index);
24+
}
25+
26+
public CharSequence subSequence(int start, int end) {
27+
return delegate.subSequence(start, end);
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return delegate.toString();
33+
}
34+
35+
public IntStream chars() {
36+
return delegate.chars();
37+
}
38+
39+
public IntStream codePoints() {
40+
return delegate.codePoints();
41+
}
42+
43+
public String getDescription() {
44+
return toString();
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.segementfalut.deep.in.java.io;
2+
3+
import java.io.Console;
4+
import java.io.IOException;
5+
6+
public class CommandLineDemo {
7+
8+
public static void main(String[] args) throws IOException {
9+
System.out.println("按任意键退出...");
10+
// 阻塞线程
11+
// 当前线程 [main]
12+
// Thread.wait();
13+
// I/O 线程等待用户输入 -> 通知 Thread.notify();
14+
System.in.read();
15+
16+
Console console = System.console();
17+
18+
while (true) {
19+
20+
String line = console.readLine();
21+
22+
if ("exit".equals(line)) {
23+
break;
24+
} else {
25+
System.out.println(line);
26+
}
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.segementfalut.deep.in.java.io;
2+
3+
import java.io.DataOutputStream;
4+
import java.io.IOException;
5+
6+
public class DataStreamsDemo {
7+
8+
public static void main(String[] args) throws IOException {
9+
DataOutputStream outputStream = new DataOutputStream(System.out);
10+
outputStream.writeByte(65);
11+
outputStream.writeChar(97);
12+
outputStream.writeUTF("Hello,World");
13+
outputStream.flush();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.segementfalut.deep.in.java.io;
2+
3+
4+
import java.util.stream.IntStream;
5+
6+
/**
7+
* 装饰器模式/包装模式
8+
* 当前装饰器模式实现类只有扩展不重要(并非无意义)的特性(方法),核心特性来自于继承的类或者接口(实际来源外部注入)
9+
* <p>
10+
* A 类型提供了 N 个方法(特性)
11+
* A 类型的装饰对象(Decorator)继承 A 类型
12+
* 代理对象(delegate) is A 类型
13+
* 装饰对象(Decorator)不一定完全按照代理对象(delegate)的行为来执行
14+
*/
15+
public class DecoratingCharSequence implements CharSequence {
16+
17+
private final CharSequence delegate;
18+
19+
public DecoratingCharSequence(CharSequence delegate) {
20+
this.delegate = delegate;
21+
}
22+
23+
public int length() {
24+
return delegate.length();
25+
}
26+
27+
public char charAt(int index) {
28+
return delegate.charAt(index);
29+
}
30+
31+
public CharSequence subSequence(int start, int end) {
32+
return delegate.subSequence(start, end);
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "@" + delegate.toString();
38+
}
39+
40+
public IntStream chars() {
41+
return delegate.chars();
42+
}
43+
44+
public IntStream codePoints() {
45+
return delegate.codePoints();
46+
}
47+
48+
public String getDescription() {
49+
return toString();
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.segementfalut.deep.in.java.io;
2+
3+
import java.util.Formatter;
4+
5+
public class FormattingDemo {
6+
7+
public static void main(String[] args) {
8+
System.out.printf("Hello,%s\n", "World");
9+
System.out.print(String.format("Hello,%s\n", "World"));
10+
System.out.print(new Formatter().format("Hello,%s\n", "World"));
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.segementfalut.deep.in.java.io;
2+
3+
import java.io.InputStream;
4+
import java.io.InputStreamReader;
5+
import java.io.UnsupportedEncodingException;
6+
7+
public class InputStreamToReaderDemo {
8+
9+
public static void main(String[] args) throws UnsupportedEncodingException {
10+
InputStream inputStream = System.in;
11+
// 转化为 Reader
12+
// InputStreamReader 属于适配器模式
13+
// 适配器模式< IN 类型,OUT 类型 >
14+
// 适配对象属于 OUT 类型,依赖注入 IN 类型,IN 类型和 OUT 类型没有直接层次关系
15+
InputStreamReader reader = new InputStreamReader(inputStream,"UTF-8");
16+
}
17+
}

0 commit comments

Comments
 (0)