享元模式一五子棋游戏

一模式定义

享元模式,以共享的方式高效地支持大量的细粒度对象。通过复用内存中已存在的对象,降低系统创建对象实例的性能消耗。享元的英文是Flyweight,表示特别小的对象,即细粒度对象。

 

二模式举例

1模式分析

我们借用五子棋游戏来说明这一模式。


2享元模式静态类图



3代码示例

3.1创建抽象棋子一AbstractChessman

package com.demo.flyweight.object;

public abstract class AbstractChessman {
	// 棋子坐标
	protected int x;
	protected int y;
	// 棋子类别(黑|白)
	protected String chess;

	public AbstractChessman(String chess) {
		this.chess = chess;
	}

	// 点坐标设置
	public abstract void point(int x, int y);

	// 显示棋子信息
	public void show() {
		System.out.println(this.chess + "(" + this.x + "," + this.y + ")");
	}
}

3.2创建黑子一BlackChessman

package com.demo.flyweight.object;

public class BlackChessman extends AbstractChessman {

	/**
	 * 构造方法 初始化黑棋子
	 */
	public BlackChessman() {
		super("●");
		System.out.println("--BlackChessman Construction Exec!!!");
	}

	// 点坐标设置
	@Override
	public void point(int x, int y) {
		this.x = x;
		this.y = y;
		// 显示棋子内容
		show();
	}
}

3.3创建白子一WhiteChessman

package com.demo.flyweight.object;

public class WhiteChessman extends AbstractChessman {
	/**
	 * 构造方法 初始化白棋子
	 */
	public WhiteChessman() {
		super("○");
		System.out.println("--WhiteChessman Construction Exec!!!");
	}

	// 点坐标设置
	@Override
	public void point(int x, int y) {
		this.x = x;
		this.y = y;
		// 显示棋子内容
		show();
	}
}

3.4创建棋子工厂一FiveChessmanFactory

package com.demo.flyweight.factory;

import java.util.Hashtable;

import com.demo.flyweight.object.AbstractChessman;
import com.demo.flyweight.object.BlackChessman;
import com.demo.flyweight.object.WhiteChessman;

public class FiveChessmanFactory {
	// 单例模式工厂
	private static FiveChessmanFactory fiveChessmanFactory = new FiveChessmanFactory();

	// 缓存存放共享对象
	private final Hashtable<Character, AbstractChessman> cache = new Hashtable<Character, AbstractChessman>();

	// 私有化构造方法
	private FiveChessmanFactory() {
	}

	// 获得单例工厂
	public static FiveChessmanFactory getInstance() {
		return fiveChessmanFactory;
	}

	/**
	 * 根据字符获得棋子
	 * 
	 * @param c
	 *            (B:黑棋 W:白棋)
	 * @return
	 */
	public AbstractChessman getChessmanObject(char c) {
		// 从缓存中获得棋子对象实例
		AbstractChessman abstractChessman = this.cache.get(c);
		if (abstractChessman == null) {
			// 缓存中没有棋子对象实例信息 则创建棋子对象实例 并放入缓存
			switch (c) {
			case 'B':
				abstractChessman = new BlackChessman();
				break;
			case 'W':
				abstractChessman = new WhiteChessman();
				break;
			default:
				break;
			}

			// 为防止 非法字符的进入 返回null
			if (abstractChessman != null) {
				// 放入缓存
				this.cache.put(c, abstractChessman);
			}
		}
		// 如果缓存中存在 棋子对象则直接返回
		return abstractChessman;

	}
}

3.5客户端实现一Client

package com.demo;

import java.util.Random;

import com.demo.flyweight.factory.FiveChessmanFactory;
import com.demo.flyweight.object.AbstractChessman;

/**
 * 主应用程序
 * 
 * @author
 */
public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 创建五子棋工厂
		FiveChessmanFactory fiveChessmanFactory = FiveChessmanFactory
				.getInstance();
		Random random = new Random();
		int radom = 0;
		AbstractChessman abstractChessman = null;
		// 随机获得棋子
		for (int i = 0; i < 10; i++) {

			radom = random.nextInt(2);
			switch (radom) {
			// 获得黑棋
			case 0:
				abstractChessman = fiveChessmanFactory.getChessmanObject('B');
				break;
			// 获得白棋
			case 1:
				abstractChessman = fiveChessmanFactory.getChessmanObject('W');
				break;
			}

			if (abstractChessman != null) {
				abstractChessman.point(i, random.nextInt(15));
			}

		}
	}
}

4运行结果

--WhiteChessman Construction Exec!!!

○(0,2)

○(1,6)

--BlackChessman Construction Exec!!!

●(2,3)

○(3,14)

○(4,13)

○(5,8)

●(6,14)

●(7,0)

●(8,3)

○(9,8)

 

三享元模式的两种状态

  • 内蕴状态:不会随环境的改变而改变,是存储在享元对象内部状态信息,困此内蕴状态是可以共享的,对于任何一个享元对象来讲,它的值是完全相同的。就像五子棋中的"黑子"和"白子",它代表的状态就是内蕴状态。
  • 外蕴状态:它会随环境的改变而改变,因此不可以共享状态,对于不同的享元对象讲,它的值可能是不同的。享元对象的外蕴状态必须由客户端保存,在享元对象被创建之后,需要使用的时候再传入享元对象内部。就像五子棋的位置信息,代表的状态就是享元对象的外蕴状态。

所以,享元的外蕴状态和内蕴状态是两类相互独立的状态,彼此没关联。

 

四该模式设计原则

1共享细粒度对象,降低内存空间。

2有效地隔离系统中变化部分和不变部分。

 

五使用场合

1当系统中某个对象类型的实例较多的时候。

2在系统设计中,对象实例进行分类后,发现真正有区别的分类很少的时候。

 

六享元模式静态类图



 

  • 大小: 96 KB
  • 大小: 61.5 KB
  • 大小: 46.5 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值