享元模式
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;
}