軟體設計-享元模式
阿新 • • 發佈:2021-12-13
圍棋
設計一個圍棋軟體,在系統中只存在一個白棋物件和一個黑棋物件,但是它們可以在棋盤的不同位置顯示多次。
類圖
Java
package rjsj.no13; /** * 客戶端測試類 * */ 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, 1)); black2.locate(new Coordinates(2, 4)); black3.locate(new Coordinates(2, 3)); white1.locate(new Coordinates(3, 5)); white2.locate(new Coordinates(2, 6)); } }
package rjsj.no13; /** * 座標類:外部狀態類 **/ public 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; } }
package rjsj.no13; /** * 圍棋棋子類:抽象享元類 * */ public 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 "白色"; } }
package rjsj.no13; import java.util.Hashtable; /** * 圍棋棋子工廠類:享元工廠類 * */ 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); } }
C++
#include <iostream> #include <map> #include <vector> using namespace std; typedef struct Coordinates{ int x; int y; Coordinates(){} Coordinates(int a, int b){x=a;y=b;} bool operator <(const Coordinates& other) const{ if (x<other.x) return true; else if (x==other.x) return y<other.y; return false; } }POINT; typedef enum PieceColorTag{ BLACK, WHITE }PIECECOLOR; class CPiece{ public: CPiece(PIECECOLOR color) : m_color(color){} PIECECOLOR GetColor() { return m_color; } void SetPoint(POINT point) { m_point = point; } POINT GetPoint() { return m_point; } protected: PIECECOLOR m_color; POINT m_point; }; class CGomoku : public CPiece{ public: CGomoku(PIECECOLOR color) : CPiece(color){} }; class IgoChessmanFactory{ public: CPiece *GetPiece(PIECECOLOR color){ CPiece *pPiece = NULL; if (m_vecPiece.empty()){ pPiece = new CGomoku(color); m_vecPiece.push_back(pPiece); } else{ for (vector<CPiece *>::iterator it = m_vecPiece.begin(); it != m_vecPiece.end(); ++it){ if ((*it)->GetColor() == color){ pPiece = *it; break; } } if (pPiece == NULL){ pPiece = new CGomoku(color); m_vecPiece.push_back(pPiece); } } return pPiece; } ~IgoChessmanFactory(){ for (vector<CPiece *>::iterator it = m_vecPiece.begin(); it != m_vecPiece.end(); ++it){ if (*it != NULL){ delete *it; *it = NULL; } } } private: vector<CPiece *> m_vecPiece; }; class IgoChessman{ public: void Draw(CPiece *piece){ if (piece->GetColor()){ cout<<"白色棋子位置:"<<piece->GetPoint().x<<","<<piece->GetPoint().y<<endl; } else{ cout<<"黑色棋子位置:"<<piece->GetPoint().x<<","<<piece->GetPoint().y<<endl; } m_mapPieces.insert(pair<POINT, CPiece *>(piece->GetPoint(), piece)); } void ShowAllPieces(){ for (map<POINT, CPiece *>::iterator it = m_mapPieces.begin(); it != m_mapPieces.end(); ++it){ if (it->second->GetColor()){ cout<<"("<<it->first.x<<","<<it->first.y<<")白色棋子"<<endl; } else{ cout<<"("<<it->first.x<<","<<it->first.y<<")黑色棋子"<<endl; } } } private: map<POINT, CPiece *> m_mapPieces; }; int main(){ IgoChessmanFactory *pPieceFactory = new IgoChessmanFactory(); IgoChessman *pCheseboard = new IgoChessman(); CPiece *pPiece = pPieceFactory->GetPiece(WHITE); pPiece->SetPoint(POINT(1, 1)); pCheseboard->Draw(pPiece); pPiece = pPieceFactory->GetPiece(BLACK); pPiece->SetPoint(POINT(3, 5)); pCheseboard->Draw(pPiece); pPiece = pPieceFactory->GetPiece(WHITE); pPiece->SetPoint(POINT(2, 2)); pCheseboard->Draw(pPiece); pPiece = pPieceFactory->GetPiece(BLACK); pPiece->SetPoint(POINT(1, 4)); pCheseboard->Draw(pPiece); }
執行截圖