享元模式设计简单的围棋软件
设计一个围棋软件,在系统中只存在一个白棋对象和一个黑棋对象,但是它们可以在棋盘的不同位置显示多次。

java代码
BlackPiece
public class BlackPiece extends Piece{ @Override public String getColor() { // TODO Auto-generated method stub return "黑棋"; } }
Piece
public abstract class Piece { public abstract String getColor(); public void locate(Location lo) { System.out.println(this.getColor()+" "+lo.getX()+","+lo.getY()); } }
WhitePiece
public class WhitePiece extends Piece{ @Override public String getColor() { // TODO Auto-generated method stub return "白棋"; } }
PieceFactory
public class PieceFatory { private static PieceFatory instance=new PieceFatory(); private static Piece wp=new WhitePiece(); private static Piece bp=new BlackPiece(); public PieceFatory() { // TODO Auto-generated constructor stub //instance=new PieceFatory(); //Piece wp=new WhitePiece(); //Piece bp=new BlackPiece(); } public static PieceFatory getInstance() { return instance; } public static Piece getPiece(String color) { if(color.equals("白棋")) { return wp; }else { return bp; } } }
Location
public class Location { private String x; private String y; public Location(String x,String y) { // TODO Auto-generated constructor stub this.x=x; this.y=y; } public String getX() { return x; } public String getY() { return y; } public void setX(String x) { this.x=x; } public void setY(String y) { this.y=y; } }
Client
public class Client { public static void main(String[]args) { Piece b1,b2,b3,w1,w2,w3; PieceFatory pf; pf=PieceFatory.getInstance(); b1=pf.getPiece("黑棋"); b2=pf.getPiece("黑棋"); System.out.println("判断两颗黑棋是否相同:"+(b1==b2)); w1=pf.getPiece("白棋"); w2=pf.getPiece("白棋"); System.out.println("判断两颗白棋是否相同:"+(w1==w2)); b1.locate(new Location("1","2")); b2.locate(new Location("3","2")); w1.locate(new Location("10","2")); w2.locate(new Location("1","21")); } }

c++代码
#include<iostream> #include<string> using namespace std; class Location { private: string x; string y; public: Location(string x, string y) { // TODO Auto-generated constructor stub this->x = x; this->y = y; } string getX() { return x; } string getY() { return y; } void setX(string x) { this->x = x; } void setY(string y) { this->y = y; } }; class Piece { public: virtual string getColor()=0; void locate(Location *lo) { cout<<this->getColor() << " " <<lo->getX() << "," <<lo->getY()<<endl; } }; class WhitePiece:public Piece{ public: string getColor() { // TODO Auto-generated method stub return "白棋"; } }; class BlackPiece :public Piece{ public: string getColor() { // TODO Auto-generated method stub return "黑棋"; } }; class PieceFatory { private: //PieceFatory *instance; Piece *wp=new WhitePiece() ; Piece *bp=new BlackPiece() ; public: PieceFatory *getInstance() { // cout << " dsv1 " << endl; return new PieceFatory(); } Piece *getPiece(char *color) { if (strcmp("白棋",color)==0) { //cout << 1; return wp; } else { return bp; } } }; int main() { Piece *b1, *b2, *w1, *w2; //cout << " dsv " << endl; PieceFatory *pf = new PieceFatory(); //cout << " dsv1 " << endl; b1 = pf->getPiece("黑棋"); //cout << " dsv1 " << endl; b2 = pf->getPiece("黑棋"); //cout << " dsv1 " << endl; cout<<"判断两颗黑棋是否相同:" <<(b1 == b2)<<endl; w1 = pf->getPiece("白棋"); w2 = pf->getPiece("白棋"); cout<<"判断两颗白棋是否相同:" <<(w1 == w2)<<endl; b1->locate(new Location("1", "2")); b2->locate(new Location("3", "2")); //cout << 1; w1->locate(new Location("10", "2")); w2->locate(new Location("1", "21")); }

浙公网安备 33010602011771号