From deabc84194031b01b3e758e4917f3ffa14bd8fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A3=B4=E6=B5=A9=E5=AE=87?= <617594538@qq.com> Date: Tue, 17 Oct 2023 15:47:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B7=A5=E5=8E=82=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 41 +++++++++++++++++++ .../DesignPatternsApplication.java | 17 ++++++++ .../factorymethod/AnimalFactory.java | 26 ++++++++++++ .../factorymethod/model/Animal.java | 14 +++++++ .../factorymethod/model/Cat.java | 16 ++++++++ .../factorymethod/model/Dog.java | 16 ++++++++ 6 files changed, 130 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/pnkx/designpatterns/DesignPatternsApplication.java create mode 100644 src/main/java/com/pnkx/designpatterns/factorymethod/AnimalFactory.java create mode 100644 src/main/java/com/pnkx/designpatterns/factorymethod/model/Animal.java create mode 100644 src/main/java/com/pnkx/designpatterns/factorymethod/model/Cat.java create mode 100644 src/main/java/com/pnkx/designpatterns/factorymethod/model/Dog.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6bdb251 --- /dev/null +++ b/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.12.RELEASE + + + com.pnkx + design-patterns + 0.0.1-SNAPSHOT + design-patterns + design-patterns + + 8 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/pnkx/designpatterns/DesignPatternsApplication.java b/src/main/java/com/pnkx/designpatterns/DesignPatternsApplication.java new file mode 100644 index 0000000..81387a2 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/DesignPatternsApplication.java @@ -0,0 +1,17 @@ +package com.pnkx.designpatterns; + +import com.pnkx.designpatterns.factorymethod.AnimalFactory; +import com.pnkx.designpatterns.factorymethod.model.Animal; +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(); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/factorymethod/AnimalFactory.java b/src/main/java/com/pnkx/designpatterns/factorymethod/AnimalFactory.java new file mode 100644 index 0000000..2162a04 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/AnimalFactory.java @@ -0,0 +1,26 @@ +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/model/Animal.java b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Animal.java new file mode 100644 index 0000000..02a609b --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Animal.java @@ -0,0 +1,14 @@ +package com.pnkx.designpatterns.factorymethod.model; + +/** + * Animal + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 13:57 + * @description 动物抽象类 + */ +public abstract class Animal { + // 声音 + public abstract 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 new file mode 100644 index 0000000..4ad4e31 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Cat.java @@ -0,0 +1,16 @@ +package com.pnkx.designpatterns.factorymethod.model; + +/** + * Cat + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 13:57 + * @description 猫咪类 + */ +public class Cat extends 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 new file mode 100644 index 0000000..38b9538 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/factorymethod/model/Dog.java @@ -0,0 +1,16 @@ +package com.pnkx.designpatterns.factorymethod.model; + +/** + * Dog + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/17 13:58 + * @description 描述 + */ +public class Dog extends Animal { + @Override + public void sound() { + System.out.println("汪汪汪"); + } +}