Skip to content

Commit f196dfb

Browse files
authored
适配器模式
适配器模式
1 parent 39c8c76 commit f196dfb

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.java.design.adapter;
2+
3+
public class Adaptee {
4+
5+
public void adapteeMethod() {
6+
System.out.println("Adaptee Method ...");
7+
}
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.java.design.adapter;
2+
3+
public class Adapter implements Target {
4+
5+
private Adaptee adaptee;
6+
7+
public Adapter(Adaptee adaptee) {
8+
9+
this.adaptee = adaptee;
10+
}
11+
12+
public void adapteeMethod() {
13+
adaptee.adapteeMethod();
14+
};
15+
16+
@Override
17+
public void adapterMethod() {
18+
System.out.println("Adapter Method ...");
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.java.design.adapter;
2+
3+
/**
4+
* 适配器就是一种适配中间件,它存在于不匹配的二者之间,用于连接二者,将不匹配变得匹配,简单点理解就是平常所见的转接头,转换器之类的存在。
5+
*
6+
*   适配器模式有两种:类适配器、对象适配器、接口适配器
7+
*
8+
*   前二者在实现上有些许区别,作用一样,第三个接口适配器差别较大。
9+
*
10+
* @author Administrator
11+
*
12+
*/
13+
public class AdapterPattern {
14+
15+
public static void main(String[] args) {
16+
17+
Target target = new Adapter(new Adaptee());
18+
target.adapteeMethod();
19+
target.adapterMethod();
20+
}
21+
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.java.design.adapter;
2+
3+
public interface Target {
4+
5+
void adapteeMethod();
6+
7+
void adapterMethod();
8+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy