• 设计模式之享元模式


    1. 享元模式概述

    享元模式(Flyweight Pattern)是一种结构型设计模式,旨在通过共享对象以减少内存使用和提高性能。它主要用于处理大量细粒度对象的场景,通过共享相同或相似对象来减少内存占用。

    • 主要角色:

      1. 享元工厂(Flyweight Factory): 负责创建和管理享元对象。它确保共享对象的正确性和一致性,通常使用工厂方法或者其他创建方法。

      2. 抽象享元(Flyweight): 定义享元对象的接口,描述对象应该具有的方法。通常包括具有内部状态和外部状态的方法,内部状态是可以共享的,外部状态是不可共享的。

      3. 具体享元(Concrete Flyweight): 实现抽象享元接口,存储内部状态,并负责处理外部状态。具体享元对象需要被共享,因此它通常是不可变的。

      4. 非共享具体享元(Unshared Concrete Flyweight): 并非所有享元对象都需要被共享,有时候可以创建不可共享的具体享元对象。

    • 工作原理:

      1. 内部状态和外部状态: 在享元模式中,对象的状态分为内部状态和外部状态。内部状态是存储在享元对象内部并可以被共享的状态,而外部状态是存储在客户端并且不能被共享的状态。

      2. 共享对象: 享元模式通过共享相同的享元对象来减少内存占用。如果一个请求可以被相同或相似对象满足,那么可以返回一个已经存在的享元对象,而不是创建一个新的对象。

      3. 客户端: 客户端负责维护外部状态,并将外部状态传递给享元对象。客户端通常使用享元工厂来获取或创建享元对象。

    • 优缺点:

      • 优点:

        1. 减少内存占用: 通过共享相同的享元对象,减少了大量细粒度对象的内存占用。

        2. 提高性能: 减少了创建和销毁对象的开销,提高了系统的性能。

      • 缺点:

      1. 共享对象可能引入复杂性: 当需要考虑共享对象的线程安全性时,可能引入额外的同步机制,增加了系统的复杂性。

    示例代码:
    以下是一个简单的 Java 示例代码,演示了享元模式的基本结构:

    import java.util.HashMap;
    import java.util.Map;
    
    // 抽象享元接口
    interface Flyweight {
        void operation(String externalState);
    }
    
    // 具体享元类
    class ConcreteFlyweight implements Flyweight {
        private String intrinsicState;
    
        public ConcreteFlyweight(String intrinsicState) {
            this.intrinsicState = intrinsicState;
        }
    
        @Override
        public void operation(String externalState) {
            System.out.println("ConcreteFlyweight: Intrinsic State - " + intrinsicState +
                               ", External State - " + externalState);
        }
    }
    
    // 享元工厂类
    class FlyweightFactory {
        private Map<String, Flyweight> flyweights = new HashMap<>();
    
        public Flyweight getFlyweight(String key) {
            if (!flyweights.containsKey(key)) {
                flyweights.put(key, new ConcreteFlyweight(key));
            }
            return flyweights.get(key);
        }
    }
    
    public class FlyweightPatternExample {
        public static void main(String[] args) {
            FlyweightFactory factory = new FlyweightFactory();
    
            Flyweight flyweight1 = factory.getFlyweight("A");
            flyweight1.operation("1");
    
            Flyweight flyweight2 = factory.getFlyweight("B");
            flyweight2.operation("2");
    
            Flyweight flyweight3 = factory.getFlyweight("A");
            flyweight3.operation("3");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    在这个示例中,ConcreteFlyweight 表示具体的享元对象,FlyweightFactory 是享元工厂,负责创建和管理享元对象。客户端通过工厂获取享元对象,然后调用对象的操作方法。在这个过程中,如果请求的享元对象已经存在,工厂将返回现有的对象,否则创建一个新的对象。

    2. 享元模式模拟实现JDBC连接池

    package com.test;
    
    import java.sql.*;
    import java.util.Map;
    import java.util.Properties;
    import java.util.concurrent.Executor;
    import java.util.concurrent.atomic.AtomicIntegerArray;
    
    /**
     * @author 曹见朋
     * @create 2023-11-11-14:55
     */
    public class MyJDBCPool {
    
        public static void main(String[] args) {
            MyJDBCPool myJDBCPool =new MyJDBCPool(2);
            for (int i = 0; i < 5; i++) {
                new Thread(()->{
                    Connection borrow = myJDBCPool.borrow();
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    myJDBCPool.free(borrow);
                }).start();
            }
        }
    
    
        // 1. 连接池大小
        private final int poolSize;
    
        // 2.连接对象数组
        private Connection[] connection;
    
        // 3. 连接状态 0空闲   1繁忙
        private AtomicIntegerArray states;
    
        // 4. 构造方法
        public MyJDBCPool(int poolSize) {
            this.poolSize = poolSize;
            this.connection=new Connection[poolSize];
            this.states=new AtomicIntegerArray(new int[poolSize]);
            for (int i = 0; i < poolSize; i++) {
                connection[i] = new MockConnection("傻狗"+i);
            }
        }
    
        // 5. 借连接
        public Connection borrow(){
            while(true){
                for (int i = 0; i < poolSize; i++) {
                    //判断是否有空闲连接
                    if (states.get(i) == 0) {
                        if (states.compareAndSet(i,0,1)) {
                            System.out.println(Thread.currentThread().getName()+" 成功获取连接池,可喜可贺!");
                            return connection[i];
                        }
                    }
                }
                // 如果没有空闲连接
                synchronized (this) {
                    try {
                        System.err.println(Thread.currentThread().getName()+" 没有获取连接池,正在等待!");
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        // 6. 归还连接
        public void free(Connection conn){
            for (int i = 0; i < poolSize; i++) {
                if(connection[i] == conn){
                    states.set(i,0);
                    System.out.println(Thread.currentThread().getName()+" 成功归还连接池,有借有还再借不难!");
                    synchronized (this) {
                        this.notifyAll();
                    }
                    break;
                }
            }
        }
    }
    
    class MockConnection implements  Connection{
    
        private String name;
    
        public MockConnection(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "MockConnection{" +
                    "name='" + name + '\'' +
                    '}';
        }
    
        @Override
        public Statement createStatement() throws SQLException {
            return null;
        }
    
        @Override
        public PreparedStatement prepareStatement(String sql) throws SQLException {
            return null;
        }
    
        @Override
        public CallableStatement prepareCall(String sql) throws SQLException {
            return null;
        }
    
        @Override
        public String nativeSQL(String sql) throws SQLException {
            return null;
        }
    
        @Override
        public void setAutoCommit(boolean autoCommit) throws SQLException {
    
        }
    
        @Override
        public boolean getAutoCommit() throws SQLException {
            return false;
        }
    
        @Override
        public void commit() throws SQLException {
    
        }
    
        @Override
        public void rollback() throws SQLException {
    
        }
    
        @Override
        public void close() throws SQLException {
    
        }
    
        @Override
        public boolean isClosed() throws SQLException {
            return false;
        }
    
        @Override
        public DatabaseMetaData getMetaData() throws SQLException {
            return null;
        }
    
        @Override
        public void setReadOnly(boolean readOnly) throws SQLException {
    
        }
    
        @Override
        public boolean isReadOnly() throws SQLException {
            return false;
        }
    
        @Override
        public void setCatalog(String catalog) throws SQLException {
    
        }
    
        @Override
        public String getCatalog() throws SQLException {
            return null;
        }
    
        @Override
        public void setTransactionIsolation(int level) throws SQLException {
    
        }
    
        @Override
        public int getTransactionIsolation() throws SQLException {
            return 0;
        }
    
        @Override
        public SQLWarning getWarnings() throws SQLException {
            return null;
        }
    
        @Override
        public void clearWarnings() throws SQLException {
    
        }
    
        @Override
        public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
            return null;
        }
    
        @Override
        public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
            return null;
        }
    
        @Override
        public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
            return null;
        }
    
        @Override
        public Map<String, Class<?>> getTypeMap() throws SQLException {
            return null;
        }
    
        @Override
        public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
    
        }
    
        @Override
        public void setHoldability(int holdability) throws SQLException {
    
        }
    
        @Override
        public int getHoldability() throws SQLException {
            return 0;
        }
    
        @Override
        public Savepoint setSavepoint() throws SQLException {
            return null;
        }
    
        @Override
        public Savepoint setSavepoint(String name) throws SQLException {
            return null;
        }
    
        @Override
        public void rollback(Savepoint savepoint) throws SQLException {
    
        }
    
        @Override
        public void releaseSavepoint(Savepoint savepoint) throws SQLException {
    
        }
    
        @Override
        public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
            return null;
        }
    
        @Override
        public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
            return null;
        }
    
        @Override
        public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
            return null;
        }
    
        @Override
        public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
            return null;
        }
    
        @Override
        public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
            return null;
        }
    
        @Override
        public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
            return null;
        }
    
        @Override
        public Clob createClob() throws SQLException {
            return null;
        }
    
        @Override
        public Blob createBlob() throws SQLException {
            return null;
        }
    
        @Override
        public NClob createNClob() throws SQLException {
            return null;
        }
    
        @Override
        public SQLXML createSQLXML() throws SQLException {
            return null;
        }
    
        @Override
        public boolean isValid(int timeout) throws SQLException {
            return false;
        }
    
        @Override
        public void setClientInfo(String name, String value) throws SQLClientInfoException {
    
        }
    
        @Override
        public void setClientInfo(Properties properties) throws SQLClientInfoException {
    
        }
    
        @Override
        public String getClientInfo(String name) throws SQLException {
            return null;
        }
    
        @Override
        public Properties getClientInfo() throws SQLException {
            return null;
        }
    
        @Override
        public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
            return null;
        }
    
        @Override
        public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
            return null;
        }
    
        @Override
        public void setSchema(String schema) throws SQLException {
    
        }
    
        @Override
        public String getSchema() throws SQLException {
            return null;
        }
    
        @Override
        public void abort(Executor executor) throws SQLException {
    
        }
    
        @Override
        public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
    
        }
    
        @Override
        public int getNetworkTimeout() throws SQLException {
            return 0;
        }
    
        @Override
        public <T> T unwrap(Class<T> iface) throws SQLException {
            return null;
        }
    
        @Override
        public boolean isWrapperFor(Class<?> iface) throws SQLException {
            return false;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372

    打印结果如下:

    Thread-2 没有获取连接池,正在等待!
    Thread-4 没有获取连接池,正在等待!
    Thread-3 没有获取连接池,正在等待!
    Thread-0 成功获取连接池,可喜可贺!
    Thread-1 成功获取连接池,可喜可贺!
    Thread-0 成功归还连接池,有借有还再借不难!
    Thread-1 成功归还连接池,有借有还再借不难!
    Thread-4 成功获取连接池,可喜可贺!
    Thread-3 成功获取连接池,可喜可贺!
    Thread-2 没有获取连接池,正在等待!
    Thread-2 没有获取连接池,正在等待!
    Thread-4 成功归还连接池,有借有还再借不难!
    Thread-3 成功归还连接池,有借有还再借不难!
    Thread-2 成功获取连接池,可喜可贺!
    Thread-2 成功归还连接池,有借有还再借不难!
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    input输入表头保存excel文件
    【深度学习】yolov5 tag7.0 实例分割 从0到1的体会
    基于FPGA的视频接口之千兆网口(四配置)
    如何将jpg转化为png?
    c++中如何利用VA_LIST 和单体模式,构建自己的log小系统
    C++ Reference: Standard C++ Library reference: C Library: cwctype: wctype
    linux 信号signal
    若依框架解读(微服务版)—— 3.验证码与登录
    【K哥爬虫普法】网盘用的好,“艳照门”跑不了
    JS逆向核心流程
  • 原文地址:https://blog.csdn.net/m0_54187478/article/details/134348327