享元(flyweight)模式主要用于减少创建对象的数量,以低内存占用,提高性能。现要开发一个网络围棋程序允许多个玩家联机下棋。由于只有一台服务器,为节内存空间,采用享元模式实现该程序,得到如图6-1所的类图。
【Java代码】
import java.util.*:
enum PieceColor {BLACK,WHITE}//棋子颜色
class PiecePos{//棋子位置
private intx;
private int y:
pubic PiecePos(int a,int b){x=a;y=b;}
public int getX0{retun x;}
public int getYO{return y;}
}
abstract class Piece{//棋子定义
protected PieceColor m_color;//颜色
protected Piecemopos m_pos;//位置
public Piece(PieceColor,color PiecePos
pos){m color=color;
{:sod=sod u
(1);
}
class BlackPiece extends Piece{
public BlackPiece(PieceColor
color,PiecePos pos){super(color,pos);}
public void drawO{
System out println("draw a black
piece");}
}
class WhitePiece extends Piece{
public WhitePiece(PieceColor
color,PiecePos pos){super(c
olor,pos):}
public void draw0{
System.out.println("draw a white
piece");
}
{
class PieceBoard{
//棋盘上已有的棋子
private static final
ArrayList<(2)>m_arrayPiece=new Arra
yList
private String m_blackName;//黑方名称
private String m_whiteName;//白方名称
public PieceBoard(String black,String
white){
m_blackName=black;m_whiteName=white;
}
//一步棋,在棋盘上放一颗棋子
public void SetePiece(PieceColor
color,PiecePos pos){
(3)piece=null;
if(color==PieceColor.BLACK)(//放黑子
piece=new BlackPiece(color,pos);//获取一颗黑子
System.out.println(m_blackName+"在位置("+pos.getX0)
+","+pos.getYO+")");
(4):
{
else{//放白子
piece=new WhitePiece(color,pos);//获取一颗白子
System.out.println(m whiteName+"在位置("+pos.getX0)+
","+pos.getYO+")");
(5):
}
m_arrayPiece.add(piece);
}
}
(1)public abstract void draw( )
(2)Piece
(3)Piece
(4)piece.draw( )
(5)piece.draw( )