博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
10享元模式Flyweight
阅读量:6834 次
发布时间:2019-06-26

本文共 4896 字,大约阅读时间需要 16 分钟。

一、什么是享元模式

  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
pool; 7 8 public MyCharacterFactory() { 9 pool = new HashMap
();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.从工厂中取出相应的MyCharacter23         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
pool; 6 7 public TeacherFactory() { 8 pool = new HashMap
(); 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 }

 

转载于:https://www.cnblogs.com/justdoitba/p/9033027.html

你可能感兴趣的文章
SpringMVC + MyBatis整合
查看>>
远程桌面的开启和故障处理
查看>>
Linux 下 /dev/zero 和 /dev/null
查看>>
java 面试
查看>>
VCenter vSphere Client下为虚拟机添加VMTools过程详解
查看>>
No enclosing instance of type错误
查看>>
常用MySQL的命令集锦
查看>>
疗伤之设计模式
查看>>
sparkJavaApi逐个详解
查看>>
在 SQL2005 使用行转列或列转行
查看>>
我的友情链接
查看>>
如何设计Android App测试用例
查看>>
dns服务器在做nslookup测试的时候,出现dns timeout 2 seconds的错误解释
查看>>
定义封装的类类型 笔记
查看>>
行业数据获取
查看>>
SpringMvc+Hibernate+Mysql保存表情字符(昵称)到数据库报错的问题?
查看>>
微软2016校园招聘在线笔试 B Professor Q's Software [ 拓扑图dp ]
查看>>
TinyUI组件开发示例
查看>>
qt添加图标
查看>>
字节流高效缓冲区文件复制
查看>>