设计模式学习8 -- Flyweight(享元模式)

1.   Flyweight(享元模式)

1.1. 概述

l         场景:字处理软件,如果以每个文字都作为一个对象,几千个字,对象数就是几千,无疑消耗内存

l         但是我们发现,很多字(内容以及特征)是一样的

l         另一个场景:你要从一个数据库中读取一系列字符串,这些字符串中有许多是重复的。

l         可以将这些字符串存储在池(pool)中。

1.2. 解决方法:使用享元模式

import java.util.*;

 

class Word{//编写享元类

       public String content;

       public String key;

       public Word(String content,String key){

              System.out.println("构造函数");

              this.content = content;

              this.key = key;

              }

       }

 

//管理享元,用池,可以将享元用HashMap存储

class WordPool{

       private static HashMap pool = new HashMap();

       public static Word getWord(String key,String content){

              Word word = (Word)pool.get(key);            

              if(word==null){

                     word = new Word(content,key);

                     pool.put(key,word);

                     }

              return word;

              }

       }

 

public class Flyweight1{

       public static void main(String args[]){

              Word w1 = WordPool.getWord("刘德华","001");

              Word w2 = WordPool.getWord("张学友","002");

              Word w3 = WordPool.getWord("刘德华","001");

              Word w4 = WordPool.getWord("刘德华","001");

             

              }

       }

 

1.3. 享元模式要点

l         编写享元类

l         编写一个工厂,在工厂里面定义hashMap类型的享元池,用get函数进行享元的生成

l         例子:Frame上文本框输入一个字符串,回车,出现以这个字符串为标题的Dialog,但是,当你输入的一个字符串如果是以前出现过的,那么就不要新分配内存。

l         以下是从数据库中读入的XML格式的cd数据,要进行处理,你有何高见?

<?xml version=”1.0”?>

<collection>

<cd>

     <title>冰雨</title>  <artist>刘德华</artist>

</cd>

<cd>

     <title>吻别</title>  <artist>张学友</artist>

</cd>

<cd>

     <title>忘情水</title>  <artist>刘德华</artist>

</cd>

 

</collection>

 

1.4. 享元模式小结

l         享元模式的目的:避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类)对象。

l         Flyweight模式是一个提高程序效率和性能的模式,会大大加快程序的运行速度。

l         条件:享元多而小,并且会在以后经常使用,但是无法估计使用哪一个。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值