享元模式(大话设计模式)C/C++版本

享元模式

C++

#include <iostream>
#include <string>
#include <map>
using namespace std;

// 用户类 用户网站的客户账号,是"网站"类的外部状态
class User
{
private:
    string m_name;

public:
    User(string name)
    {
        m_name = name;
    }
    std::string GetName()
    {
        return m_name;
    }
};

// 抽象网站类 定义对象的内部状态和外部状态及其对应的方法。
class WebSite
{
public:
    virtual ~WebSite() = default;
    virtual void Use(User user) = 0;
};

// 此处为具体网站类  实现抽象享元角色的方法,在具体的角色中,实现具体方法时需要注意将内部状态与外部状态区分开,不应出现二者同时被修改的方法。
class ConcreteWebSite : public WebSite
{
private:
    string m_name;

public:
    ConcreteWebSite(std::string name)
    {
        m_name = name;
    }
    void Use(User user) override
    {
        cout << "网站分类:" << m_name << "  用户:" + user.GetName() << endl;
    }
};

// 此处为网站工程类 负责创建和管理享元角色。当客户对象请求一个享元对象时,享元工厂检査系统中是否存在符合要求的享元对象,
// 如果存在则提供给客户;如果不存在的话,则创建一个新的享元对象。
class WebSiteFactory
{
private:
    std::map<std::string, WebSite *> flyweights;

public:
    ~WebSiteFactory()
    {
        for (auto it = flyweights.begin(); it != flyweights.end(); ++it)
            delete it->second;
    }
    WebSite *GetWebSiteCategory(string key)
    {
        for (auto it = flyweights.begin(); it != flyweights.end(); ++it)
        {
            if (it->first == key)
                return it->second;
        }

        WebSite *website = new ConcreteWebSite(key);
        flyweights.insert(pair<std::string, WebSite *>(key, website));
        return website;
    }
    int GetWebSiteCount()
    {
        return flyweights.size();
    }
};

int main()
{
    WebSiteFactory f;

    WebSite *fx = f.GetWebSiteCategory("产品展示");
    fx->Use(User("小菜"));

    WebSite *fy = f.GetWebSiteCategory("产品展示");
    fy->Use(User("大鸟"));

    WebSite *fz = f.GetWebSiteCategory("产品展示");
    fz->Use(User("娇娇"));

    WebSite *fl = f.GetWebSiteCategory("博客");
    fl->Use(User("老顽童"));

    WebSite *fm = f.GetWebSiteCategory("博客");
    fm->Use(User("桃谷六仙"));

    WebSite *fn = f.GetWebSiteCategory("博客");
    fn->Use(User("南海鳄神"));

    cout << "得到网站分类总数:" << f.GetWebSiteCount() << endl;

    return 0;
}

C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

// 定义哈希表的节点结构体
typedef struct HashNode
{
    char *key;
    void *value;
    struct HashNode *next;
} HashNode;

// 定义哈希表结构体
typedef struct HashTable
{
    HashNode **table;
    size_t size;
} HashTable;

// 创建哈希表
HashTable *create_hash_table(size_t size)
{
    HashTable *hash_table = malloc(sizeof(HashTable));
    hash_table->table = malloc(sizeof(HashNode *) * size);
    hash_table->size = size;
    memset(hash_table->table, 0, sizeof(HashNode *) * size);
    return hash_table;
}

// 销毁哈希表
void destroy_hash_table(HashTable *hash_table)
{
    size_t i;
    for (i = 0; i < hash_table->size; ++i)
    {
        HashNode *node = hash_table->table[i];
        while (node)
        {
            HashNode *next = node->next;
            free(node->key);
            free(node->value);
            free(node);
            node = next;
        }
    }
    free(hash_table->table);
    free(hash_table);
}

// 计算哈希值
size_t hash(const char *key, size_t size)
{
    size_t hash = 5381;
    int c;
    while ((c = *key++))
    {
        hash = ((hash << 5) + hash) + c;
    }
    return hash % size;
}

// 在哈希表中插入键值对
bool insert_hash_table(HashTable *hash_table, const char *key, void *value)
{
    size_t index = hash(key, hash_table->size);
    HashNode *new_node = malloc(sizeof(HashNode));
    new_node->key = strdup(key);
    new_node->value = value;
    new_node->next = hash_table->table[index];
    hash_table->table[index] = new_node;
    return true;
}

// 从哈希表中获取值
void *get_from_hash_table(HashTable *hash_table, const char *key)
{
    size_t index = hash(key, hash_table->size);
    HashNode *node = hash_table->table[index];
    while (node)
    {
        if (strcmp(node->key, key) == 0)
        {
            return node->value;
        }
        node = node->next;
    }
    return NULL;
}

