微信扫一扫打赏支持

北风设计模式课程---13、享元模式

北风设计模式课程---13、享元模式

一、总结

一句话总结:

不仅要通过视频学,还要看别的博客里面的介绍,搜讲解,搜作用,搜实例

 

1、享元模式的核心是什么?

对象插入到一个池子里面去,如果池子里面没有数据就插入,有相同的就直接返回
池子中的每个元素都是键值对,键是数值(比如学号),值是这个对象
import java.util.HashMap;
import java.util.Map;


public class MyCharacterFactory {
    private Map<Character,MyCharacter> pool;
    
    public MyCharacterFactory() {
        pool = new HashMap<Character,MyCharacter>();
    }
    
    public MyCharacter getMyCharacter(Character character) {
        MyCharacter myChar = pool.get(character);
        if(myChar == null) {
            myChar = new MyCharacter(character);
            pool.put(character, myChar);
        }
        return myChar;
    }
}

 

 

2、什么是享元模式?

Flyweight模式
它通过与其他类似对象共享数据来减小内存占用
Flyweight模式也叫享元模式,是构造型模式之
一,它通过与其他类似对象共享数据来减小内存
占用。

 

 

3、享元模式的作用是什么?

它通过与其他类似对象共享数据来减小内存占用

 

4、享元模式的角色和职责?

抽象享元角色:所有具体享元类的父类,规定一些需要实现的公共接口。
具体享元角色:抽象享元角色的具体实现类,并实现了抽象享元角色规定的方法。
享元工厂角色:负责创建和管理享元角色。

 

 

 

 

二、内容在总结中

1、相关知识

 

 

2、代码

抽象享元角色:Person.java

具体享元角色:Teacher.java

享元工厂角色:TeacherFactory.java

客户端调用:MainClass.java

 
 

抽象享元角色:Person.java

package com.ibeifeng.news;

public class Person {
    private String name;
    private int age;
    private String sex;
    
    public Person(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    
    public Person() {
        
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    
}

 

 

具体享元角色:Teacher.java

package com.ibeifeng.news;

public class Teacher extends Person {
    private String number;
    
    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public Teacher(String name, int age, String sex,String number) {
        super(name, age, sex);
        this.number = number;
    }
    
    public Teacher() {
        super();
    }
    
}

 

 

享元工厂角色:TeacherFactory.java

package com.ibeifeng.news;

import java.util.HashMap;
import java.util.Map;

public class TeacherFactory {
    private Map<String,Teacher> pool;
    
    public TeacherFactory() {
        pool = new HashMap<String,Teacher>();
    }
    
    public Teacher getTeacher(String number) {
        Teacher teacher = pool.get(number);
        if(teacher == null) {
            teacher = new Teacher();
            teacher.setNumber(number);
            pool.put(number, teacher);
        }
        return teacher;
    }
}

 

 

客户端调用:MainClass.java
 
package com.ibeifeng.news;

public class MainClass {
    public static void main(String[] args) {
        TeacherFactory factory = new TeacherFactory();
        Teacher teacher1 = factory.getTeacher("0102034");
        Teacher teacher2 = factory.getTeacher("0102035");
        Teacher teacher3 = factory.getTeacher("0102034");
        Teacher teacher4 = factory.getTeacher("0102037");
        
        System.out.println(teacher1.getNumber());
        System.out.println(teacher2.getNumber());
        System.out.println(teacher3.getNumber());
        System.out.println(teacher4.getNumber());
        
        if(teacher1 == teacher3) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}

 

 
 
 
 
 
 
posted @ 2019-06-15 22:08  范仁义  阅读(158)  评论(0编辑  收藏  举报