feat: 工厂模式
This commit is contained in:
parent
deabc84194
commit
208bc840bb
33
.gitignore
vendored
Normal file
33
.gitignore
vendored
Normal file
@ -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/
|
@ -1,17 +1,12 @@
|
|||||||
package com.pnkx.designpatterns;
|
package com.pnkx.designpatterns;
|
||||||
|
|
||||||
import com.pnkx.designpatterns.factorymethod.AnimalFactory;
|
import org.springframework.boot.SpringApplication;
|
||||||
import com.pnkx.designpatterns.factorymethod.model.Animal;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class DesignPatternsApplication {
|
public class DesignPatternsApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// 使用工厂类创建不同的 Animal 对象
|
SpringApplication.run(DesignPatternsApplication.class, args);
|
||||||
Animal dog = AnimalFactory.createAnimal("dog");
|
|
||||||
dog.sound();
|
|
||||||
Animal cat = AnimalFactory.createAnimal("cat");
|
|
||||||
cat.sound();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -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系统启动~");
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -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~");
|
||||||
|
}
|
||||||
|
}
|
@ -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("打开微信~");
|
||||||
|
}
|
||||||
|
}
|
@ -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系统启动~");
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,7 @@ package com.pnkx.designpatterns.factorymethod.model;
|
|||||||
* @date 2023/10/17 13:57
|
* @date 2023/10/17 13:57
|
||||||
* @description 动物抽象类
|
* @description 动物抽象类
|
||||||
*/
|
*/
|
||||||
public abstract class Animal {
|
public interface Animal {
|
||||||
// 声音
|
// 叫
|
||||||
public abstract void sound();
|
public void sound();
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ package com.pnkx.designpatterns.factorymethod.model;
|
|||||||
* @date 2023/10/17 13:57
|
* @date 2023/10/17 13:57
|
||||||
* @description 猫咪类
|
* @description 猫咪类
|
||||||
*/
|
*/
|
||||||
public class Cat extends Animal {
|
public class Cat implements Animal {
|
||||||
@Override
|
@Override
|
||||||
public void sound() {
|
public void sound() {
|
||||||
System.out.println("喵喵喵");
|
System.out.println("喵喵喵");
|
||||||
|
@ -6,9 +6,9 @@ package com.pnkx.designpatterns.factorymethod.model;
|
|||||||
* @author 裴浩宇
|
* @author 裴浩宇
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
* @date 2023/10/17 13:58
|
* @date 2023/10/17 13:58
|
||||||
* @description 描述
|
* @description 小狗类
|
||||||
*/
|
*/
|
||||||
public class Dog extends Animal {
|
public class Dog implements Animal {
|
||||||
@Override
|
@Override
|
||||||
public void sound() {
|
public void sound() {
|
||||||
System.out.println("汪汪汪");
|
System.out.println("汪汪汪");
|
||||||
|
1
src/main/resources/application.properties
Normal file
1
src/main/resources/application.properties
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
Loading…
Reference in New Issue
Block a user