享元模式
1.每日博客2.每日总结3.每日总结4.每日总结5.每日总结6.每日总结7.每日总结,软件需求8.每日总结9.每日博客10.每日博客11.每日博客12.每日博客13.00014.每日博客15.每日博客16.每日博客17.每日总结18.每日总结19.每日博客20.65416521.每日博客22.每日博客223.每日博客24.每日博客25.每日博客26.每日博客27.每日博客28.每日总结29.每日博客,30.每日博客,31.每日博客32.每日总结33.每日博客34.调查问卷35.每日博客
36.享元模式
37.职责链模式38.[实验任务一]:JAVA和C++常见数据结构迭代器的使用39.命令模式40.每日博客41.每日学习总结42.每日博客43.每日博客44.每日博客45.十一月读书笔记46.十一月读书笔记二47.每日博客48.每日博客49.每日博客50.每日博客51.每日52.每日博客53.试题三:(2023年软件设计师原题)54.uml的9种类图55.每日博客56.每日博客57.158.每日博客59.百度机器翻译SDK60.百度图像增强与特效SDK实验61.每日博客62.每日博客63.每日博客64.每日博客65.java期末考试66.软件需求与分析课堂测试十——综合案例分析67.软件设计模式基本知识点68.软件设计期末试卷2020级69.每日博客70.今日学习71.每日博客72.今日博客73.每日博客74.模板方法75.每日博客76.每日博客77.每日博客78.每日博客79.每日博客80.每日博客81.博客82.每日博客83.每日总结84.每日博客85.每日博客86.每日博客87.每日博客88.每日博客89.每日博客90.每日博客91.每日博客92.每日博客93.每日总结94.每日博客95.每日博客196.每日博客97.每日博客98.每日博客99.每日博客100.每日博客
实验13:享元模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解享元模式的动机,掌握该模式的结构;
2、能够利用享元模式解决实际问题。
[实验任务一]:围棋
设计一个围棋软件,在系统中只存在一个白棋对象和一个黑棋对象,但是它们可以在棋盘的不同位置显示多次。
实验要求:
1. 提交类图;
2. 提交源代码;
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());
}
}
class BlackIgoChessman extends IgoChessman{
@Override
public String getColor() {
// TODO Auto-generated method stub
return "黑色";
}
}
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);
}
3.注意编程规范;
4.要求用简单工厂模式和单例模式实现享元工厂类的设计。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!