享元模式模式
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.flyweight;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 享元模式:利用共享的方式来支持大量细粒度的对象,这些对象一部分内部状态是相同的。
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-10-14 16:13
|
||||||
|
*/
|
||||||
|
public interface Employee {
|
||||||
|
void report();
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.flyweight;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 员工工厂类
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-10-14 16:15
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class EmployeeFactory {
|
||||||
|
private static final Map<String, Employee> EMPLOYEE_MAP = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public static Employee getEmployee(String department) {
|
||||||
|
Manager manager = (Manager) EMPLOYEE_MAP.get(department);
|
||||||
|
// 如果在对象池中不存在,则新建对象
|
||||||
|
if (manager == null) {
|
||||||
|
manager = new Manager(department);
|
||||||
|
log.warn("创建{}部门经理", department);
|
||||||
|
String content = "报告内容……";
|
||||||
|
log.warn("创建报告,内容为:{}", content);
|
||||||
|
manager.setContent(content);
|
||||||
|
EMPLOYEE_MAP.put(department, manager);
|
||||||
|
}
|
||||||
|
return manager;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.flyweight;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-10-14 16:16
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class Manager implements Employee {
|
||||||
|
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
private String department;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 内部状态
|
||||||
|
*/
|
||||||
|
private String title = "部门经理";
|
||||||
|
|
||||||
|
public Manager(String department) {
|
||||||
|
this.department = department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void report() {
|
||||||
|
log.info("{}{},汇报内容:{}", department, title, content);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.flyweight;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class EmployeeTest {
|
||||||
|
|
||||||
|
private static final String departments[] = {"RD", "QA", "PM", "BD"};
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void report() {
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
String department = departments[(int) (Math.random() * departments.length)];
|
||||||
|
Employee employee = EmployeeFactory.getEmployee(department);
|
||||||
|
employee.report();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user