一、什么是享元模式
Flyweight模式也叫享元模式,是构造型模式之 一,它通过与其他类似对象共享数据来减小内存 占用。
二、享元模式的结构
三、享元模式的角色和职责
抽象享元角色: 所有具体享元类的父类,规定一些需要实现的公共接口。
具体享元角色: 抽象享元角色的具体实现类,并实现了抽象享元角色规定的方法。
享元工厂角色: 负责创建和管理享元角色。
没用享元模式前
1 //我的性格 2 public class MyCharacter { 3 private char mychar; 4 5 public MyCharacter(char mychar) { 6 this.mychar = mychar; 7 } 8 9 public void display() { 10 System.out.println(mychar); 11 } 12 }
我的性格的工厂
1 import java.util.HashMap; 2 import java.util.Map; 3 4 //我的性格的工厂 5 public class MyCharacterFactory { 6 private Map<Character,MyCharacter> pool; 7 8 public MyCharacterFactory() { 9 pool = new HashMap<Character,MyCharacter>(); 10 } 11 12 public MyCharacter getMyCharacter(Character character) { 13 MyCharacter myChar = pool.get(character); 14 if(myChar == null) { 15 myChar = new MyCharacter(character); 16 pool.put(character, myChar); 17 } 18 return myChar; 19 } 20 }
测试
1 public class MainClass { 2 3 public static void main(String[] args) { 4 // MyCharacter myChar1 = new MyCharacter('a'); 5 // MyCharacter myChar2 = new MyCharacter('b'); 6 // MyCharacter myChar3 = new MyCharacter('a'); 7 // MyCharacter myChar4 = new MyCharacter('d'); 8 // 9 // myChar1.display(); 10 // myChar2.display(); 11 // myChar3.display(); 12 // myChar4.display(); 13 // 14 // if(myChar1 == myChar3) { 15 // System.out.println("true"); 16 // } else { 17 // System.out.println("false"); 18 // } 19 //1.创建MyCharacter工厂 20 MyCharacterFactory factory = new MyCharacterFactory(); 21 22 //2.从工厂中取出相应的MyCharacter 23 MyCharacter myChar1 = factory.getMyCharacter('a'); 24 MyCharacter myChar2 = factory.getMyCharacter('b'); 25 MyCharacter myChar3 = factory.getMyCharacter('a'); 26 MyCharacter myChar4 = factory.getMyCharacter('d'); 27 28 myChar1.display(); 29 myChar2.display(); 30 myChar3.display(); 31 myChar4.display(); 32 33 if(myChar1 == myChar3) { 34 System.out.println("true"); 35 } else { 36 System.out.println("false"); 37 } 38 } 39 }
===============================================================
人
1 public class Person { 2 private String name; 3 private int age; 4 private String sex; 5 6 public Person(String name, int age, String sex) { 7 super(); 8 this.name = name; 9 this.age = age; 10 this.sex = sex; 11 } 12 13 public Person() { 14 15 } 16 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public int getAge() { 24 return age; 25 } 26 public void setAge(int age) { 27 this.age = age; 28 } 29 public String getSex() { 30 return sex; 31 } 32 public void setSex(String sex) { 33 this.sex = sex; 34 } 35 }
老师
1 public class Teacher extends Person { 2 private String number; 3 4 public String getNumber() { 5 return number; 6 } 7 8 public void setNumber(String number) { 9 this.number = number; 10 } 11 12 public Teacher(String name, int age, String sex,String number) { 13 super(name, age, sex); 14 this.number = number; 15 } 16 17 public Teacher() { 18 super(); 19 } 20 }
老师工厂
1 import java.util.HashMap; 2 import java.util.Map; 3 //老师工厂 4 public class TeacherFactory { 5 private Map<String,Teacher> pool; 6 7 public TeacherFactory() { 8 pool = new HashMap<String,Teacher>(); 9 } 10 11 public Teacher getTeacher(String number) { 12 Teacher teacher = pool.get(number); 13 if(teacher == null) { 14 teacher = new Teacher(); 15 teacher.setNumber(number); 16 pool.put(number, teacher); 17 } 18 return teacher; 19 } 20 }
测试方法
1 public class MainClass { 2 public static void main(String[] args) { 3 TeacherFactory factory = new TeacherFactory(); 4 Teacher teacher1 = factory.getTeacher("0102034"); 5 Teacher teacher2 = factory.getTeacher("0102035"); 6 Teacher teacher3 = factory.getTeacher("0102034"); 7 Teacher teacher4 = factory.getTeacher("0102037"); 8 9 System.out.println(teacher1.getNumber()); 10 System.out.println(teacher2.getNumber()); 11 System.out.println(teacher3.getNumber()); 12 System.out.println(teacher4.getNumber()); 13 14 if(teacher1 == teacher3) { 15 System.out.println("true"); 16 } else { 17 System.out.println("false"); 18 } 19 } 20 }