基本概念:
定义:提供了减少对象数量从而改善应用所需的对象结构的方式。
运用共享技术有效地支持大量细粒度的对象。
适用场景:
常常应用于系统底层的开发,以便解决系统的性能问题。
系统中有大量相似对象,需要缓冲池的场景。
优点:
减少对象的创建,降低内存中对象的数量,降低系统的内存,提高效率。
减少内存之外的其他资源(时间资源,new一个对象也是需要时间的)占用。
补充概念
内部状态:在享元对象的内部,不会随着环境的改变而改变的共享部分。
外部状态:随着环境改变而改变,是不可以共享的状态,这个状态是记录在享元对象的外部的。
经理类:
package com.geely.design.pattern.structural.flyweight;
/**
* Created by geely
*/
public interface Employee {
void report();
}
package com.geely.design.pattern.structural.flyweight;
/**
* Created by geely
*/
public class Manager implements Employee {
@Override
public void report() {
System.out.println(reportContent);
}
private String title = "部门经理";
private String department;
private String reportContent;
public void setReportContent(String reportContent) {
this.reportContent = reportContent;
}
public Manager(String department) {
this.department = department;
}
}
Manager也是一个Employee,所以实现了Employee这个接口。是manager的员工必然有个值为“部门经理”的title,它不随外部而改变。是manager的员工也是属于不同部门的,所以它的department属性随外部而改变。title是内部状态,department数外部状态。
Manager的“享元”类:
package com.geely.design.pattern.structural.flyweight;
import java.util.HashMap;
import java.util.Map;
/**
* Created by geely
*/
public class EmployeeFactory {
private static final Map<String,Employee> EMPLOYEE_MAP = new HashMap<String,Employee>();
public static Employee getManager(String department){
Manager manager = (Manager) EMPLOYEE_MAP.get(department);
if(manager == null){
manager = new Manager(department);
System.out.print("创建部门经理:"+department);
String reportContent = department+"部门汇报:此次报告的主要内容是......";
manager.setReportContent(reportContent);
System.out.println(" 创建报告:"+reportContent);
EMPLOYEE_MAP.put(department,manager);
}
return manager;
}
}
测试类:
package com.geely.design.pattern.structural.flyweight.wbp;
package com.geely.design.pattern.structural.flyweight;
/**
* Created by geely
*/
public class Test {
private static final String departments[] = {"RD","QA","PM","BD"};
public static void main(String[] args) {
for(int i=0; i<10; i++){
String department = departments[(int)(Math.random() * departments.length)];
Manager manager = (Manager) EmployeeFactory.getManager(department);
manager.report();
}
}
}
结果:
图片.png
从结果中可以看出对于已经“创建”过的经理,从map里头取,不再new,节省内存空间。
使用享元模式要关注线程安全问题,但是在这个例子中,尽管线程不安全,但是对于结果和性能没有太大的影响,所以也就不考虑线程安全了。
源码
图片.png测试一下:
package com.geely.design.pattern.structural.flyweight;
/**
* Created by geely
*/
public class Test {
private static final String departments[] = {"RD","QA","PM","BD"};
public static void main(String[] args) {
Integer a = Integer.valueOf(100);
Integer b = 100;
Integer c = Integer.valueOf(1000);
Integer d = 1000;
System.out.println("a==b:"+(a==b));
System.out.println("c==d:"+(c==d));
}
}
结果:
图片.png
网友评论