设计模式-享元模式

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

类图:

    

说明:Flyweight表示抽象向元磊,它声明一个接口,通过他可以接受并作用于外部状态;ConcreteFlyweight表示具体享元类,它实现了Flyweight接口,并为内部状态(如果有的话)增加存储空间,,具体享元对象必须是可以共享的,它所存储的状态必须是内部的,即独立存在于享元对象所处环境,UnsharedConcreteFlyweight表示非共享具体向元磊,并不是所有的Flyweight的子类都需要被共享,Flyweight的共享不是强制的,在某些Flyweight的层次结构中,UnsharedConcreteFlyweight对象通常将ConcreteFlyweight对象作为子节点;FlyweightFactory表示享元工厂类,通过它来创建并管理享元对象,并确保Flyweight的使用,当用户请求一个Flyweight时,FlyweightFactory提供一个已创建的实例或者创建一个新实例(如果不存在);Client表示客户类,用于维持一个队Flyweight的引用,并可计算或存储一个或多个Flyweight的外部状态。

优点:

①大幅度降低内存中对象的数量

②享元模式由于使用了外部状态,外部状态相对独立,不会影响到内部状态,所以使用享元模式使得享元对象能够在不同的环境被共享。

缺点:

①由于享元模式需要区分外部状态和内部状态,使得应用程序在某种程度上来说更加复杂化了。

②为了使对象可以共享,享元模式需要将享元对象的状态外部化,而读取外部状态使得运行时间变长。

适用环境:

①如果一个系统中存在大量的相同或者相似的对象,由于这类对象的大量使用,会造成系统内存的耗费,可以使用享元模式来减少系统中对象的数量。

②对象的大部分状态都可以外部化,可以将这些外部状态传入对象中。

实例场景:使用享元模式设计一个围棋软件,在系统中之存在一个白棋对象和一个黑棋对象,但是他们可以在期盼的不同位置显示多次,以下使用简单工厂模式和单例模式实现享元工厂类的设计。

实例类图:

           

实例代码:

//坐标类:

/**
 * 坐标类:外部状态类
 * @author fly
 *
 */
class Coordinates {
	
	private int x;
	private int y;
	
	public Coordinates(int x,int y) {
		// TODO Auto-generated constructor stub
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
}

//享元类

/**
 * 围棋棋子类:抽象享元类
 * @author fly
 *
 */
abstract class IgoChessman {
	
	public abstract String getColor();
	public void locate(Coordinates coord){
		System.out.println("棋子颜色:"+this.getColor()+",棋子位置:"+coord.getX()+","+coord.getY());
	}
}
/**
 * 黑色棋子类:具体享元类
 * @author fly
 *
 */
class BlackIgoChessman extends IgoChessman{

	@Override
	public String getColor() {
		// TODO Auto-generated method stub
		return "黑色";
	}

}
/**
 * 白色棋子类:具体享元类
 * @author fly
 *
 */
class WhiteIgoChessman extends IgoChessman{

	@Override
	public String getColor() {
		// TODO Auto-generated method stub
		return "白色";
	}

}
//享元工厂类

/**
 * 围棋棋子工厂类:享元工厂类
 * @author fly
 *
 */
public class IgoChessmanFactory {
	
	private static IgoChessmanFactory instance = new IgoChessmanFactory();
	private static Hashtable ht;
	
	public IgoChessmanFactory() {
		// TODO Auto-generated constructor stub
		ht = new Hashtable();
		IgoChessman black,white;
		black = new BlackIgoChessman();
		ht.put("b", black);
		white = new WhiteIgoChessman();
		ht.put("w", white);
	}
	
	public static IgoChessmanFactory getInstance(){
		return instance;
	}
	
	public static IgoChessman getIgoChessman(String color){
		return (IgoChessman)ht.get(color);
	}
}

//客户端测试类

/**
 * 客户端测试类
 * @author fly
 *
 */
public class Client {
	
	public static void main(String[] args) {
		IgoChessman black1,black2,black3,white1,white2;
		IgoChessmanFactory factory;
		factory = IgoChessmanFactory.getInstance();
		black1 = factory.getIgoChessman("b");
		black2 = factory.getIgoChessman("b");
		black3 = factory.getIgoChessman("b");
		System.out.println("判断两颗黑棋是否相同:"+(black1==black2));
		white1 = factory.getIgoChessman("w");
		white2 = factory.getIgoChessman("w");
		System.out.println("判断两颗白棋是否相同:"+(white1==white2));
		black1.locate(new Coordinates(1, 2));
		black2.locate(new Coordinates(3, 4));
		black3.locate(new Coordinates(1, 3));
		white1.locate(new Coordinates(2, 5));
		white2.locate(new Coordinates(2, 4));
	}
}

//运行结果:


  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值