commit deabc84194031b01b3e758e4917f3ffa14bd8fb1 Author: 裴浩宇 <617594538@qq.com> Date: Tue Oct 17 15:47:51 2023 +0800 feat: 工厂方法模式 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("汪汪汪"); + } +}