Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b364d0e

Browse files
author
deeper
committed
complete adater pettern
1 parent 4c21681 commit b364d0e

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.ruoxu.pattern.adapter;
2+
3+
public class ChinaStudent {
4+
public void sayCN(String content){
5+
System.out.println(content);
6+
}
7+
}

‎src/com/ruoxu/pattern/adapter/Demo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
*/
2424
public class Demo {
2525
public static void main(String[] args) {
26+
Greet michael_jackson = new TranslatorAdapter(new ChinaStudent());
27+
michael_jackson.say("hello");
28+
2629

2730
}
2831
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ruoxu.pattern.adapter;
2+
3+
/**
4+
* Target(客户所期待的的接口,目标接口)
5+
* @author Limitless
6+
*
7+
*/
8+
public interface Greet { // 打招呼
9+
void say(String content);
10+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.ruoxu.pattern.adapter;
2+
3+
public class TranslatorAdapter implements Greet{
4+
private ChinaStudent chinaStu;
5+
public TranslatorAdapter(ChinaStudent chinaStudent) {
6+
chinaStu = chinaStudent;
7+
}
8+
9+
@Override
10+
public void say(String content) {
11+
// 翻译
12+
String msg_cn = translate(content);
13+
chinaStu.sayCN(msg_cn);
14+
15+
}
16+
17+
private String translate(String content) {
18+
String msg = null;
19+
switch (content) {
20+
case "hello":
21+
msg = "你好";
22+
break;
23+
default:
24+
msg = "不好意思,我没听清他的话!我再问他一遍!";
25+
break;
26+
}
27+
return msg;
28+
}
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /