享元模式:
又称轻量级模式, 是对象池的一种实现。类似于线程池, 线程池可以避免不停的创建和销毁多个对象, 消耗新能。提供了减少对象数量从而改善应用所需的对象结构的方式
特征: 共享细粒度对象, 将多个对同一对象的访问集中起来。属于结构型模式
使用场景:
1、常常应用于系统底层的开发,以便解决系统的性能问题
2、系统有大量相似对象、需要缓冲池的场景
public interface ITicket { void showInfo(String bunk); }
public class TrainTicket implements ITicket{ private String from; private String to; private int price; public TrainTicket(String from, String to) { this.from = from; this.to = to; } @Override public void showInfo(String bunk) { this.price = new Random().nextInt(2000) ; System.out.println(from + "->" + to + ": " + bunk + "价格: " + this.price); } }
public class TicketFactory { private static Map<String, ITicket> pool = new HashMap<>(); public static ITicket queryTicket(String from, String to) { String key = from + "->" + to; if (pool.containsKey(key)) { System.out.println("使用缓存: " + key); return pool.get(key); } System.out.println("首次查询, 创建对象: " + key); ITicket ticket = new TrainTicket(from, to); pool.put(key, ticket); return ticket; } }
public static void main(String[] args) { ITicket ticket = TicketFactory.queryTicket("成都东", "北京西"); ticket.showInfo("商务座"); ticket = TicketFactory.queryTicket("成都东", "北京西"); ticket.showInfo("软卧"); ticket = TicketFactory.queryTicket("成都东", "厦门"); ticket.showInfo("软卧"); }
=====================================================================================================
public class ConnectionPool { private Vector<Connection> pool; private String url = "jdbc:mysql://localhost:3306/test"; private String username = "root"; private String password = "123456"; private String driverClassName = "com.mysql.jdbc.Driver"; private int poolSize = 100; public ConnectionPool() { pool = new Vector<>(poolSize); try { Class.forName(driverClassName); for (int i = 0; i < poolSize; i++) { Connection conn = DriverManager.getConnection(url, username, password); pool.add(conn); } } catch (Exception e) { } } public synchronized Connection getConnection() { if (pool.size() > 0) { Connection conn = pool.get(0); pool.remove(conn); return conn; } return null; } public synchronized void relese(Connection conn) { pool.add(conn); } }
public static void main(String[] args) { ConnectionPool pool = new ConnectionPool(); Connection conn = pool.getConnection(); System.out.println(conn); }
在源码中的应用String、Integer
public static void main(String[] args) { /** * 字符串如果是变量,先开空间,再拼接 * 如果是常量,先拼接,再在常量池里找,如果有就直接返回,否则,就创建 * * String s=new String("string")和String s="string"的区别; * 有,前者创建两个对象(或者一个对象,如果字符串常量池里已经存在,就只创建一个对象),后者创建一个对象 * ==:比较引用类型比较的是地址值是否相同 * equals:比较引用类型默认比较的也是地址值是否相同 * 而string类重写了equals()方法,比较的是内容是否相同 */ String s1 = "hello"; String s2 = "hello"; String s3 = "he" + "llo"; String s4 = "hel" + new String("lo"); String s5 = new String("hello"); String s6 = s5.intern(); String s7 = "h"; String s8 = "ello"; String s9 = s7 + s8; // true System.out.println(s1 == s2); // false;s5是new出来的对象,“==”比较的是地址值 System.out.println(s1 == s5); // true System.out.println(s1 == s3); // false System.out.println(s1 == s4); // false System.out.println(s1 == s5); // false System.out.println(s4 == s5); // true System.out.println(s1 == s6); // false System.out.println(s5 == s6); // false System.out.println(s4 == s6); // false System.out.println(s1 == s9); }
public static void main(String[] args) { Integer a = Integer.valueOf(100); Integer b = 100; Integer c = Integer.valueOf(1000); Integer d = 1000; // true System.out.println("a==b:" + (a == b)); // false System.out.println("c==d:" + (c == d)); }
Long
优点:
1、减少对象的创建, 降低内存中对象的数量, 降低系统的内存, 提高效率
2、减少内存之外的其他资源占用
缺点:
1、关注内、外部状态、关注线程安全问题
2、使系统、程序的逻辑复杂化
来源:oschina
链接:https://my.oschina.net/u/2954646/blog/3190769