diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/src/main/java/com/pnkx/designpatterns/DesignPatternsApplication.java b/src/main/java/com/pnkx/designpatterns/DesignPatternsApplication.java index 81387a2..a575d60 100644 --- a/src/main/java/com/pnkx/designpatterns/DesignPatternsApplication.java +++ b/src/main/java/com/pnkx/designpatterns/DesignPatternsApplication.java @@ -1,17 +1,12 @@ package com.pnkx.designpatterns; -import com.pnkx.designpatterns.factorymethod.AnimalFactory; -import com.pnkx.designpatterns.factorymethod.model.Animal; +import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DesignPatternsApplication { public static void main(String[] args) { - // 使用工厂类创建不同的 Animal 对象 - Animal dog = AnimalFactory.createAnimal("dog"); - dog.sound(); - Animal cat = AnimalFactory.createAnimal("cat"); - cat.sound(); + SpringApplication.run(DesignPatternsApplication.class, args); } } diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/Main.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/Main.java new file mode 100644 index 0000000..69fc414 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/Main.java @@ -0,0 +1,44 @@ +package com.pnkx.designpatterns.abstractfactory; + +import com.pnkx.designpatterns.abstractfactory.factory.LinuxFactory; +import com.pnkx.designpatterns.abstractfactory.factory.SoftwareFactory; +import com.pnkx.designpatterns.abstractfactory.factory.WindowsFactory; +import com.pnkx.designpatterns.abstractfactory.model.Application; +import com.pnkx.designpatterns.abstractfactory.model.OperatingSystem; +import com.pnkx.designpatterns.factorymethod.factory.AnimalFactory; +import com.pnkx.designpatterns.factorymethod.factory.CatFactory; +import com.pnkx.designpatterns.factorymethod.factory.DogFactory; +import com.pnkx.designpatterns.factorymethod.model.Animal; + +/** + * Main + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:52 + * @description 抽象工厂模式客户端 + */ +public class Main { + public static void main(String[] args) { + // 初始化Windows工厂 + SoftwareFactory windowsFactory = new WindowsFactory(); + // 初始化操作系统 + OperatingSystem windowsOS = windowsFactory.createOperatingSystem(); + // 启动 + windowsOS.run(); + // 初始化应用程序 + Application wechat = windowsFactory.openApplication(); + // 打开 + wechat.open(); + // 初始化Linux操作系统 + SoftwareFactory linuxFactory = new LinuxFactory(); + // 初始化操作系统 + OperatingSystem linuxOS = linuxFactory.createOperatingSystem(); + // 启动 + linuxOS.run(); + // 初始化应用程序 + Application vim = linuxFactory.openApplication(); + // 打开 + vim.open(); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/factory/LinuxFactory.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/factory/LinuxFactory.java new file mode 100644 index 0000000..f677f8a --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/factory/LinuxFactory.java @@ -0,0 +1,25 @@ +package com.pnkx.designpatterns.abstractfactory.factory; + +import com.pnkx.designpatterns.abstractfactory.model.Application; +import com.pnkx.designpatterns.abstractfactory.model.LinuxOS; +import com.pnkx.designpatterns.abstractfactory.model.OperatingSystem; +import com.pnkx.designpatterns.abstractfactory.model.Vim; + +/** + * LinuxFactory + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:50 + * @description 具体工厂:Linux工厂 + */ +public class LinuxFactory implements SoftwareFactory { + @Override + public OperatingSystem createOperatingSystem() { + return new LinuxOS(); + } + @Override + public Application openApplication() { + return new Vim(); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/factory/SoftwareFactory.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/factory/SoftwareFactory.java new file mode 100644 index 0000000..4aa95ea --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/factory/SoftwareFactory.java @@ -0,0 +1,26 @@ +package com.pnkx.designpatterns.abstractfactory.factory; + +import com.pnkx.designpatterns.abstractfactory.model.Application; +import com.pnkx.designpatterns.abstractfactory.model.OperatingSystem; + +/** + * SoftwareFactory + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:47 + * @description 抽象工厂接口 + */ +public interface SoftwareFactory { + /** + * 创建操作系统 + * @return 操作系统 + */ + OperatingSystem createOperatingSystem(); + + /** + * 打开程序 + * @return 程序 + */ + Application openApplication(); +} diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/factory/WindowsFactory.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/factory/WindowsFactory.java new file mode 100644 index 0000000..840eb08 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/factory/WindowsFactory.java @@ -0,0 +1,26 @@ +package com.pnkx.designpatterns.abstractfactory.factory; + +import com.pnkx.designpatterns.abstractfactory.model.Application; +import com.pnkx.designpatterns.abstractfactory.model.OperatingSystem; +import com.pnkx.designpatterns.abstractfactory.model.WeChat; +import com.pnkx.designpatterns.abstractfactory.model.WindowsOS; + +/** + * WindowsFactory + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:49 + * @description 具体工厂:Windows工厂 + */ +public class WindowsFactory implements SoftwareFactory { + @Override + public OperatingSystem createOperatingSystem() { + return new WindowsOS(); + } + + @Override + public Application openApplication() { + return new WeChat(); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/model/Application.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/Application.java new file mode 100644 index 0000000..83dd1f6 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/Application.java @@ -0,0 +1,16 @@ +package com.pnkx.designpatterns.abstractfactory.model; + +/** + * Application + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:41 + * @description 抽象产品接口:应用程序 + */ +public interface Application { + /** + * 打开 + */ + void open(); +} diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/model/LinuxOS.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/LinuxOS.java new file mode 100644 index 0000000..4facc5c --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/LinuxOS.java @@ -0,0 +1,16 @@ +package com.pnkx.designpatterns.abstractfactory.model; + +/** + * LinuxOS + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:40 + * @description 具体产品:Linux操作系统 + */ +public class LinuxOS implements OperatingSystem { + @Override + public void run() { + System.out.println("Linux系统启动~"); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/model/OperatingSystem.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/OperatingSystem.java new file mode 100644 index 0000000..5490c52 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/OperatingSystem.java @@ -0,0 +1,16 @@ +package com.pnkx.designpatterns.abstractfactory.model; + +/** + * OperatingSystem + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:39 + * @description 抽象产品接口:操作系统 + */ +public interface OperatingSystem { + /** + * 启动 + */ + void run(); +} diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/model/Vim.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/Vim.java new file mode 100644 index 0000000..a67ae59 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/Vim.java @@ -0,0 +1,16 @@ +package com.pnkx.designpatterns.abstractfactory.model; + +/** + * Vim + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:45 + * @description 具体产品:Vim + */ +public class Vim implements Application { + @Override + public void open() { + System.out.println("打开Vim~"); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/model/WeChat.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/WeChat.java new file mode 100644 index 0000000..c531d05 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/WeChat.java @@ -0,0 +1,16 @@ +package com.pnkx.designpatterns.abstractfactory.model; + +/** + * WeChat + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:44 + * @description 具体产品:微信 + */ +public class WeChat implements Application { + @Override + public void open() { + System.out.println("打开微信~"); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/abstractfactory/model/WindowsOS.java b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/WindowsOS.java new file mode 100644 index 0000000..b3048e4 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/abstractfactory/model/WindowsOS.java @@ -0,0 +1,17 @@ +package com.pnkx.designpatterns.abstractfactory.model; + +/** + * WindowsOS + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:39 + * @description 具体产品:Window操作系统 + */ +public class WindowsOS implements OperatingSystem { + + @Override + public void run() { + System.out.println("Windows系统启动~"); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/factorymethod/AnimalFactory.java b/src/main/java/com/pnkx/designpatterns/factorymethod/AnimalFactory.java deleted file mode 100644 index 2162a04..0000000 --- a/src/main/java/com/pnkx/designpatterns/factorymethod/AnimalFactory.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.pnkx.designpatterns.factorymethod; - -import com.pnkx.designpatterns.factorymethod.model.Animal; -import com.pnkx.designpatterns.factorymethod.model.Cat; -import com.pnkx.designpatterns.factorymethod.model.Dog; - -/** - * AnimalFactory - * - * @author 裴浩宇 - * @version 1.0 - * @date 2023/10/17 13:58 - * @description 动物工厂类 - */ -public class AnimalFactory { - // 根据传入的参数创建具体的动物类对象 - public static Animal createAnimal(String type) { - if (type.equalsIgnoreCase("dog")) { - return new Dog(); - } else if (type.equalsIgnoreCase("cat")) { - return new Cat(); - } else { - throw new IllegalArgumentException("Invalid animal type: " + type); - } - } -} diff --git a/src/main/java/com/pnkx/designpatterns/factorymethod/Main.java b/src/main/java/com/pnkx/designpatterns/factorymethod/Main.java new file mode 100644 index 0000000..5e44a07 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/Main.java @@ -0,0 +1,27 @@ +package com.pnkx.designpatterns.factorymethod; + +import com.pnkx.designpatterns.factorymethod.factory.AnimalFactory; +import com.pnkx.designpatterns.factorymethod.factory.CatFactory; +import com.pnkx.designpatterns.factorymethod.factory.DogFactory; +import com.pnkx.designpatterns.factorymethod.model.Animal; + +/** + * Main + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 17:52 + * @description 工厂方法模式客户端 + */ +public class Main { + public static void main(String[] args) { + // 初始化猫咪工厂 + AnimalFactory catFactory = new CatFactory(); + Animal cat = catFactory.createAnimal(); + cat.sound(); + // 初始化小狗工厂 + AnimalFactory dogFactory = new DogFactory(); + Animal dog = dogFactory.createAnimal(); + dog.sound(); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/factorymethod/factory/AnimalFactory.java b/src/main/java/com/pnkx/designpatterns/factorymethod/factory/AnimalFactory.java new file mode 100644 index 0000000..3765c74 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/factory/AnimalFactory.java @@ -0,0 +1,16 @@ +package com.pnkx.designpatterns.factorymethod.factory; + +import com.pnkx.designpatterns.factorymethod.model.Animal; + +/** + * AnimalFactory + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 13:58 + * @description 动物工厂抽象类 + */ +public abstract class AnimalFactory { + // 创建动物类对象 + public abstract Animal createAnimal(); +} diff --git a/src/main/java/com/pnkx/designpatterns/factorymethod/factory/CatFactory.java b/src/main/java/com/pnkx/designpatterns/factorymethod/factory/CatFactory.java new file mode 100644 index 0000000..807fe17 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/factory/CatFactory.java @@ -0,0 +1,19 @@ +package com.pnkx.designpatterns.factorymethod.factory; + +import com.pnkx.designpatterns.factorymethod.model.Animal; +import com.pnkx.designpatterns.factorymethod.model.Cat; + +/** + * CatFactory + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 16:22 + * @description 猫咪工厂 + */ +public class CatFactory extends AnimalFactory { + @Override + public Animal createAnimal() { + return new Cat(); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/factorymethod/factory/DogFactory.java b/src/main/java/com/pnkx/designpatterns/factorymethod/factory/DogFactory.java new file mode 100644 index 0000000..f086e8c --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/factory/DogFactory.java @@ -0,0 +1,19 @@ +package com.pnkx.designpatterns.factorymethod.factory; + +import com.pnkx.designpatterns.factorymethod.model.Animal; +import com.pnkx.designpatterns.factorymethod.model.Dog; + +/** + * DogFactory + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 16:23 + * @description 小狗工厂 + */ +public class DogFactory extends AnimalFactory{ + @Override + public Animal createAnimal() { + return new Dog(); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/factorymethod/model/Animal.java b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Animal.java index 02a609b..e5399d1 100644 --- a/src/main/java/com/pnkx/designpatterns/factorymethod/model/Animal.java +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Animal.java @@ -8,7 +8,7 @@ package com.pnkx.designpatterns.factorymethod.model; * @date 2023/10/17 13:57 * @description 动物抽象类 */ -public abstract class Animal { - // 声音 - public abstract void sound(); +public interface Animal { + // 叫 + public void sound(); } diff --git a/src/main/java/com/pnkx/designpatterns/factorymethod/model/Cat.java b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Cat.java index 4ad4e31..6ec511b 100644 --- a/src/main/java/com/pnkx/designpatterns/factorymethod/model/Cat.java +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Cat.java @@ -8,7 +8,7 @@ package com.pnkx.designpatterns.factorymethod.model; * @date 2023/10/17 13:57 * @description 猫咪类 */ -public class Cat extends Animal { +public class Cat implements Animal { @Override public void sound() { System.out.println("喵喵喵"); diff --git a/src/main/java/com/pnkx/designpatterns/factorymethod/model/Dog.java b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Dog.java index 38b9538..d1014e7 100644 --- a/src/main/java/com/pnkx/designpatterns/factorymethod/model/Dog.java +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Dog.java @@ -6,9 +6,9 @@ package com.pnkx.designpatterns.factorymethod.model; * @author 裴浩宇 * @version 1.0 * @date 2023/10/17 13:58 - * @description 描述 + * @description 小狗类 */ -public class Dog extends Animal { +public class Dog implements Animal { @Override public void sound() { System.out.println("汪汪汪"); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +