享元模式:
享元模式的主要目的是实现对象的共享,即共享池,当系统中对象多的时候可以减少内存的开销,通常与工厂模式一起使用。
FlyWeightFactory负责创建和管理享元单元,当一个客户端请求时,工厂需要检查当前对象池中是否有符合条件的对象,如果有,就返回已经存在的对象,如果没有,则创建一个新对象,FlyWeight是超类。一提到共享池,我们很容易联想到Java里面的JDBC连接池,想想每个连接的特点,我们不难总结出:适用于作共享的一些个对象,他们有一些共有的属性,就拿数据库连接池来说,url、driverClassName、username、password及dbname,这些属性对于每个连接来说都是一样的,所以就适合用享元模式来处理,建一个工厂类,将上述类似属性作为内部数据,其它的作为外部数据,在方法调用时,当做参数传进来,这样就节省了空间,减少了实例的数量。
通过创建数据库连接池的应用实例一:
代码如下:
/**
* 通过连接池的管理,实现了数据库连接的共享,不需要每一次都重新创建连接,节省了数据库重新创建的开销,提升了系统的性能!
*/
public class ConnectionPool {
private Vector<Connection> pool;
/*公有属性*/
private String url = "jdbc:mysql://192.168.163.131:3307/mybatis";
private String username = "root";
private String password = "root";
private String driverClassName = "com.mysql.jdbc.Driver";
private int poolSize = 100;
private static ConnectionPool instance = null;
Connection conn = null;
/*构造方法,做一些初始化工作*/
private ConnectionPool() {
pool = new Vector<Connection>(poolSize);
for (int i = 0; i < poolSize; i++) {
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, username, password);
pool.add(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/* 返回连接到连接池 */
public synchronized void release() {
pool.add(conn);
}
/* 返回连接池中的一个数据库连接 */
public synchronized Connection getConnection() {
if (pool.size() > 0) {
Connection conn = pool.get(0);
pool.remove(conn);
return conn;
} else {
return null;
}
}
}
实例二:
public class MyCharacter {
private char mychar;
public MyCharacter(char mychar) {
this.mychar = mychar;
}
public void display() {
System.out.println(mychar);
}
}
*****************************************************
public class MyCharacterFactory {
private Map<Character,MyCharacter> pool;
public MyCharacterFactory() {
pool = new HashMap<>();
}
public MyCharacter getMyCharacter(Character character) {
MyCharacter myCharacter = pool.get(character);
if(myCharacter == null) {
myCharacter = new MyCharacter(character);
pool.put(character,myCharacter);
}
return myCharacter;
}
}
*****************************************************
public class MainClass {
public static void main(String[] args) {
MyCharacterFactory myCharacterFactory = new MyCharacterFactory();
MyCharacter a = myCharacterFactory.getMyCharacter('a');
MyCharacter b = myCharacterFactory.getMyCharacter('b');
MyCharacter c = myCharacterFactory.getMyCharacter('a');
MyCharacter d = myCharacterFactory.getMyCharacter('d');
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
if(a==c){
System.out.println(true);
}else {
System.out.println(false);
}
}
}
**********************************************************
测试结果:
com.atguigu.flyweight.flyweightDemo1.MyCharacter@135fbaa4
com.atguigu.flyweight.flyweightDemo1.MyCharacter@45ee12a7
com.atguigu.flyweight.flyweightDemo1.MyCharacter@135fbaa4
com.atguigu.flyweight.flyweightDemo1.MyCharacter@330bedb4
true
实例三:
public class Person {
private String name;
private int age;
private String sex;
public Person() {
}
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
}
**************************************************************
public class Teacher extends Person {
private String phoneNumber;
public Teacher() {
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Teacher(String name, int age, String sex,String phoneNumber) {
super(name, age, sex);
this.phoneNumber = phoneNumber;
}
}
**************************************************************
public class TeacherFactory {
private Map<String,Teacher> pool;
public TeacherFactory() {
this.pool = new HashMap<>();
}
public Teacher getTeacher(String number) {
Teacher teacher = pool.get(number);
if(teacher == null) {
teacher = new Teacher();
pool.put(number,teacher);
}
return teacher;
}
}
**************************************************************
public class MainClass {
public static void main(String[] args) {
TeacherFactory factory = new TeacherFactory();
Teacher teacher = factory.getTeacher("1111");
Teacher teacher1 = factory.getTeacher("1111");
Teacher teacher2 = factory.getTeacher("1111");
System.out.println(teacher);
System.out.println(teacher1);
System.out.println(teacher2);
if(teacher==teacher1){
System.out.println(true);
}else {
System.out.println(false);
}
}
}
**************************************************************
测试结果:
com.atguigu.flyweight.flyweightDemo2.Teacher@135fbaa4
com.atguigu.flyweight.flyweightDemo2.Teacher@135fbaa4
com.atguigu.flyweight.flyweightDemo2.Teacher@135fbaa4
true
二、事例2
抽象层
public abstract class WebSite {
// 共享的状态,内部状态,不会随着环境改变而改变
// 网站发布的类型
protected String type;
public WebSite(String type) {
this.type = type;
}
public String getType() {
return type;
}
public WebSite setType(String type) {
this.type = type;
return this;
}
/**
* 抽象方法
* @param user 外部状态
*/
public abstract void user(User user);
}
实际实现层
public class ConcreteWebSite extends WebSite {
public ConcreteWebSite(String type) {
super(type);
}
@Override
public void user(User user) {
System.out.println("网站发布形式为:" + type + "在使用中...使用者是:" + user.getName());
}
}
外部状态
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public User setName(String name) {
this.name = name;
return this;
}
}
工厂
public class WebSiteFactory {
private Map<String, WebSite> pool = new HashMap<>();
public WebSite getWebSiteCategory(String type) {
if (!pool.containsKey(type)) {
pool.put(type,new ConcreteWebSite(type));
}
return pool.get(type);
}
}
输出
public class MainClient {
public static void main(String[] args) {
WebSiteFactory webSiteFactory = new WebSiteFactory();
WebSite category = webSiteFactory.getWebSiteCategory("微博");
category.user(new User("张三"));
category = webSiteFactory.getWebSiteCategory("微信公众号");
category.user(new User("李四"));
}
}
// console
网站发布形式为:微博在使用中...使用者是:张三
网站发布形式为:微信公众号在使用中...使用者是:李四
Process finished with exit code 0
三、事例3
UnsharedConcreteFlyweight 非享元角色,外部状态
/**
* @auhtor
* @create 2023-01-13-16:42
* 非享元角色,外部状态
*/
public class UnsharedConcreteFlyweight {
private String info;
public UnsharedConcreteFlyweight(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public UnsharedConcreteFlyweight setInfo(String info) {
this.info = info;
return this;
}
}
抽象享元角色
/**
* @auhtor
* @create 2023-01-13-16:41
* 抽象享元角色
*/
public interface Flyweight {
void operation(UnsharedConcreteFlyweight state);
}
具体享元角色
/**
* @auhtor
* @create 2023-01-13-16:43
* 具体享元角色
*/
public class ConcreteFlyweight implements Flyweight {
// 内部状态
private String key;
public ConcreteFlyweight(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public ConcreteFlyweight setKey(String key) {
this.key = key;
return this;
}
@Override
public void operation(UnsharedConcreteFlyweight state) {
System.out.print("具体享元" + key + "被调用,");
System.out.println("非享元信息是:" + state.getInfo());
}
}
享元工厂
/**
* @auhtor
* @create 2023-01-13-16:46
* 享元工厂
*/
public class FlyWeightFactory {
private static Map<String, Flyweight> pool = new HashMap<>();
public static Flyweight getFlyWeightCategory(String key) {
Flyweight flyweight = pool.get(key);
if (flyweight == null) {
ConcreteFlyweight flyweight1 = new ConcreteFlyweight(key);
pool.put(key, flyweight1);
return flyweight1;
}
return flyweight;
}
}
输出
public class MainClient {
public static void main(String[] args) {
Flyweight f1 = FlyWeightFactory.getFlyWeightCategory("a");
Flyweight f2 = FlyWeightFactory.getFlyWeightCategory("a");
Flyweight f3 = FlyWeightFactory.getFlyWeightCategory("a");
Flyweight f4 = FlyWeightFactory.getFlyWeightCategory("b");
Flyweight f5 = FlyWeightFactory.getFlyWeightCategory("b");
f1.operation(new UnsharedConcreteFlyweight("第一次调用a"));
f2.operation(new UnsharedConcreteFlyweight("第二次调用a"));
f3.operation(new UnsharedConcreteFlyweight("第三次调用a"));
f4.operation(new UnsharedConcreteFlyweight("第①次调用b"));
f5.operation(new UnsharedConcreteFlyweight("第⑤次调用b"));
}
}
// console
具体享元a被调用,非享元信息是:第一次调用a
具体享元a被调用,非享元信息是:第二次调用a
具体享元a被调用,非享元信息是:第三次调用a
具体享元b被调用,非享元信息是:第①次调用b
具体享元b被调用,非享元信息是:第⑤次调用b
Process finished with exit code 0