Browse Source

feat: 适配器模式,可能不算吧

master
niushuai233 1 year ago
parent
commit
7f4c175c7d
  1. 27
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/ChargeAdapter.java
  2. 29
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/ChargeInterfaceEnum.java
  3. 33
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/IChargeInterface.java
  4. 49
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/LightingChargeInterface.java
  5. 49
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbAChargeInterface.java
  6. 47
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbCChargeInterface.java
  7. 49
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/BrandEnum.java
  8. 61
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/Phone.java
  9. 24
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/package-info.java
  10. 59
      src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PhoneTest.java

27
src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/ChargeAdapter.java

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.constructmodel.adapter;
/**
* 充电器接口
*
* @author niushuai233
* @date 2024/1/5 15:55
* @since 0.0.1
*/
public class ChargeAdapter {
}

29
src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/ChargeInterfaceEnum.java

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface;
/**
* 接口类型
*
* @author niushuai233
* @date 2024/1/5 16:06
* @since 0.0.1
*/
public enum ChargeInterfaceEnum {
USB_A, USB_C, LIGHTING
}

33
src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/IChargeInterface.java

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface;
/**
* 充电接口
*
* @author niushuai233
* @date 2024/1/5 15:59
* @since 0.0.1
*/
public interface IChargeInterface {
boolean supports(ChargeInterfaceEnum _interface);
void pushIn();
void pullOut();
}

49
src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/LightingChargeInterface.java

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface;
import lombok.extern.slf4j.Slf4j;
/**
* 雷电接口
*
* @author niushuai233
* @date 2024/1/5 16:04
* @since 0.0.1
*/
@Slf4j
public class LightingChargeInterface implements IChargeInterface {
private ChargeInterfaceEnum _INTERFACE = ChargeInterfaceEnum.LIGHTING;
@Override
public boolean supports(ChargeInterfaceEnum _interface) {
boolean equals = _INTERFACE.equals(_interface);
log.info("chargeInterface: {} supports {}", _interface, equals);
return equals;
}
@Override
public void pushIn() {
log.info("lighting 插入");
}
@Override
public void pullOut() {
log.info("lighting 拔出");
}
}

49
src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbAChargeInterface.java

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface;
import lombok.extern.slf4j.Slf4j;
/**
* usb a接口
*
* @author niushuai233
* @date 2024/1/5 16:01
* @since 0.0.1
*/
@Slf4j
public class UsbAChargeInterface implements IChargeInterface {
private ChargeInterfaceEnum _INTERFACE = ChargeInterfaceEnum.USB_A;
@Override
public boolean supports(ChargeInterfaceEnum _interface) {
boolean equals = _INTERFACE.equals(_interface);
log.info("期望: {} 实际: {} 可用: {}", _INTERFACE, _interface, equals);
return equals;
}
@Override
public void pushIn() {
log.info("usb a 插入");
}
@Override
public void pullOut() {
log.info("usb a 拔出");
}
}

47
src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbCChargeInterface.java

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface;
import lombok.extern.slf4j.Slf4j;
/**
* usb c接口
*
* @author niushuai233
* @date 2024/1/5 16:02
* @since 0.0.1
*/
@Slf4j
public class UsbCChargeInterface implements IChargeInterface {
private ChargeInterfaceEnum _INTERFACE = ChargeInterfaceEnum.USB_C;
@Override
public boolean supports(ChargeInterfaceEnum _interface) {
boolean equals = _INTERFACE.equals(_interface);
log.info("chargeInterface: {} supports {}", _interface, equals);
return equals;
}
@Override
public void pushIn() {
log.info("usb c 插入");
}
@Override
public void pullOut() {
log.info("usb c 拔出");
}
}

49
src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/BrandEnum.java

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.constructmodel.adapter.phone;
import cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface.ChargeInterfaceEnum;
/**
* 手机品牌以及支持的充电接口
*
* @author niushuai233
* @date 2024/1/5 16:15
* @since 0.0.1
*/
public enum BrandEnum {
OPPO("OPPO", ChargeInterfaceEnum.USB_A),
XIAOMI("XIAOMI", ChargeInterfaceEnum.USB_C),
IPHONE("IPHONE", ChargeInterfaceEnum.LIGHTING);
private String name;
private ChargeInterfaceEnum interfaceEnum;
BrandEnum(String name, ChargeInterfaceEnum interfaceEnum) {
this.name = name;
this.interfaceEnum = interfaceEnum;
}
public String getName() {
return name;
}
public ChargeInterfaceEnum getInterface() {
return interfaceEnum;
}
}

61
src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/Phone.java

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.constructmodel.adapter.phone;
import cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface.IChargeInterface;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
/**
* phone interface
*
* @author niushuai233
* @date 2024/1/5 16:19
* @since 0.0.1
*/
@Slf4j
public class Phone {
private boolean in;
private BrandEnum brand;
private IChargeInterface chargeInterface;
public Phone(BrandEnum brand, IChargeInterface chargeInterface) {
this.brand = brand;
this.chargeInterface = chargeInterface;
}
public void pushIn() {
log.info("{}手机插入充电器 准备充电", brand.getName());
if (!chargeInterface.supports(brand.getInterface())) {
log.warn("接口不匹配, 请检查 需要: {}", brand.getInterface().name());
return;
}
chargeInterface.pushIn();
in = true;
}
public void pullOut() {
if (!in) {
return;
}
chargeInterface.pullOut();
log.info("{}手机拔出充电器 当前电量: {}%", brand.getName(), RandomUtil.randomInt(50, 100));
}
}

24
src/main/java/cc/niushuai/demo/designpattern/constructmodel/package-info.java

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 结构性模式 适配器 桥接 组合 装饰器 外观 代理 享元
*
* @author niushuai233
* @date 2024/1/5 15:54
* @since 0.0.1
*/
package cc.niushuai.demo.designpattern.constructmodel;

59
src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PhoneTest.java

@ -0,0 +1,59 @@ @@ -0,0 +1,59 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.constructmodel.adapter;
import cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface.LightingChargeInterface;
import cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface.UsbAChargeInterface;
import cc.niushuai.demo.designpattern.constructmodel.adapter.chargeinterface.UsbCChargeInterface;
import cc.niushuai.demo.designpattern.constructmodel.adapter.phone.BrandEnum;
import cc.niushuai.demo.designpattern.constructmodel.adapter.phone.Phone;
import cn.hutool.core.thread.ThreadUtil;
import org.junit.jupiter.api.Test;
/**
* phone test
*
* @author niushuai233
* @date 2024/1/5 16:31
* @since 0.0.1
*/
public class PhoneTest {
@Test
public void testPhone() {
Phone xiaomi = new Phone(BrandEnum.XIAOMI, new UsbCChargeInterface());
xiaomi.pushIn();
ThreadUtil.sleep(1000);
xiaomi.pullOut();
System.out.println();
Phone oppo = new Phone(BrandEnum.OPPO, new UsbCChargeInterface());
oppo.pushIn();
ThreadUtil.sleep(1000);
oppo.pullOut();
System.out.println();
Phone iphone = new Phone(BrandEnum.IPHONE, new LightingChargeInterface());
iphone.pushIn();
ThreadUtil.sleep(1000);
iphone.pullOut();
}
}
Loading…
Cancel
Save