博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式之享元模式
阅读量:3913 次
发布时间:2019-05-23

本文共 3896 字,大约阅读时间需要 12 分钟。

定义:享元模式(Flyweight Pattern)又称为轻量级模式,是对象池的一种实现.类似于线程池,线程池可以避免不停的创建和销毁多个对象,消耗性能.提供了减少对象数量从而改善应用所需的对象结构的方式

宗旨:共享细粒度对象,将多个对同一对象的访问集中起来

属于结构型模式

适用场景

常常应用于系统低层的开发,以便解决系统的性能问题
系统有大量相似对象,需要缓冲池的场景

享元模式扩展

内部状态 :不可改变的,不随环境的变化而变化
外部状态 :可以改变的

优点

减少对象的创建,降低内存中对象的数量,降低系统的内存,提高效率
减少内存之外的其他资源占用

缺点

关注内,外部状态,关注线程安全问题
使系统,程序的逻辑复杂化

例子

一般用法

key做为一个类别,在多个对象中抽出来相似的做为一个类别,当参数进行传递

public interface IFlyweight {
void operation(String extrinsicState);}
public class ConcreteFlyweight implements IFlyweight{
private String intrinsicState; public ConcreteFlyweight(String intrinsicState){
this.intrinsicState = intrinsicState; } @Override public void operation(String extrinsicState) {
System.out.println("Object address: " + System.identityHashCode(this)); System.out.println("IntrinsicState: " + this.intrinsicState); System.out.println("ExtrinsicState: " + extrinsicState); }}
public class FlyweightFactory {
private static Map
pool = new HashMap<>(); public static IFlyweight getFlyweight(String intrinsicState){
if(!pool.containsKey(intrinsicState)){
IFlyweight flyweight = new ConcreteFlyweight(intrinsicState); pool.put(intrinsicState,flyweight); } return pool.get(intrinsicState); }}
public class Test {
public static void main(String[] args) {
String[] str = {
"student","teacher","police"}; for(int i = 0; i < 100;i ++) {
IFlyweight iFlyweight = FlyweightFactory.getFlyweight(str[i%3]); iFlyweight.operation("你在干啥"); } }}
数据库连接池
public class ConnectionPool {
private Vector
pool; private int poolSize = 100; private String url = "jdbc:mysql://localhost:3306/test"; private String username = "root"; private String password = "root"; private String driverClassName = "com.mysql.jdbc.Driver"; public ConnectionPool(){
pool = new Vector
(poolSize); try{
Class.forName(driverClassName); for(int i = 0; i < poolSize; i ++){
Connection connection = DriverManager.getConnection(url, username, password); pool.add(connection); } }catch (Exception e){
e.printStackTrace(); } } public synchronized Connection getConnection(){
if(pool.size()>0){
Connection conn = pool.get(0); pool.remove(conn); return conn; } return null; } public synchronized void release(Connection conn){
pool.add(conn); }}
public class Test {
public static void main(String[] args) {
ConnectionPool pool = new ConnectionPool(); Connection connection = pool.getConnection(); System.out.println(connection); }}
jdk中享元模式使用

String 中常量池

public class StringTest {
public static void main(String[] args) {
String s1 = "hello"; String s2 = "hello"; System.out.println(s1 == s2);//true String s3 = "he" + "llo"; System.out.println(s1 == s3);//true String s4 = "he" + new String("llo"); System.out.println(s1 == s4);//false String s5 = new String("hello"); System.out.println(s1 == s5);//false String s6 = s5.intern(); System.out.println(s1 == s6);//true String s7 = "h"; String s8 = "ello"; String s9 = s7 + s8; System.out.println(s1 == s9);//false String s10 = s9.intern(); System.out.println(s1 == s10); }}

Interger 中缓存-128 到127的缓存池

public class IntegerTest {
public static void main(String[] args) {
Integer a = Integer.valueOf(100); Integer b = 100; System.out.println(a == b);//true Integer c = Integer.valueOf(1000); Integer d = 1000; System.out.println(c == d);//false }}

转载地址:http://ikjrn.baihongyu.com/

你可能感兴趣的文章
一套标准的ASP.NET Core容器化应用日志收集分析方案
查看>>
如何使用 C# 扩展方法
查看>>
C#如何回到主线程,如何在委托指定线程执行
查看>>
服务器重新部署踩坑记
查看>>
.NET应用程序安全操作概述
查看>>
C# WPF:把文件给我拖进来!!!
查看>>
.NET5发布了,腾讯招聘点名要求精通MySQL,而不是SQLServer!
查看>>
让 CefSharp.WinForms 应用程序同时支持32位(x86)和64位(x64)的解决方案
查看>>
Docker Vs Podman
查看>>
程序员过关斩将--论系统设计的高可扩展性
查看>>
如何在 Asp.Net Core MVC 中处理 null 值
查看>>
浅谈AsyncLocal,我们应该知道的那些事儿
查看>>
移动建模平台元数据存储架构演进
查看>>
Visual Studio 即时窗口实用技巧
查看>>
如何在 C# 中使用 Dapper ORM
查看>>
AgileConfig-轻量级配置中心 1.1.0 发布,支持应用间配置继承
查看>>
C# :异步编程的注意点
查看>>
Dotnet Core下的Channel, 你用了吗?
查看>>
ASP.NET Core 5.0新增功能摘要
查看>>
回顾 | 进击吧! Blazor!系列
查看>>