Skip to content

Commit 248ae73

Browse files
committed
add Facade 模式
1 parent 2948e8f commit 248ae73

11 files changed

Lines changed: 136 additions & 16 deletions

File tree

designPattern/.idea/workspace.xml

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

designPattern/maildata.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.leosanqing.facade;
2+
3+
import com.leosanqing.facade.pagemakeer.PageMaker;
4+
5+
/**
6+
* @Author: leosanqing
7+
* @Date: 2019-09-16 22:45
8+
*/
9+
public class Main {
10+
public static void main(String[] args) {
11+
PageMaker.makeWelcomePage("[email protected]","welcome.html");
12+
}
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.leosanqing.facade.pagemakeer;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.util.Properties;
6+
7+
/**
8+
* @Author: leosanqing
9+
* @Date: 2019-09-16 08:18
10+
* <p>
11+
* 从邮箱地址中获取用户名的类,在这个例子中我们只是从一个文本中获取,并不是链接数据库
12+
*/
13+
public class Database {
14+
private Database() {
15+
}
16+
17+
public static Properties getProperties(String dbname) {
18+
String filename = dbname + ".txt";
19+
Properties properties = new Properties();
20+
try {
21+
properties.load(new FileInputStream(filename));
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
}
25+
return properties;
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.leosanqing.facade.pagemakeer;
2+
3+
import java.io.IOException;
4+
import java.io.Writer;
5+
6+
/**
7+
* @Author: leosanqing
8+
* @Date: 2019-09-16 08:25
9+
*/
10+
public class HtmlWriter {
11+
private Writer writer;
12+
13+
public HtmlWriter(Writer writer) {
14+
this.writer = writer;
15+
}
16+
17+
public void title(String title) throws IOException {
18+
writer.write("<html>");
19+
writer.write("<head>");
20+
writer.write("<title>" + title + "</title>");
21+
writer.write("</head>");
22+
writer.write("<body>\n");
23+
writer.write("<h1>" + title + "</h1>\n");
24+
}
25+
26+
public void paragraph(String msg) throws IOException {
27+
writer.write("<p>" + msg + "</p>");
28+
}
29+
30+
public void link(String href, String caption) throws IOException {
31+
paragraph("<a href\"" + href + "\">" + caption + "</a>");
32+
}
33+
public void mailto(String mailaddr,String username) throws IOException{
34+
link("mailto:"+mailaddr,username);
35+
}
36+
37+
public void close() throws IOException{
38+
writer.write("</body>");
39+
writer.write("</html\n>");
40+
writer.close();
41+
}
42+
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.leosanqing.facade.pagemakeer;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
import java.util.Properties;
6+
7+
/**
8+
* @Author: leosanqing
9+
* @Date: 2019-09-16 22:38
10+
*/
11+
public class PageMaker {
12+
private PageMaker() {
13+
}
14+
15+
public static void makeWelcomePage(String mailaddr, String filename) {
16+
try {
17+
Properties properties = Database.getProperties("maildata");
18+
String username = properties.getProperty(mailaddr);
19+
HtmlWriter htmlWriter = new HtmlWriter(new FileWriter(filename));
20+
htmlWriter.title("Welcome to " + username + "'s page");
21+
htmlWriter.paragraph(username + "欢迎来到" + username + "的主页");
22+
htmlWriter.paragraph("等你邮件哦");
23+
24+
htmlWriter.mailto(mailaddr, username);
25+
htmlWriter.close();
26+
System.out.println(filename + " is created for " + mailaddr + " (" + username + " )");
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)