Skip to content

Commit 73507b2

Browse files
committed
update:adpter
1 parent 0ae6229 commit 73507b2

File tree

11 files changed

+32
-18
lines changed

11 files changed

+32
-18
lines changed

Chapter 10 Adapter/Adapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
public class Adapter extends Target
22
{
3+
4+
// 模板
5+
36
private Adaptee adaptee;
47

58
public Adapter(Adaptee adaptee)

Chapter 10 Adapter/sample01/Bird.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ public class Bird
22
{
33
public void tweedle()
44
{
5-
System.out.println("鸟儿叽叽叫!");
5+
System.out.println("鸟儿叽叽叫!");
66
}
77

88
public void fly()
99
{
10-
System.out.println("鸟儿快快飞!");
10+
System.out.println("鸟儿快快飞!");
1111
}
1212
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
public class BirdAdapter extends Bird implements Robot
22
{
3+
4+
// Bird与Robot进行适配
5+
36
public void cry()
47
{
5-
System.out.print("机器人模仿:");
8+
System.out.print("机器人模仿:");
69
super.tweedle();
710
}
811

912
public void move()
1013
{
11-
System.out.print("机器人模仿:");
14+
System.out.print("机器人模仿:");
1215
super.fly();
1316
}
1417
}

Chapter 10 Adapter/sample01/Client.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ public class Client
22
{
33
public static void main(String args[])
44
{
5+
// 类适配器
56
Robot robot=(Robot)XMLUtil.getBean();
67
robot.cry();
78
robot.move();

Chapter 10 Adapter/sample01/Dog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ public class Dog
22
{
33
public void wang()
44
{
5-
System.out.println("¹·ÍôÍô½Ð£¡");
5+
System.out.println("狗汪汪叫!");
66
}
77

88
public void run()
99
{
10-
System.out.println("¹·¿ì¿ìÅÜ£¡");
10+
System.out.println("狗快快跑!");
1111
}
1212
}

Chapter 10 Adapter/sample01/DogAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ public class DogAdapter extends Dog implements Robot
22
{
33
public void cry()
44
{
5-
System.out.print("机器人模仿:");
5+
System.out.print("机器人模仿:");
66
super.wang();
77
}
88

99
public void move()
1010
{
11-
System.out.print("机器人模仿:");
11+
System.out.print("机器人模仿:");
1212
super.run();
1313
}
1414
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
public interface Robot
22
{
3+
// target
34
public void cry();
45
public void move();
56
}

Chapter 10 Adapter/sample01/XMLUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
import java.io.*;
55
public class XMLUtil
66
{
7-
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
7+
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
88
public static Object getBean()
99
{
1010
try
1111
{
12-
//创建文档对象
12+
//创建文档对象
1313
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
1414
DocumentBuilder builder = dFactory.newDocumentBuilder();
1515
Document doc;
1616
doc = builder.parse(new File("config.xml"));
1717

18-
//获取包含类名的文本节点
18+
//获取包含类名的文本节点
1919
NodeList nl = doc.getElementsByTagName("className");
2020
Node classNode=nl.item(0).getFirstChild();
2121
String cName=classNode.getNodeValue();
2222

23-
//通过类名生成实例对象并将其返回
23+
//通过类名生成实例对象并将其返回
2424
Class c=Class.forName(cName);
2525
Object obj=c.newInstance();
2626
return obj;

Chapter 10 Adapter/sample02/CipherAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
public class CipherAdapter extends DataOperation
22
{
3+
4+
// target DataOperation
5+
// adaptee Caesar
6+
37
private Caesar cipher;
48

59
public CipherAdapter()

Chapter 10 Adapter/sample02/Client.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ public class Client
22
{
33
public static void main(String args[])
44
{
5+
6+
// 对象适配器模式
57
DataOperation dao=(DataOperation)XMLUtil.getBean();
68
dao.setPassword("sunnyLiu");
79
String ps=dao.getPassword();
810
String es=dao.doEncrypt(6,ps);
9-
System.out.println("Ã÷ÎÄΪ£º" + ps);
10-
System.out.println("ÃÜÎÄΪ£º" + es);
11+
System.out.println("明文为:" + ps);
12+
System.out.println("密文为:" + es);
1113
}
1214
}
1315

0 commit comments

Comments
 (0)