1    代码演练

1.1  代码演练1

 

1    代码演练
1.1  代码演练1

需求:

每周由随机部门经历做报告;

 

重点关注:

a    该案例是单例模式和享元模式共同使用

b    外部传入的department是外部状态,如果是Manager内部定义一个属性String类型的ManagerTitile,那么无论外部传过来的部门如何变化,这个ManagerTitile不会变化,这个ManagerTitile 称之为内部状态

 

 

uml类图:

 

员工工厂类:

package com.geely.design.pattern.structural.flyweight;

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

/**
 * 创建员工工厂类,
 * 我第一次做的时候就忘了创建这个类
 */
public class EmployFactory {
    //此处应用了 final修饰 引用成员变量,引用对象的内容可以修改,但是引用地址不可以修改
    private static final Map<String,Employee> EMPLOYEE_MAP = new HashMap<String,Employee>();

    public static Employee getManager(String department){
        String reportContent = "开始演讲报告";

        Employee manager = (Manager) EMPLOYEE_MAP.get(department);
        if(manager==null){
            manager = new Manager(department);
            EMPLOYEE_MAP.put(department,manager);
            System.out.println("创建部门经理"+department);
            reportContent = "开始演讲报告";
            ((Manager) manager).setReportContent(reportContent);
            System.out.println("演讲报告内容为:"+reportContent);
        }else{
            manager = EMPLOYEE_MAP.get(department);
            System.out.println("从池子里取部门经理"+department);
            reportContent = "开始演讲报告";
            ((Manager) manager).setReportContent(reportContent);
            System.out.println("演讲报告内容为:"+reportContent);
        }
        return manager;
    }
}

 

员工接口:

package com.geely.design.pattern.structural.flyweight;

/**
 * 员工接口
 *
 * 管理者和员工类实现该接口
 */
public interface Employee  {
    /**
     * 打印报告
     */
    void report(String department);
}

 

员工类:

package com.geely.design.pattern.structural.flyweight;

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

public class Manager implements Employee {

    //该经理所在部门
   private String department;

    //该经理汇报内容
   private String reportContent;


    public void setReportContent(String reportContent) {
        this.reportContent = reportContent;
    }

    //有参构造方法,输入部门
    public Manager(String department) {
        this.department = department;
    }

    /**
     * 实现report方法
     *
     * 如果池子里能取到 该部门的对象,就取,否则,直接创建一个 该部门的对象,并放到从池子里
     */
    @Override
    public void report(String reportContent){
        this.reportContent = reportContent;
    }
}

 

测试类:

package com.geely.design.pattern.structural.flyweight;

public class Test {
    public static void main(String [] args){
        String [] departmentArray = {"业务部","研发部","管理部"};
        String department = "";
        for (int i = 0;i < 10;i++){
            int departmentNum = (int)(Math.random()*departmentArray.length);
            department =departmentArray[departmentNum];
            Employee manager = EmployFactory.getManager(department);
            manager.report(department);
        }
    }
}

 

打印日志:

