diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/IProduct.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/IProduct.java new file mode 100644 index 0000000..86d3752 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/IProduct.java @@ -0,0 +1,31 @@ +/* + * 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.creatormodel.abstractfactory._01; + +/** + * 电子产品抽象类 + * + * @author niushuai233 + * @date 2023/12/20 15:10 + * @since 0.0.1 + */ +public interface IProduct { + + void start(); + + void shutdown(); +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/IProductFactory.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/IProductFactory.java new file mode 100644 index 0000000..fbb802a --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/IProductFactory.java @@ -0,0 +1,34 @@ +/* + * 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.creatormodel.abstractfactory._01; + +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.IMobileProduct; +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.IPcProduct; + +/** + * 电子产品抽象工厂类 + * + * @author niushuai233 + * @date 2023/12/20 15:10 + * @since 0.0.1 + */ +public interface IProductFactory { + + IMobileProduct producePhone(); + + IPcProduct producePc(); +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/OPPOFactory.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/OPPOFactory.java new file mode 100644 index 0000000..cd00d10 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/OPPOFactory.java @@ -0,0 +1,43 @@ +/* + * 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.creatormodel.abstractfactory._01; + +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.OPPOPhone; +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.OPPOPc; +import lombok.extern.slf4j.Slf4j; + +/** + * oppo factory + * + * @author niushuai233 + * @date 2023/12/20 15:24 + * @since 0.0.1 + */ +@Slf4j +public class OPPOFactory implements IProductFactory { + @Override + public OPPOPhone producePhone() { + log.info("Producing phone by oppo"); + return new OPPOPhone(); + } + + @Override + public OPPOPc producePc() { + log.info("Producing pc by oppo"); + return new OPPOPc(); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/XiaomiFactory.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/XiaomiFactory.java new file mode 100644 index 0000000..a7878e5 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/XiaomiFactory.java @@ -0,0 +1,45 @@ +/* + * 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.creatormodel.abstractfactory._01; + +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.IMobileProduct; +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.XiaomiPhone; +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.IPcProduct; +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.XiaomiPc; +import lombok.extern.slf4j.Slf4j; + +/** + * xiaomi factory + * + * @author niushuai233 + * @date 2023/12/20 15:24 + * @since 0.0.1 + */ +@Slf4j +public class XiaomiFactory implements IProductFactory { + @Override + public IMobileProduct producePhone() { + log.info("Producing phone by xiaomi"); + return new XiaomiPhone(); + } + + @Override + public IPcProduct producePc() { + log.info("Producing pc by xiaomi"); + return new XiaomiPc(); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/IMobileProduct.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/IMobileProduct.java new file mode 100644 index 0000000..1ac392f --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/IMobileProduct.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.creatormodel.abstractfactory._01.mobile; + +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.IProduct; + +/** + * 手机产品类 + * + * @author niushuai233 + * @date 2023/12/20 15:10 + * @since 0.0.1 + */ +public interface IMobileProduct extends IProduct { + + void call(String number); + + void sendSMS(String number, String message); +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/OPPOPhone.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/OPPOPhone.java new file mode 100644 index 0000000..a05c569 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/OPPOPhone.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.creatormodel.abstractfactory._01.mobile; + +import lombok.extern.slf4j.Slf4j; + +/** + * oppo phone + * + * @author niushuai233 + * @date 2023/12/20 15:13 + * @since 0.0.1 + */ +@Slf4j +public class OPPOPhone implements IMobileProduct { + @Override + public void start() { + log.info("oppo phone started"); + } + + @Override + public void shutdown() { + log.info("oppo phone shutdown"); + } + + @Override + public void call(String number) { + log.info("oppo phone call {} say hello", number); + } + + @Override + public void sendSMS(String number, String message) { + log.info("oppo phone send SMS {} to {}", message, number); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/VIVOPhone.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/VIVOPhone.java new file mode 100644 index 0000000..3b1df85 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/VIVOPhone.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.creatormodel.abstractfactory._01.mobile; + +import lombok.extern.slf4j.Slf4j; + +/** + * vivo phone + * + * @author niushuai233 + * @date 2023/12/20 15:13 + * @since 0.0.1 + */ +@Slf4j +public class VIVOPhone implements IMobileProduct { + @Override + public void start() { + log.info("vivo phone started"); + } + + @Override + public void shutdown() { + log.info("vivo phone shutdown"); + } + + @Override + public void call(String number) { + log.info("vivo phone call {} say hello", number); + } + + @Override + public void sendSMS(String number, String message) { + log.info("vivo phone send SMS {} to {}", message, number); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/XiaomiPhone.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/XiaomiPhone.java new file mode 100644 index 0000000..200c154 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/mobile/XiaomiPhone.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.creatormodel.abstractfactory._01.mobile; + +import lombok.extern.slf4j.Slf4j; + +/** + * 小米手机 + * + * @author niushuai233 + * @date 2023/12/20 15:12 + * @since 0.0.1 + */ +@Slf4j +public class XiaomiPhone implements IMobileProduct { + @Override + public void start() { + log.info("xiaomi phone started"); + } + + @Override + public void shutdown() { + log.info("xiaomi phone shutdown"); + } + + @Override + public void call(String number) { + log.info("xiaomi phone call {} say hello", number); + } + + @Override + public void sendSMS(String number, String message) { + log.info("xiaomi phone send SMS {} to {}", message, number); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/pc/IPcProduct.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/pc/IPcProduct.java new file mode 100644 index 0000000..2c4aaa9 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/pc/IPcProduct.java @@ -0,0 +1,31 @@ +/* + * 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.creatormodel.abstractfactory._01.pc; + +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.IProduct; + +/** + * pc interface + * + * @author niushuai233 + * @date 2023/12/20 15:16 + * @since 0.0.1 + */ +public interface IPcProduct extends IProduct { + + void surfing(); +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/pc/OPPOPc.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/pc/OPPOPc.java new file mode 100644 index 0000000..9e0498f --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/pc/OPPOPc.java @@ -0,0 +1,44 @@ +/* + * 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.creatormodel.abstractfactory._01.pc; + +import lombok.extern.slf4j.Slf4j; + +/** + * oppo pc + * + * @author niushuai233 + * @date 2023/12/20 15:19 + * @since 0.0.1 + */ +@Slf4j +public class OPPOPc implements IPcProduct { + @Override + public void start() { + log.info("oppo pc Starting"); + } + + @Override + public void shutdown() { + log.info("oppo pc shutdown"); + } + + @Override + public void surfing() { + log.info("oppo pc surfing on the internet"); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/pc/XiaomiPc.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/pc/XiaomiPc.java new file mode 100644 index 0000000..a750008 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/pc/XiaomiPc.java @@ -0,0 +1,45 @@ +/* + * 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.creatormodel.abstractfactory._01.pc; + +import lombok.extern.slf4j.Slf4j; + +/** + * xiaomi pc + * + * @author niushuai233 + * @date 2023/12/20 15:19 + * @since 0.0.1 + */ +@Slf4j +public class XiaomiPc implements IPcProduct { + + @Override + public void start() { + log.info("xiaomi pc Starting"); + } + + @Override + public void shutdown() { + log.info("xiaomi pc shutdown"); + } + + @Override + public void surfing() { + log.info("xiaomi pc surfing on the internet"); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/package-info.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/package-info.java new file mode 100644 index 0000000..eb77dfe --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/package-info.java @@ -0,0 +1,8 @@ +/** + * 抽象工厂模式 + * + * @author niushuai233 + * @date 2023/12/20 11:41 + * @since 0.0.1 + */ +package cc.niushuai.demo.designpattern.creatormodel.abstractfactory; \ No newline at end of file diff --git a/src/test/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/ProductFactoryTest.java b/src/test/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/ProductFactoryTest.java new file mode 100644 index 0000000..dec5e53 --- /dev/null +++ b/src/test/java/cc/niushuai/demo/designpattern/creatormodel/abstractfactory/_01/ProductFactoryTest.java @@ -0,0 +1,60 @@ +/* + * 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.creatormodel.abstractfactory._01; + +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.mobile.IMobileProduct; +import cc.niushuai.demo.designpattern.creatormodel.abstractfactory._01.pc.IPcProduct; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; + +/** + * product factory + * + * @author niushuai233 + * @date 2023/12/20 15:28 + * @since 0.0.1 + */ +@Slf4j +public class ProductFactoryTest { + + @Test + public void testProduct() { + + log.info("================xiaomi product demo================"); + XiaomiFactory xiaomiFactory = new XiaomiFactory(); + testPhone(xiaomiFactory.producePhone()); + testPc(xiaomiFactory.producePc()); + + log.info("================OPPO product demo================"); + OPPOFactory oppoFactory = new OPPOFactory(); + testPhone(oppoFactory.producePhone()); + testPc(oppoFactory.producePc()); + } + + private void testPc(IPcProduct pc) { + pc.start(); + pc.surfing(); + pc.shutdown(); + } + + private void testPhone(IMobileProduct phone) { + phone.start(); + phone.call("13800138000"); + phone.sendSMS("13800138000", "pls say hello."); + phone.shutdown(); + } +}