• (@WhiteTaken)设计模式学习——享元模式


    继续学习享元模式。。。

    乍一看到享元的名字,一头雾水,学习了以后才觉得,这个名字确实比较适合这个模式。

    享元,即共享对象的意思。

      举个例子,如果制作一个五子棋的游戏,如果每次落子都实例化一个对象的话,那么一个多人在线的游戏,要实例化的对象就无穷无尽,

    为了避免这种情况的发生,我们可以怎么做呢。

      试想一下,如果有个盛放对象的容器,用到什么对象就拿出来,如果容器中没有要用到的对象,那么就创建新的对象,并放入容器中。

    这样资源就会得到充分的利用,也避免频繁的创建对象,造成内存的浪费。

      享元模式,就是做到了对象的重复使用,减小内存占用。

      

    下面直接上代码。依然是Java实现。

    Person基类,定义一些人的基本信息。可以被其他关于人的类继承。

     1 public class Person {
     2     private String name = "";
     3     private int age = 0;
     4     private String sex = "";
     5     
     6     public Person(String name, int age, String sex)
     7     {
     8         this.name = name;
     9         this.age = age;
    10         this.sex = sex;
    11     }
    12     
    13     public Person()
    14     {
    15         
    16     }
    17     
    18     public String getName() {
    19         return name;
    20     }
    21     public void setName(String name) {
    22         this.name = name;
    23     }
    24     public int getAge() {
    25         return age;
    26     }
    27     public void setAge(int age) {
    28         this.age = age;
    29     }
    30     public String getSex() {
    31         return sex;
    32     }
    33     public void setSex(String sex) {
    34         this.sex = sex;
    35     }
    36 }

    Student类,继承了Person基类,拥有自己独立的方法,并加入了字段id。

     1 public class Student extends Person{
     2     private String id = "";
     3     public Student(String name, int age, String sex, String id){
     4         super(name, age, sex);
     5         this.id = id;
     6     }
     7     
     8     public Student() {
     9         super();
    10     }
    11     
    12     public String getId() {
    13         return id;
    14     }
    15     
    16     public void setId(String id) {
    17         this.id = id;
    18     }
    19 
    20     public void studentSpeak()
    21     {
    22         System.out.println("我的学号是:" + id);
    23     }
    24 }

    StudentFactory类,享元模式的核心类,有容器pool,有对象拿出来用,没有对象就创建对象。

     1 import java.util.HashMap;
     2 import java.util.Map;
     3 
     4 public class StudentFactory {
     5     private Map<String, Student> pool;
     6 
     7     public StudentFactory() {
     8         pool = new HashMap<String, Student>();
     9     }
    10     
    11     public Student getStudent(String id){
    12         Student student = pool.get(id);
    13         if(null == student){
    14             student = new Student();
    15             student.setId(id);
    16             pool.put(id, student);
    17         }
    18         return student;
    19     }
    20 }

    MainClass测试主类。

     1 public class MainClass {
     2 
     3     public static void main(String[] args) {
     4         StudentFactory studentFactory = new StudentFactory();
     5         Student student1 = studentFactory.getStudent("10001");
     6         Student student2 = studentFactory.getStudent("10002");
     7         Student student3 = studentFactory.getStudent("10001");
     8         Student student4 = studentFactory.getStudent("10004");
     9         student1.studentSpeak();
    10         student2.studentSpeak();
    11         student3.studentSpeak();
    12         student4.studentSpeak();
    13         if(student1 == student3){
    14             System.out.println("=======同一个对象======");
    15         }else {
    16             System.out.println("--------不同对象-------");
    17         }
    18     }
    19 }

    测试结果。

    由此可见,student1和student3是同一个对象。

    此外还有一个概念需要注意一下,就是人们口中的对象池,对象池是享元模式的一个分支,但是和上边的享元模式还是有区别的。

    区别就在于,对象池中的对象都是相同的,而以上的享元模式的对象是不同的,并用id做了区分。需要好好体会一下。

    希望大家共同进步,每天学到一点,积少成多。

    Sweet Dream!!!

  • 相关阅读:
    政务公开系统专栏首页的跨站攻击漏洞
    Spring+XFire WSSecurity安全认证开发感悟
    Appfuse使用中遇到的问题及解决方案
    How to get the rowid when insert the data to Oracle database
    How to configure CVS in IntelliJ IDEA
    localhost打不开 127.0.0.1可以打开,,,或 hosts 文件不起作用的解决方法
    ASp.net中Froms验证方式
    ASP.NET 页面执行顺序详解
    【转】防止用户通过后退按钮重复提交表单ASP中的response.Buffer,Response.Expires,Response.CacheControl
    页面事件(Init,Load,PreRender)执行顺序__简单总结
  • 原文地址:https://www.cnblogs.com/WhiteTaken/p/7690021.html
Copyright © 2020-2023  润新知