"C:\Program Files\Java\jdk1.7.0_79\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=62707:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\design_pattern\target\classes;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\apache\tomcat\tomcat-servlet-api\7.0.64\tomcat-servlet-api-7.0.64.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\session\spring-session-core\2.1.7.RELEASE\spring-session-core-2.1.7.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-webmvc\4.0.3.RELEASE\spring-webmvc-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-beans\4.0.3.RELEASE\spring-beans-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-context\4.0.3.RELEASE\spring-context-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-aop\4.0.3.RELEASE\spring-aop-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-core\4.0.3.RELEASE\spring-core-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-expression\4.0.3.RELEASE\spring-expression-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-web\4.0.3.RELEASE\spring-web-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-oxm\4.0.3.RELEASE\spring-oxm-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-jdbc\4.0.3.RELEASE\spring-jdbc-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-tx\4.0.3.RELEASE\spring-tx-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\springframework\spring-test\4.0.3.RELEASE\spring-test-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\aspectj\aspectjweaver\1.7.3\aspectjweaver-1.7.3.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\mybatis\mybatis-spring\1.3.0\mybatis-spring-1.3.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\aspectj\aspectjrt\1.6.11\aspectjrt-1.6.11.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\codehaus\jackson\jackson-mapper-asl\1.9.12\jackson-mapper-asl-1.9.12.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\codehaus\jackson\jackson-core-asl\1.9.12\jackson-core-asl-1.9.12.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-pool\commons-pool\1.5.4\commons-pool-1.5.4.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\ch\qos\logback\logback-classic\1.1.2\logback-classic-1.1.2.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\slf4j\slf4j-api\1.7.6\slf4j-api-1.7.6.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\ch\qos\logback\logback-core\1.1.2\logback-core-1.1.2.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\google\guava\guava\20.0\guava-20.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\apache\commons\commons-lang3\3.5\commons-lang3-3.5.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\junit\junit\4.12\junit-4.12.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\joda-time\joda-time\2.3\joda-time-2.3.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\hashids\hashids\1.0.1\hashids-1.0.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-net\commons-net\3.1\commons-net-3.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-fileupload\commons-fileupload\1.2.2\commons-fileupload-1.2.2.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-io\commons-io\2.0.1\commons-io-2.0.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\github\pagehelper\pagehelper\4.1.0\pagehelper-4.1.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\github\miemiedev\mybatis-paginator\1.2.17\mybatis-paginator-1.2.17.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\github\jsqlparser\jsqlparser\0.9.4\jsqlparser-0.9.4.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-configuration\commons-configuration\1.10\commons-configuration-1.10.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\google\zxing\core\2.1\core-2.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\google\code\gson\gson\2.3.1\gson-2.3.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\redis\clients\jedis\2.6.0\jedis-2.6.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\apache\commons\commons-pool2\2.0\commons-pool2-2.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\projectlombok\lombok\1.16.18\lombok-1.16.18.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\redisson\redisson\2.9.0\redisson-2.9.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\io\netty\netty-common\4.1.8.Final\netty-common-4.1.8.Final.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\io\netty\netty-codec\4.1.8.Final\netty-codec-4.1.8.Final.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\io\netty\netty-buffer\4.1.8.Final\netty-buffer-4.1.8.Final.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\io\netty\netty-transport\4.1.8.Final\netty-transport-4.1.8.Final.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\io\netty\netty-resolver\4.1.8.Final\netty-resolver-4.1.8.Final.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\io\netty\netty-handler\4.1.8.Final\netty-handler-4.1.8.Final.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\javax\cache\cache-api\1.0.0\cache-api-1.0.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\io\projectreactor\reactor-stream\2.0.8.RELEASE\reactor-stream-2.0.8.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\io\projectreactor\reactor-core\2.0.8.RELEASE\reactor-core-2.0.8.RELEASE.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\reactivestreams\reactive-streams\1.0.0\reactive-streams-1.0.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\fasterxml\jackson\dataformat\jackson-dataformat-yaml\2.6.7\jackson-dataformat-yaml-2.6.7.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\yaml\snakeyaml\1.15\snakeyaml-1.15.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\fasterxml\jackson\core\jackson-core\2.6.7\jackson-core-2.6.7.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\fasterxml\jackson\core\jackson-databind\2.6.7\jackson-databind-2.6.7.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\fasterxml\jackson\dataformat\jackson-dataformat-avro\2.9.0\jackson-dataformat-avro-2.9.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\apache\avro\avro\1.8.1\avro-1.8.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\thoughtworks\paranamer\paranamer\2.7\paranamer-2.7.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\xerial\snappy\snappy-java\1.1.1.3\snappy-java-1.1.1.3.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\apache\commons\commons-compress\1.8.1\commons-compress-1.8.1.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\org\tukaani\xz\1.5\xz-1.5.jar;F:\xiangmu3\Xin\FuQiang\maven\code\repo\com\sun\faces\jsf-api\1.2\jsf-api-1.2.jar" com.geely.design.pattern.structural.flyweight.Test
创建部门经理业务部
演讲报告内容为:开始演讲报告
创建部门经理管理部
演讲报告内容为:开始演讲报告
从池子里取部门经理业务部
演讲报告内容为:开始演讲报告
从池子里取部门经理管理部
演讲报告内容为:开始演讲报告
从池子里取部门经理管理部
演讲报告内容为:开始演讲报告
创建部门经理研发部
演讲报告内容为:开始演讲报告
从池子里取部门经理研发部
演讲报告内容为:开始演讲报告
从池子里取部门经理管理部
演讲报告内容为:开始演讲报告
从池子里取部门经理研发部
演讲报告内容为:开始演讲报告
从池子里取部门经理管理部
演讲报告内容为:开始演讲报告

Process finished with exit code 0

 

posted on 2019-09-14 12:02  菜鸟乙  阅读(158)  评论(0编辑  收藏  举报