享元模式

1. 什么是享元模式

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

2. 享元模式的结构

08_享元模式 - 图1

3. 享元模式的角色和职责

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

4. 代码演示

4.1. MyCharacter

  1. public class MyCharacter {
  2. private final char myChar;
  3. public MyCharacter(char myChar) {
  4. this.myChar = myChar;
  5. }
  6. public void display() {
  7. System.out.println(myChar);
  8. }
  9. }
  1. public class MyCharacterFactory {
  2. private final Map<Character, MyCharacter> pool;
  3. public MyCharacterFactory() {
  4. this.pool = new HashMap<>();
  5. }
  6. public MyCharacter getMyCharacter(Character character) {
  7. MyCharacter myChar = this.pool.get(character);
  8. if (myChar == null) {
  9. myChar = new MyCharacter(character);
  10. this.pool.put(character, myChar);
  11. }
  12. return myChar;
  13. }
  14. }
  1. MyCharacter myChar1 = new MyCharacter('a');
  2. MyCharacter myChar2 = new MyCharacter('b');
  3. MyCharacter myChar3 = new MyCharacter('a');
  4. MyCharacter myChar4 = new MyCharacter('d');
  5. myChar1.display();
  6. myChar2.display();
  7. myChar3.display();
  8. myChar4.display();
  9. System.out.println(myChar1 == myChar3);
  1. a
  2. b
  3. a
  4. d
  5. false
  1. MyCharacterFactory factory = new MyCharacterFactory();
  2. MyCharacter myChar1 = factory.getMyCharacter('a');
  3. MyCharacter myChar2 = factory.getMyCharacter('b');
  4. MyCharacter myChar3 = factory.getMyCharacter('a');
  5. MyCharacter myChar4 = factory.getMyCharacter('d');
  6. myChar1.display();
  7. myChar2.display();
  8. myChar3.display();
  9. myChar4.display();
  10. System.out.println(myChar1 == myChar3);
  1. a
  2. b
  3. a
  4. d
  5. true

4.2. Teacher

  1. public class Person {
  2. private String name;
  3. private int age;
  4. private String sex;
  5. public Person() {
  6. }
  7. public Person(String name, int age, String sex) {
  8. this.name = name;
  9. this.age = age;
  10. this.sex = sex;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public int getAge() {
  19. return age;
  20. }
  21. public void setAge(int age) {
  22. this.age = age;
  23. }
  24. public String getSex() {
  25. return sex;
  26. }
  27. public void setSex(String sex) {
  28. this.sex = sex;
  29. }
  30. }
  1. public class Teacher extends Person {
  2. private String number;
  3. public Teacher(String number) {
  4. super();
  5. this.number = number;
  6. }
  7. public Teacher(String name, int age, String sex, String number) {
  8. super(name, age, sex);
  9. this.number = number;
  10. }
  11. public String getNumber() {
  12. return number;
  13. }
  14. public void setNumber(String number) {
  15. this.number = number;
  16. }
  17. @Override
  18. public String toString() {
  19. return "Teacher{" +
  20. "number='" + number + '\'' +
  21. '}';
  22. }
  23. }
  1. public class TeacherFactory {
  2. private final Map<String, Teacher> pool;
  3. public TeacherFactory() {
  4. this.pool = new HashMap<>();
  5. }
  6. public Teacher getTeacher(String number) {
  7. Teacher teacher = this.pool.get(number);
  8. if (teacher == null) {
  9. teacher = new Teacher(number);
  10. this.pool.put(number, teacher);
  11. }
  12. return teacher;
  13. }
  14. }
  1. TeacherFactory factory = new TeacherFactory();
  2. Teacher teacher1 = factory.getTeacher("0102034");
  3. Teacher teacher2 = factory.getTeacher("0102035");
  4. Teacher teacher3 = factory.getTeacher("0102034");
  5. Teacher teacher4 = factory.getTeacher("0102037");
  6. System.out.println(teacher1);
  7. System.out.println(teacher2);
  8. System.out.println(teacher3);
  9. System.out.println(teacher4);
  10. System.out.println(teacher1 == teacher3);
  1. Teacher{number='0102034'}
  2. Teacher{number='0102035'}
  3. Teacher{number='0102034'}
  4. Teacher{number='0102037'}
  5. true