首页 > 其他分享 >软件设计-享元模式

软件设计-享元模式

时间:2022-11-04 15:55:48浏览次数:123  
标签:享元 软件设计 模式 IgoChessmanFactory pPiece int Coordinates new public

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

 

类图

 

 

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);
}
 

 

运行截图

标签:享元,软件设计,模式,IgoChessmanFactory,pPiece,int,Coordinates,new,public
From: https://www.cnblogs.com/wlqyyds/p/16858089.html

相关文章

  • 软件设计-命令模式 _
    多次撤销和重复的命令模式某系统需要提供一个命令集合(注:可以使用链表,栈等集合对象实现),用于存储一系列命令对象,并通过该命令集合实现多次undo()和redo()操作,可以使用加法运......
  • 软件设计-迭代器模式
    JAVA和C++常见数据结构迭代器的使用信1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,要求按照学号从......
  • 行为型设计模式之访问者模式
    访问者模式访问者模式属于行为型模式。它是一种将数据结构与数据操作分离的设计模式。是指封装一些作用于某种数据结构中的各元素的操作,它可以在不改变数据结构的前提下定......
  • 广州华锐互动:虚拟现实技术打造安全事故模拟体验新模式
    随着我国的不断发展和文明程度的不断提高,安全问题越来越受到重视。对于安全事故的预防处理,作业人员每年都需要进行至少一次的安全教育培训和考核,但是很多事故无法亲身经历,......
  • 观察者模式
    观察者模式对象间存在一对多关系时,使用观察者模式.当一个对象被修改时,会自动通知依赖它的对象,观察者模式属于行为模式如何解决抽象类里有一个ArrayList存放观察者们......
  • 图扑软件智慧云展厅,开启数字化展馆新模式
    前言随着疫情的影响以及新兴技术的不断发展,展会的发展形式也逐渐从线下转向线上。通过“云”上启动、云端互动、双线共频的形式开展。通过应用大数据、人工智能、沉浸式交......
  • 线程安全的单例模式
    懒汉式加方法锁publicclassSingleton{privatestaticSingletonsingleton=null;privateSingleton(){}publicstaticsynchronizedSingletongetIn......
  • SpringCloud(六) - RabbitMQ安装,三种消息发送模式,消息发送确认,消息消费确认(自动,手动)
    1、安装erlang语言环境1.1创建erlang安装目录mkdirerlang1.2上传解压压缩包上传到:/root/解压缩#tar-zxvfotp_src_22.0.tar.gz1.3进入解压缩目录,指定......
  • BIOS三种硬盘模式
    文章目录​​IDE模式​​​​RAID模式​​​​AHCI模式​​IDE模式IDE表示硬盘的传输接口,我们常说的IDE接口,也叫ATA(AdvancedTechnologyAttachment)接口,现在PC机使用的硬盘......
  • TDengine:无模式写入行协议的四种方式
    小T导读:为了在数据采集项频繁变动的情况下保证用户仍然能够顺利地完成数据记录工作,TDengine 提供了三种无模式写入协议,分别是InfluxDBLine协议、OpenTSDB Telnet协......