diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/ChargeAdapter.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/ChargeAdapter.java new file mode 100644 index 0000000..7ece758 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/ChargeAdapter.java @@ -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 { +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/ChargeInterfaceEnum.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/ChargeInterfaceEnum.java new file mode 100644 index 0000000..724544e --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/ChargeInterfaceEnum.java @@ -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 +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/IChargeInterface.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/IChargeInterface.java new file mode 100644 index 0000000..ca951bd --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/IChargeInterface.java @@ -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(); +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/LightingChargeInterface.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/LightingChargeInterface.java new file mode 100644 index 0000000..f2334c8 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/LightingChargeInterface.java @@ -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 拔出"); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbAChargeInterface.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbAChargeInterface.java new file mode 100644 index 0000000..847e587 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbAChargeInterface.java @@ -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 拔出"); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbCChargeInterface.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbCChargeInterface.java new file mode 100644 index 0000000..b9e57be --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/chargeinterface/UsbCChargeInterface.java @@ -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 拔出"); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/BrandEnum.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/BrandEnum.java new file mode 100644 index 0000000..6d18912 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/BrandEnum.java @@ -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; + } + +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/Phone.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/Phone.java new file mode 100644 index 0000000..859bf99 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/adapter/phone/Phone.java @@ -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)); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/package-info.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/package-info.java new file mode 100644 index 0000000..e1d94fa --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/package-info.java @@ -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; \ No newline at end of file diff --git a/src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PhoneTest.java b/src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PhoneTest.java new file mode 100644 index 0000000..bc282c9 --- /dev/null +++ b/src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PhoneTest.java @@ -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(); + + } +}