提交 b4a84171 编写于 作者: 无难事者若执's avatar 无难事者若执

feature(工厂方法):工厂方法

上级 248c86a0
run = "javac Main.java && java Main"
language = "java"
[debugger]
program = "Main"
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
{ pkgs }: {
deps = [
pkgs.graalvm17-ce
pkgs.maven
];
}
\ No newline at end of file
package com.kongxiang.raindrop.dp.type.create.factory.factorymethod;
public class AProduct implements IProduct{
@Override
public String getName() {
return "A";
}
}
package com.kongxiang.raindrop.dp.type.create.factory.factorymethod;
public class BProduct implements IProduct{
@Override
public String getName() {
return "B";
}
}
package com.kongxiang.raindrop.dp.type.create.factory.factorymethod;
/**
* 工厂类接口
*/
public interface IFactory {
/**
* 期望获取的 产品类,工厂生产对应的类
* @param product 产品类
* @return
*/
IProduct get(Class<? extends IProduct> product) throws InstantiationException, IllegalAccessException;
}
package com.kongxiang.raindrop.dp.type.create.factory.factorymethod;
/**
* 产品类
*/
public interface IProduct {
/**
* 获取产品名称
* @return
*/
String getName();
}
package com.kongxiang.raindrop.dp.type.create.factory.factorymethod;
public class ProductFactory implements IFactory{
@Override
public IProduct get(Class<? extends IProduct> wants) throws InstantiationException, IllegalAccessException {
if (wants!= null){
// 空参构造器
return wants.newInstance();
}
return null;
}
}
package com.kongxiang.raindrop.dp.type.create.factory.simplefactory;
import com.kongxiang.raindrop.dp.type.create.factory.factorymethod.IProduct;
/**
* 简单工厂模式:
* 1. 使用条件: 只需要有一个 工厂,但我们不需要去New出来,通过static就可以使用工厂方法
*
*/
public class SimpleFactory {
public static IProduct getProduct(Class<? extends IProduct> wants) throws InstantiationException, IllegalAccessException {
if (wants!= null){
// 空参构造器
return wants.newInstance();
}
return null;
}
}
@startuml
'https://plantuml.com/class-diagram
title: 工厂方法通用实现
interface CreateFactory as "工厂类" {
factoryMethod():IProduct;
}
interface IProduct as "工厂产品"
class Creator as "工厂实现"
CreateFactory <|-- Creator
Creator .> IProduct
class A
IProduct <|-- A
class B
IProduct <|-- B
@enduml
\ No newline at end of file
@startuml
'https://plantuml.com/class-diagram
class Singleton as "单例类" {
.. 私有构造器..
- construct()
.. static 实例 ..
- {static} INSTANCE : Singleton
.. 静态单例方法 ..
+ {static} getInstance():Singleton
}
Singleton "1" -> "1" Singleton
@enduml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="SonarLintModuleSettings">
<option name="uniqueId" value="88e1d104-c961-43ef-925c-2ef60297718b" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="SonarLintModuleSettings">
<option name="uniqueId" value="963d936e-8979-414a-98ac-724c2c4fc949" />
</component>
</module>
\ No newline at end of file
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>mygroupid</groupId>
<artifactId>myartifactid</artifactId>
<version>0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>de.qaware.maven</groupId>
<artifactId>go-offline-maven-plugin</artifactId>
<version>1.2.5</version>
<configuration>
<dynamicDependencies>
<DynamicDependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.20.1</version>
<classifier></classifier>
<repositoryType>PLUGIN</repositoryType>
</DynamicDependency>
<DynamicDependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.2.1</version>
<classifier>jpa</classifier>
<repositoryType>MAIN</repositoryType>
</DynamicDependency>
</dynamicDependencies>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册