每日总结

软件设计模式  享元模式:

 

#include<iostream>
#include<string>
using namespace std;
class AbstractText {
public:
    virtual string getType() = 0;
    virtual void use() = 0;
};
class TextFactory {
public:
    int totalwhite = 0;
    int totalblack = 0;
    int totalword = 0;
    AbstractText* getText(string type);
    int getTotalWhite();
    int getTotalBlack();
    int getTotalWord();
};
class Word :public AbstractText{
public:
    string getType();
    void use();
};
class White :public AbstractText{
public:
    string getType();
    void use();
};
class Black:public AbstractText{
public:
    string getType();
    void use();
};
string White::getType(){
    return "white";
}
void White::use() {
    cout << "白棋\n";
}
string Black::getType() {
    return "black";
}
void Black::use() {
    cout << "黑棋\n";
}
string Word::getType() {
    return "Hello world";
}
void Word::use() {
    cout << "下棋\n";
}
AbstractText* TextFactory::getText(string type) {
    if (type=="white") {
        totalwhite++;
        AbstractText* a = new White();
        return a;

    }
    else if (type=="black") {
        totalblack++;
        AbstractText* a = new Black();
        return a;

    }
    else if (type=="word") {
        totalword++;
        AbstractText *a = new Word();
        return a;

    }
    else
        return NULL;
}
int TextFactory::getTotalWhite() {
    return totalwhite;
}
int TextFactory::getTotalBlack() {
    return totalblack;
}
int TextFactory::getTotalWord() {
    return totalword;
}
int main() {
    AbstractText *at1, *at2, *at3, *at4;
    TextFactory *tf = new TextFactory();

    at3 = tf->getText("word");

    at1 = tf->getText("white");
    at1->use();
    at3->use();

    at2 = tf->getText("black");
    at2->use();
    at3->use();

    at4 = tf->getText("white");
    at4->use();
    at3->use();

    cout << "white: " << tf->getTotalWhite() << endl;
    cout << "black: " << tf->getTotalBlack() << endl;
    cout << "word: " << tf->getTotalWord() << endl;
    return 0;
}

 

posted @   chenghaixinag  阅读(27)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
历史上的今天:
2020-10-11 每日总结17
点击右上角即可分享
微信分享提示