适配器模式

This commit is contained in:
2018-10-09 16:29:58 +08:00
parent 86d9409ae2
commit 5b6775bcaa
14 changed files with 233 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
package top.fjy8018.designpattern.pattern.structural.adapter.classadapter;
import lombok.extern.slf4j.Slf4j;
/**
* 适配器模式:
* 类适配器模式
* 被适配对象
*
* @author F嘉阳
* @date 2018-10-09 16:04
*/
@Slf4j
public class Adaptee {
public void adapteeRequest() {
log.info("adapteeRequest...");
}
}

View File

@@ -0,0 +1,15 @@
package top.fjy8018.designpattern.pattern.structural.adapter.classadapter;
/**
* 适配器模式:将一个类的接口转换成客户希望的另一个接口。适配器模式让那些接口不兼容的类可以一起工作
*
* @author F嘉阳
* @date 2018-10-09 16:07
*/
public class Adapter extends Adaptee implements Target {
@Override
public void request() {
// ... 新增业务逻辑
super.adapteeRequest();
}
}

View File

@@ -0,0 +1,18 @@
package top.fjy8018.designpattern.pattern.structural.adapter.classadapter;
import lombok.extern.slf4j.Slf4j;
/**
* 目标类具体实现
*
* @author F嘉阳
* @date 2018-10-09 16:06
*/
@Slf4j
public class ConcreteTarget implements Target {
@Override
public void request() {
log.info("ConcreteTarget...");
}
}

View File

@@ -0,0 +1,11 @@
package top.fjy8018.designpattern.pattern.structural.adapter.classadapter;
/**
* 目标类实现接口
*
* @author F嘉阳
* @date 2018-10-09 16:05
*/
public interface Target {
void request();
}

View File

@@ -0,0 +1,19 @@
package top.fjy8018.designpattern.pattern.structural.adapter.objectadapter;
import lombok.extern.slf4j.Slf4j;
/**
* 适配器模式:
* 类适配器模式
* 被适配对象
*
* @author F嘉阳
* @date 2018-10-09 16:04
*/
@Slf4j
public class Adaptee {
public void adapteeRequest() {
log.info("adapteeRequest...");
}
}

View File

@@ -0,0 +1,21 @@
package top.fjy8018.designpattern.pattern.structural.adapter.objectadapter;
/**
* 适配器模式:将一个类的接口转换成客户希望的另一个接口。适配器模式让那些接口不兼容的类可以一起工作
*
* @author F嘉阳
* @date 2018-10-09 16:07
*/
public class Adapter implements Target {
/**
* 将继承改成组合方式,降低耦合度
*/
private Adaptee adaptee = new Adaptee();
@Override
public void request() {
// ... 新增业务逻辑
adaptee.adapteeRequest();
}
}

View File

@@ -0,0 +1,18 @@
package top.fjy8018.designpattern.pattern.structural.adapter.objectadapter;
import lombok.extern.slf4j.Slf4j;
/**
* 目标类具体实现
*
* @author F嘉阳
* @date 2018-10-09 16:06
*/
@Slf4j
public class ConcreteTarget implements Target {
@Override
public void request() {
log.info("ConcreteTarget...");
}
}

View File

@@ -0,0 +1,11 @@
package top.fjy8018.designpattern.pattern.structural.adapter.objectadapter;
/**
* 目标类实现接口
*
* @author F嘉阳
* @date 2018-10-09 16:05
*/
public interface Target {
void request();
}

View File

@@ -0,0 +1,16 @@
package top.fjy8018.designpattern.pattern.structural.adapter.poweradapter;
import lombok.extern.slf4j.Slf4j;
/**
* @author F嘉阳
* @date 2018-10-09 16:24
*/
@Slf4j
public class AC220 {
public int output() {
int output = 220;
log.info(this.getClass().getSimpleName() + "output:" + output + "V");
return output;
}
}

View File

@@ -0,0 +1,22 @@
package top.fjy8018.designpattern.pattern.structural.adapter.poweradapter;
import lombok.extern.slf4j.Slf4j;
/**
* @author F嘉阳
* @date 2018-10-09 16:25
*/
@Slf4j
public class Adapter implements DC5 {
private AC220 ac220 = new AC220();
@Override
public int output() {
int output220 = ac220.output();
// 变压器模拟
int output5 = output220 / 44;
log.info("{} 输入{}V,输出{}V", this.getClass().getSimpleName(), output220, output5);
return output5;
}
}

View File

@@ -0,0 +1,9 @@
package top.fjy8018.designpattern.pattern.structural.adapter.poweradapter;
/**
* @author F嘉阳
* @date 2018-10-09 16:25
*/
public interface DC5 {
int output();
}

View File

@@ -0,0 +1,22 @@
package top.fjy8018.designpattern.pattern.structural.adapter.classadapter;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
class AdapterTest {
/**
* 通过适配器实现调用新接口将具体实现转发到旧类中实现
*/
@Test
void request() {
Target newTarget = new ConcreteTarget();
newTarget.request();
Target adapter = new Adapter();
adapter.request();
}
}

View File

@@ -0,0 +1,15 @@
package top.fjy8018.designpattern.pattern.structural.adapter.objectadapter;
import org.junit.jupiter.api.Test;
class AdapterTest {
@Test
void request() {
Target newTarget = new ConcreteTarget();
newTarget.request();
Target adapter = new Adapter();
adapter.request();
}
}

View File

@@ -0,0 +1,17 @@
package top.fjy8018.designpattern.pattern.structural.adapter.poweradapter;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class AdapterTest {
@Test
void output() {
AC220 ac220 = new AC220();
ac220.output();
DC5 dc5 = new Adapter();
dc5.output();
}
}