File tree Expand file tree Collapse file tree 11 files changed +32
-18
lines changed Expand file tree Collapse file tree 11 files changed +32
-18
lines changed Original file line number Diff line number Diff line change 11public class Adapter extends Target
22{
3+
4+ // 模板
5+
36 private Adaptee adaptee ;
47
58 public Adapter (Adaptee adaptee )
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11public 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}
Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11public interface Robot
22{
3+ // target
34 public void cry ();
45 public void move ();
56}
Original file line number Diff line number Diff line change 44import java .io .*;
55public 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 ;
Original file line number Diff line number Diff line change 11public class CipherAdapter extends DataOperation
22{
3+
4+ // target DataOperation
5+ // adaptee Caesar
6+
37 private Caesar cipher ;
48
59 public CipherAdapter ()
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments