享元模式

来源:互联网 发布:有什么好的护肤品知乎 编辑:程序博客网 时间:2024/05/22 07:02

享元模式

   运用共享技术有效地支持大量细粒度的对象。

一 、 概述

一个类中的成员变量表明该类所创建的对象所具有的属性,在某些程序设计中我们可能用一个类创建若干个对象,但是我们发现这些对象的一个共同特点是它们有一部分属性的取值必须是完全相同的。

二、享元模式的结构与使用 

模式的结构中包括三种角色:

享元接口(Flyweight
具体享元(Concrete Flyweight
享元工厂(Flyweight Factory

模式的UML类图

模式的结构的描述与使用'

1.享元接口(Flyweight:Flyweight.java

public interface Flyweight{

    public double getHeight(); 

    public double getWidth();

    public double getLength();

    public void printMess(String mess);

}

2.享元工厂(FlyweightFactory)与具体享元_1:FlyweightFactory.java

import java.util.HashMap;

public class FlyweightFactory{

       private   HashMap<String,FlyweighthashMap;

       static  FlyweightFactory factory=newFlyweightFactory();

       private FlyweightFactory(){

             hashMap=newHashMap<String,Flyweight>();

       }

       public static FlyweightFactorygetFactory(){

             returnfactory;

       }

       publicsynchronized Flyweight getFlyweight(String key){

            if(hashMap.containsKey(key))

                      return hashMap.get(key);

            else{

                     double width=0,height=0,length=0;

                     String [] str=key.split("#");

                     width=Double.parseDouble(str[0]);

                     height=Double.parseDouble(str[1]);

                     length=Double.parseDouble(str[2]);

                     Flyweight ft=new ConcreteFlyweight(width,height,length);

                      hashMap.put(key,ft);

                     return ft;

            }

       }

2.享元工厂(FlyweightFactory)与具体享元_2:FlyweightFactory.java

class ConcreteFlyweight implementsFlyweight{ 

             private double width;

             private double height;

             private double length;

             private ConcreteFlyweight(double width,doubleheight,double length){

                   this.width=width;

                   this.height=height;

                   this.length=length;

            }

            public double getHeight(){

                   return height;

            }

            public double getWidth(){

                   return width;

            }

            public double getLength(){

                   return length;

           }

           public void printMess(String mess){

                 System.out.print(mess);      

                 System.out.print("宽度:"+width);

                 System.out.print("高度:"+height);

                 System.out.println("长度:"+length);

           }

      }

}

3.应用_1: Car.java

public class Car{

     Flyweight flyweight

     String name,color;

     int power;

     Car(Flyweight flyweight,Stringname,Stringcolor,int power){

           this.flyweight=flyweight;

           this.name=name;

           this.color=color;

           this.power=power;

     }

    public void print(){

          System.out.print("名称:"+name);

          System.out.print("颜色:"+color);

          System.out.print("功率:"+power);

          System.out.print("宽度:"+flyweight.getWidth());

          System.out.print("高度:"+flyweight.getHeight());

          System.out.println("长度:"+flyweight.getLength());

     }

}

3.应用_2: Application.java

 public class Application{

     public static void main(String args[]) {

           FlyweightFactory factory=FlyweightFactory.getFactory();

           doublewidth=1.82,height=1.47,length=5.12;

           Stringkey=""+width+"#"+height+"#"+length;

           Flyweight flyweight=factory.getFlyweight(key);

           Car audiA6One=newCar(flyweight,"奥迪A6","黑色",128);

           Car audiA6Two=newCar(flyweight,"奥迪A6","灰色",160);

           audiA6One.print();

           audiA6Two.print();

           width=1.77;

           height=1.45;

           length=4.63;

          key=""+width+"#"+height+"#"+length;

           flyweight=factory.getFlyweight(key);

           Car audiA4One=newCar(flyweight,"奥迪A4","蓝色",126);

           Car audiA4Two=newCar(flyweight,"奥迪A4","红色",138);

           flyweight.printMess("名称:奥迪A4 颜色:蓝色 功率:126");

           flyweight.printMess("名称:奥迪A4 颜色:红色 功率:138");

     }

}

三、享元模式的优点 

使用享元可以节省内存的开销,特别适合处理大量细粒度对象,这些对象的许多属性值是相同的,而且一旦创建则不容许修改。
享元模式中的享元可以使用方法的参数接受外部状态中的数据,但外部状态数据不会干扰到享元中的内部数据,这就使得享元可以在不同的环境中被共享。


原创粉丝点击