设计模式(10)--蝇量模式

  • 蝇量模式(轻量级模式)也叫享元模式(共用一个方法)

  • 景观设计软件项目遇到的问题:

    • 设置一个公园或者小区,会有树的大小,外观等等

    • 虽然对象不复杂,但是如果量大就会消耗很多内存,比如10000000棵树

  • 代码分析:

package yingliangmoshi.old;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:07
*/
public class Tree {
    private int xCoord, yCoord, age;
    public Tree(int xCoord, int yCoord, int age){
        this.xCoord = xCoord;
        this.yCoord = yCoord;
        this.age = age;
    }
    public void display(){
//        System.out.println(xCoord);
    }
}

package yingliangmoshi.old;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:09
*/
public class TreesTest {
    private int length = 1000000;
    private Tree[] treeList = new Tree[length];
    public TreesTest(){
        for(int i = 0;i<length;i++){
            treeList[i] = new Tree((int)(Math.random() * length), (int)(Math.random() * length), (int)(Math.random() * length)%5);
        }
    }


    public void display() {
        for(int i =  0; i<treeList.length;i++){
            treeList[i].display();
        }
    }
}
package yingliangmoshi.old;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:12
*/
public class MainTest {
    public static void main(String[] args) {
        showInfo();
        TreesTest treesTest;
        treesTest = new TreesTest();
        showInfo();
        treesTest.display();
        showInfo();


    }
    public static void showInfo(){
        long max = Runtime.getRuntime().maxMemory();
        long total = Runtime.getRuntime().totalMemory();


        long free  = Runtime.getRuntime().totalMemory();


        long used = total - free;


        System.out.println("最大内存= "+max);
        System.out.println("已分配内存= "+ total);
        System.out.println("已分配剩余内存内存= "+free);
        System.out.println("已用内存= "+used);
        System.out.println("当前时间="+System.currentTimeMillis());
        System.out.println();




    }
}
  • 这些树之间有什么关系,大量的对象导致内存泄漏,如何将对象变少:

  • 分成两种状态:

    • 内部状态:可以公用的放在一起的 display()

    • 外部状态:xCoord, yCoord, age

      • 内部状态放到类里面
      • 外部状态简化成一个树管理的类来管理外部状态
      • 显示的时候使用外部状态在调用内部状态
  • 代码分析:

package yingliangmoshi.TreeFlyWeight;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:25
*/


/**
* 内部状态
*/
public class TreeFlyWeight {
    public TreeFlyWeight(){


    }
    //只放一个内部状态
    public void display(int xCoord, int yCoord, int age){
//        System.out.println(xCoord);
    }
}


package yingliangmoshi.TreeFlyWeight;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:26
*/


/**
* 处理外部状态
*/
public class TreeManager {
    public int length = 1000000;
    //将外部状态放到数组中
    int[] xArray = new int[length];
    int[] yArray = new int[length];
    int[] ageArray = new int[length];
    private TreeFlyWeight treeFlyWeight;
    public TreeManager(){
        treeFlyWeight = new TreeFlyWeight();
        for(int i = 0;i<length;i++){
            xArray[i] = (int)(Math.random() * length);
            yArray[i] = (int)(Math.random() * length);
            ageArray[i] =(int)(Math.random() * length)%5;
        }
    }


    public void displayTrees(){
        for(int i= 0;i<length;i++){
            treeFlyWeight.display(xArray[i], yArray[i], ageArray[i]);
        }
    }


}
package yingliangmoshi.TreeFlyWeight;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-05-06
* Time: 19:29
*/
public class MainTest {
    public static void main(String[] args) {
        showInfo();
        TreeManager treeManager;
        treeManager = new TreeManager();
        showInfo();
        treeManager.displayTrees();
        showInfo();
    }
    public static void showInfo(){
        long max = Runtime.getRuntime().maxMemory();
        long total = Runtime.getRuntime().totalMemory();


        long free  = Runtime.getRuntime().totalMemory();


        long used = total - free;


        System.out.println("最大内存= "+ max);
        System.out.println("已分配内存= "+ total);
        System.out.println("已分配剩余内存内存= "+ free);
        System.out.println("已用内存= " + used);
        System.out.println("当前时间="+System.currentTimeMillis());
        System.out.println();




    }
}



  • 不管项目变得多么复杂:
当存在多个细粒度对象的时候,找到公共方法,抽象成一个抽象类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wjxbless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值