// 用户结构体
typedef struct User
{
    char *name;
} User;

// 网站结构体
typedef struct Website
{
    char *name;
} Website;

// 网站工厂结构体
typedef struct WebsiteFactory
{
    HashTable *flyweights;
} WebsiteFactory;

// 创建用户
User *create_user(const char *name)
{
    User *user = malloc(sizeof(User));
    user->name = strdup(name);
    return user;
}

// 销毁用户
void destroy_user(User *user)
{
    free(user->name);
    free(user);
}

// 创建网站
Website *create_website(const char *name)
{
    Website *website = malloc(sizeof(Website));
    website->name = strdup(name);
    return website;
}

// 销毁网站
void destroy_website(Website *website)
{
    free(website->name);
    free(website);
}

// 使用网站
void use_website(Website *website, User *user)
{
    printf("网站分类:%s  用户:%s\n", website->name, user->name);
}

// 创建网站工厂
WebsiteFactory *create_website_factory()
{
    WebsiteFactory *factory = malloc(sizeof(WebsiteFactory));
    factory->flyweights = create_hash_table(10);
    return factory;
}

// 销毁网站工厂
void destroy_website_factory(WebsiteFactory *factory)
{
    destroy_hash_table(factory->flyweights);
    free(factory);
}

// 从工厂获取网站
Website *get_website_category(WebsiteFactory *factory, const char *key)
{
    Website *website = get_from_hash_table(factory->flyweights, key);
    if (!website)
    {
        website = create_website(key);
        insert_hash_table(factory->flyweights, key, website);
    }
    return website;
}

int main()
{
    WebsiteFactory *f = create_website_factory();

    Website *fx = get_website_category(f, "产品展示");
    User *user_fx = create_user("小菜");
    use_website(fx, user_fx);
    destroy_user(user_fx);

    Website *fy = get_website_category(f, "产品展示");
    User *user_fy = create_user("大鸟");
    use_website(fy, user_fy);
    destroy_user(user_fy);

    Website *fz = get_website_category(f, "产品展示");
    User *user_fz = create_user("娇娇");
    use_website(fz, user_fz);
    destroy_user(user_fz);

    Website *fl = get_website_category(f, "博客");
    User *user_fl = create_user("老顽童");
    use_website(fl, user_fl);
    destroy_user(user_fl);

    Website *fm = get_website_category(f, "博客");
    User *user_fm = create_user("桃谷六仙");
    use_website(fm, user_fm);
    destroy_user(user_fm);

    Website *fn = get_website_category(f, "博客");
    User *user_fn = create_user("南海鳄神");
    use_website(fn, user_fn);
    destroy_user(user_fn);

    size_t count = 0;
    size_t i;
    for (i = 0; i < f->flyweights->size; ++i)
    {
        if (f->flyweights->table[i])
        {
            ++count;
        }
    }
    printf("得到网站分类总数:%zu\n", count);

    destroy_website_factory(f);

    return 0;
}

相关推荐

  1. 模式(大话设计模式)C/C++版本

    2024-07-15 16:40:02       19 阅读
  2. 设计模式

    2024-07-15 16:40:02       50 阅读
  3. 设计模式-模式

    2024-07-15 16:40:02       54 阅读
  4. 设计模式模式

    2024-07-15 16:40:02       33 阅读
  5. 设计模式模式

    2024-07-15 16:40:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 16:40:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 16:40:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 16:40:02       58 阅读
  4. Python语言-面向对象

    2024-07-15 16:40:02       69 阅读

热门阅读

  1. html 关闭信息窗口

    2024-07-15 16:40:02       22 阅读
  2. vue3+springboot+minio,实现文件上传功能

    2024-07-15 16:40:02       20 阅读
  3. 使用Python进行桌面应用程序开发

    2024-07-15 16:40:02       16 阅读
  4. 启动 zabbix 相关服务

    2024-07-15 16:40:02       19 阅读
  5. 【AI应用探讨】—KAN应用场景

    2024-07-15 16:40:02       23 阅读
  6. 【无标题】

    2024-07-15 16:40:02       19 阅读
  7. 租用海外服务器需要考虑哪些因素

    2024-07-15 16:40:02       18 阅读
  8. 1448. 统计二叉树中好节点的数目

    2024-07-15 16:40:02       21 阅读
  9. solidity实战练习2--ERC20实现

    2024-07-15 16:40:02       24 阅读