feat: 工厂方法模式

This commit is contained in:
裴浩宇 2023-10-17 15:47:51 +08:00
commit deabc84194
6 changed files with 130 additions and 0 deletions

41
pom.xml Normal file
View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.pnkx</groupId>
<artifactId>design-patterns</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>design-patterns</name>
<description>design-patterns</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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();
}

View File

@ -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("喵喵喵");
}
}

View File

@ -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("汪汪汪");
}
}