zoukankan      html  css  js  c++  java
  • 第十一讲:享元模式

    享元,享,就是分享,元,就是数据.共享数据减少内存占用.

    Flyweight:轻量级.享元模式的核心就是享元工厂.享元模式在后面Java EE学到的自定义连接池的原理是一样的.

    public class MainClass {
        /**
         * 
         * @param args
         */
         public static void main(String[] args) {
    /*        MyCharacter myChar1 = new MyCharacter('a');
            MyCharacter myChar2 = new MyCharacter('b');
            MyCharacter myChar3 = new MyCharacter('a');
            MyCharacter myChar4 = new MyCharacter('d');
            
            myChar1.display();
            myChar2.display();
            myChar3.display();
            myChar4.display();
            
            if(myChar1==myChar3){//myChar1和myChar3共享的数据相同,但是它们却不是同一个对象.
                System.out.println("true");
            }else{
                System.out.println("false");
            }*/
            //1.创建MyCharacter工厂
             MyCharacterFactory factory = new MyCharacterFactory();
             
             //2.从工厂中取出相应的MyCharacter
             MyCharacter myChar1 = factory.getMyCharacter('a');
             MyCharacter myChar2 = factory.getMyCharacter('b');
             MyCharacter myChar3 = factory.getMyCharacter('a');
             MyCharacter myChar4 = factory.getMyCharacter('d');
             
             myChar1.display();
             myChar2.display();
             myChar3.display();      
             myChar4.display();
             
             if(myChar1 == myChar3){//因为myChar1和myChar3是同一个对象.
                 
                 System.out.println(true);
             }else{
                 System.out.println(false);
             }
             
        }
    }
    public class MyCharacter {
         private char mychar;
    
        public MyCharacter(char mychar) {
            super();
            this.mychar = mychar;
        }
        
        public void display(){
            System.out.println(mychar);
        }
        
         
         
    }
    import java.util.HashMap;
    import java.util.Map;
    
    //享元工厂
    public class MyCharacterFactory {
          private Map<Character,MyCharacter>   pool   ;//Character是泛型,后面是我们自定义的MyCharacter
    
        public MyCharacterFactory() {
            super();
            pool = new HashMap<Character,MyCharacter>();//HashMap是一个键值对,键是一个字符,值是MyCharacter这个自定义对象
            //那么需要传这个键才能找到这个值.
            
        }
        public MyCharacter getMyCharacter(Character character){
            MyCharacter myChar = pool.get(character);
            
            if(myChar == null){//如果pool里面没有键相对应的那个值
                myChar = new MyCharacter(character);
                pool.put(character,myChar);
            }
            
            
            
            return myChar;
            
        }
        
    }

    再做一个应用。

    抽象享元角色Person

    具体享元角色Teacher

    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("0102036");
            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());
            //Teacher1和Teacher3的number相同,它是同一个对象.
            if(teacher1==teacher3){
                System.out.println("true");
            }else{
                System.out.println(false);
            }
        }
    }
    package com.ibeifeng.news;
    
    public class Person {
         private String name;
         private int age;
         private String sex;
        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;
        }
        public Person(String name, int age, String sex) {
            super();
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        public Person() {
            super();
            
        }
         
    }
    package com.ibeifeng.news;
    
    public class Teacher extends Person{
        //private int number;//int的NO比较受限,教师号可能是0开头或者是050几
        private String number;
        
        /* public Teacher(String name, int age, String sex,int number) {
            super(name, age, sex);
            this.number = number;
        }
    */
    
    /*    public int getNumber() {
            return number;
        }
    */
    
        public Teacher(String name, int age, String sex, String number) {
            super(name, age, sex);
            this.number = number;
        }
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        public Teacher() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    
    
    
    /*    public void setNumber(int number) {
            this.number = number;
        }*/
    
    
    
    
    
         
    }
    package com.ibeifeng.news;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class TeacherFactory {
         private Map<String,Teacher> pool;//通过Teacher的number找到这样一个老师,number是String类型的.
    
        public TeacherFactory() {
            super();
             pool = new HashMap<String,Teacher>();
        }
      
      public Teacher getTeacher(String number){
          Teacher teacher = pool.get(number);
          if(teacher==null){//没有teacher的话我们需要构造这样一个teacher
              teacher = new Teacher();//
              teacher.setNumber(number);
              pool.put(number, teacher);
          }
          return teacher;
      }
    
    
    
    
    
    
    
    
    
    
    
    
    
    }

  • 相关阅读:
    SQL Server 2019 安装错误
    SQL Server Cardinality Estimation 简介
    SQL Server 要避免的编程坏习惯
    SQL Server 中SET XACT_ABORT设置的作用
    SQL Server nested transaction try...catch 处理模板
    SVN迁移至gitlab
    安装单机版RabbitMQ
    校验MySQL主从数据并修复
    使用Xtrabackup对数据库进行部分备份恢复
    MySQL之Xtrabackup备份还原与binlog恢复
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/6737491.html
Copyright © 2011-2022 走看看