完整教程:第五章:原型模式 - 克隆大法的大师

news/2025/10/21 14:37:06/文章来源:https://www.cnblogs.com/lxjshuju/p/19154997

第五章:原型模式 - 克隆大法的大师

故事延续:幻影分身的奇妙艺术

在Builder展示完他的精细构建艺术后,Prototype身形飘忽地走出,他那幻影重重的身影吸引了所有人的注意。这位神秘的高手时而化作三人,时而合为一体,令人难以捕捉。

“Builder兄的逐步构建确实精妙,” Prototype发出空灵的笑声,“但有时候我们并不需要从零开始创建对象。我的武学核心在于——通过复制现有对象来创建新对象。这在创建成本高昂或需要快速创建相似对象时特别有用!”

原型模式的武学精要

核心心法

Prototype双手结印,空中浮现出多个相同的幻影:“我的八字真言是——克隆复制,快速创建。通过实现一个克隆方法,我可以快速创建对象的副本,避免重新执行昂贵的创建过程。”

C++ 代码实战
#include <iostream>#include <memory>#include <string>#include <unordered_map>#include <vector>#include <sstream>// 抽象原型接口class Prototype {public:virtual ~Prototype() = default;virtual std::unique_ptr<Prototype> clone() const = 0;virtual void display() const = 0;virtual std::string getType() const = 0;virtual void customize(const std::string& customization) = 0;};// 具体原型:游戏角色class GameCharacter : public Prototype {private:std::string name_;std::string characterClass_;int level_;int health_;int mana_;std::vector<std::string> skills_;std::string equipment_;public:GameCharacter(const std::string& name, const std::string& characterClass,int level, int health, int mana,const std::vector<std::string>& skills, const std::string& equipment): name_(name), characterClass_(characterClass), level_(level),health_(health), mana_(mana), skills_(skills), equipment_(equipment) {}// 克隆方法 - 核心所在std::unique_ptr<Prototype> clone() const override {std::cout << " 克隆角色: " << name_ << std::endl;return std::make_unique<GameCharacter>(*this);}void display() const override {std::cout << "\n 角色信息:" << std::endl;std::cout << "   名称: " << name_ << std::endl;std::cout << "   职业: " << characterClass_ << std::endl;std::cout << "   等级: " << level_ << std::endl;std::cout << "  ❤️ 生命值: " << health_ << std::endl;std::cout << "   法力值: " << mana_ << std::endl;std::cout << "   技能: ";for (const auto& skill : skills_) {std::cout << skill << " ";}std::cout << std::endl;std::cout << "  ⚔️ 装备: " << equipment_ << std::endl;}std::string getType() const override {return characterClass_;}void customize(const std::string& customization) override {name_ = customization;std::cout << " 自定义角色名为: " << name_ << std::endl;}// 修改等级void setLevel(int level) {level_ = level;std::cout << "⬆️ 设置等级为: " << level_ << std::endl;}// 添加技能void addSkill(const std::string& skill) {skills_.push_back(skill);std::cout << "➕ 添加技能: " << skill << std::endl;}// 更换装备void changeEquipment(const std::string& equipment) {equipment_ = equipment;std::cout << " 更换装备为: " << equipment_ << std::endl;}std::string getDescription() const {std::stringstream ss;ss << name_ << " (Lv." << level_ << " " << characterClass_ << ")";return ss.str();}};// 具体原型:魔法武器class MagicWeapon : public Prototype {private:std::string name_;std::string weaponType_;int damage_;int magicPower_;std::string element_;std::vector<std::string> enchantments_;public:MagicWeapon(const std::string& name, const std::string& weaponType,int damage, int magicPower, const std::string& element,const std::vector<std::string>& enchantments): name_(name), weaponType_(weaponType), damage_(damage),magicPower_(magicPower), element_(element), enchantments_(enchantments) {}std::unique_ptr<Prototype> clone() const override {std::cout << " 克隆魔法武器: " << name_ << std::endl;return std::make_unique<MagicWeapon>(*this);}void display() const override {std::cout << "\n⚔️ 魔法武器信息:" << std::endl;std::cout << "   名称: " << name_ << std::endl;std::cout << "   类型: " << weaponType_ << std::endl;std::cout << "   物理伤害: " << damage_ << std::endl;std::cout << "  ✨ 魔法威力: " << magicPower_ << std::endl;std::cout << "   元素属性: " << element_ << std::endl;std::cout << "   附魔效果: ";for (const auto& enchantment : enchantments_) {std::cout << enchantment << " ";}std::cout << std::endl;}std::string getType() const override {return weaponType_;}void customize(const std::string& customization) override {name_ = customization;std::cout << " 自定义武器名为: " << name_ << std::endl;}// 增强武器void enhance(int additionalDamage, int additionalMagic) {damage_ += additionalDamage;magicPower_ += additionalMagic;std::cout << "⬆️ 增强武器: +" << additionalDamage << "伤害, +" << additionalMagic << "魔法" << std::endl;}// 添加附魔void addEnchantment(const std::string& enchantment) {enchantments_.push_back(enchantment);std::cout << "✨ 添加附魔: " << enchantment << std::endl;}};// 具体原型:游戏地图class GameMap : public Prototype {private:std::string name_;std::string terrainType_;int width_;int height_;std::vector<std::string> landmarks_;std::string weather_;int difficulty_;public:GameMap(const std::string& name, const std::string& terrainType,int width, int height, const std::vector<std::string>& landmarks,const std::string& weather, int difficulty): name_(name), terrainType_(terrainType), width_(width), height_(height),landmarks_(landmarks), weather_(weather), difficulty_(difficulty) {}std::unique_ptr<Prototype> clone() const override {std::cout << "️ 克隆游戏地图: " << name_ << std::endl;return std::make_unique<GameMap>(*this);}void display() const override {std::cout << "\n️ 地图信息:" << std::endl;std::cout << "   名称: " << name_ << std::endl;std::cout << "   地形: " << terrainType_ << std::endl;std::cout << "   尺寸: " << width_ << "x" << height_ << std::endl;std::cout << "  ️ 天气: " << weather_ << std::endl;std::cout << "  ⚠️ 难度: " << difficulty_ << std::endl;std::cout << "   地标: ";for (const auto& landmark : landmarks_) {std::cout << landmark << " ";}std::cout << std::endl;}std::string getType() const override {return terrainType_;}void customize(const std::string& customization) override {name_ = customization;std::cout << " 自定义地图名为: " << name_ << std::endl;}// 改变天气void changeWeather(const std::string& weather) {weather_ = weather;std::cout << "️ 改变天气为: " << weather_ << std::endl;}// 调整难度void adjustDifficulty(int difficulty) {difficulty_ = difficulty;std::cout << "⚠️ 调整难度为: " << difficulty_ << std::endl;}};// 原型管理器:存储和管理原型实例class PrototypeManager {private:std::unordered_map<std::string, std::unique_ptr<Prototype>> prototypes_;public:// 注册原型void registerPrototype(const std::string& key, std::unique_ptr<Prototype> prototype) {prototypes_[key] = std::move(prototype);std::cout << " 注册原型: " << key << std::endl;}// 克隆原型std::unique_ptr<Prototype> clonePrototype(const std::string& key) {auto it = prototypes_.find(key);if (it != prototypes_.end()) {return it->second->clone();}std::cout << "❌ 未找到原型: " << key << std::endl;return nullptr;}// 批量克隆std::vector<std::unique_ptr<Prototype>> cloneMultiple(const std::string& key, int count) {std::vector<std::unique_ptr<Prototype>> clones;for (int i = 0; i < count; ++i) {auto clone = clonePrototype(key);if (clone) {clones.push_back(std::move(clone));}}return clones;}// 显示所有原型void displayAllPrototypes() const {std::cout << "\n 原型管理器中的所有原型:" << std::endl;for (const auto& pair : prototypes_) {std::cout << "   键: " << pair.first << " | 类型: " << pair.second->getType() << std::endl;}}// 获取所有原型键std::vector<std::string> getAllKeys() const {std::vector<std::string> keys;for (const auto& pair : prototypes_) {keys.push_back(pair.first);}return keys;}};

UML 武功秘籍图

manages
«interface»
Prototype
+clone()
+display() : void
+getType() : string
+customize(string) : void
GameCharacter
-string name_
-string characterClass_
-int level_
-int health_
-int mana_
-vector<string> skills_
-string equipment_
+clone()
+display() : void
+getType() : string
+customize(string) : void
+setLevel(int) : void
+addSkill(string) : void
+changeEquipment(string) : void
+getDescription() : string
MagicWeapon
-string name_
-string weaponType_
-int damage_
-int magicPower_
-string element_
-vector<string> enchantments_
+clone()
+display() : void
+getType() : string
+customize(string) : void
+enhance(int, int) : void
+addEnchantment(string) : void
GameMap
-string name_
-string terrainType_
-int width_
-int height_
-vector<string> landmarks_
-string weather_
-int difficulty_
+clone()
+display() : void
+getType() : string
+customize(string) : void
+changeWeather(string) : void
+adjustDifficulty(int) : void
PrototypeManager
-unordered_map<string, Prototype*> prototypes_
+registerPrototype(string, Prototype*) : void
+clonePrototype(string)
+cloneMultiple(string, int)
+displayAllPrototypes() : void
+getAllKeys() : vector<string>

实战演练:游戏对象复制系统

#include <random>#include <algorithm>// 更复杂的游戏对象复制系统// 具体原型:怪物模板class MonsterTemplate : public Prototype {private:std::string name_;std::string monsterType_;int baseHealth_;int baseAttack_;int baseDefense_;std::vector<std::string> abilities_;std::string lootTable_;int experienceValue_;public:MonsterTemplate(const std::string& name, const std::string& monsterType,int baseHealth, int baseAttack, int baseDefense,const std::vector<std::string>& abilities,const std::string& lootTable, int experienceValue): name_(name), monsterType_(monsterType), baseHealth_(baseHealth),baseAttack_(baseAttack), baseDefense_(baseDefense), abilities_(abilities),lootTable_(lootTable), experienceValue_(experienceValue) {}std::unique_ptr<Prototype> clone() const override {std::cout << " 克隆怪物模板: " << name_ << std::endl;return std::make_unique<MonsterTemplate>(*this);}void display() const override {std::cout << "\n 怪物模板信息:" << std::endl;std::cout << "   名称: " << name_ << std::endl;std::cout << "   类型: " << monsterType_ << std::endl;std::cout << "  ❤️ 基础生命: " << baseHealth_ << std::endl;std::cout << "  ⚔️ 基础攻击: " << baseAttack_ << std::endl;std::cout << "  ️ 基础防御: " << baseDefense_ << std::endl;std::cout << "   经验值: " << experienceValue_ << std::endl;std::cout << "   掉落表: " << lootTable_ << std::endl;std::cout << "   能力: ";for (const auto& ability : abilities_) {std::cout << ability << " ";}std::cout << std::endl;}std::string getType() const override {return monsterType_;}void customize(const std::string& customization) override {name_ = customization;std::cout << " 自定义怪物名为: " << name_ << std::endl;}// 生成随机变体std::unique_ptr<MonsterTemplate> createVariant() const {std::random_device rd;std::mt19937 gen(rd());std::uniform_int_distribution<> healthDis(-10, 10);std::uniform_int_distribution<> attackDis(-5, 5);auto variant = std::make_unique<MonsterTemplate>(*this);variant->baseHealth_ += healthDis(gen);variant->baseAttack_ += attackDis(gen);variant->name_ += "变体";std::cout << " 创建怪物变体: " << variant->name_ << std::endl;return variant;}// 获取基础属性int getBaseHealth() const { return baseHealth_; }int getBaseAttack() const { return baseAttack_; }int getBaseDefense() const { return baseDefense_; }};// 具体原型:任务模板class QuestTemplate : public Prototype {private:std::string title_;std::string questType_;std::string description_;std::vector<std::string> objectives_;std::vector<std::string> rewards_;int requiredLevel_;std::string zone_;public:QuestTemplate(const std::string& title, const std::string& questType,const std::string& description, const std::vector<std::string>& objectives,const std::vector<std::string>& rewards, int requiredLevel, const std::string& zone): title_(title), questType_(questType), description_(description),objectives_(objectives), rewards_(rewards), requiredLevel_(requiredLevel), zone_(zone) {}std::unique_ptr<Prototype> clone() const override {std::cout << " 克隆任务模板: " << title_ << std::endl;return std::make_unique<QuestTemplate>(*this);}void display() const override {std::cout << "\n 任务模板信息:" << std::endl;std::cout << "   标题: " << title_ << std::endl;std::cout << "   类型: " << questType_ << std::endl;std::cout << "   描述: " << description_ << std::endl;std::cout << "   要求等级: " << requiredLevel_ << std::endl;std::cout << "  ️ 区域: " << zone_ << std::endl;std::cout << "   目标: " << std::endl;for (const auto& objective : objectives_) {std::cout << "    - " << objective << std::endl;}std::cout << "   奖励: " << std::endl;for (const auto& reward : rewards_) {std::cout << "    - " << reward << std::endl;}}std::string getType() const override {return questType_;}void customize(const std::string& customization) override {title_ = customization;std::cout << " 自定义任务标题为: " << title_ << std::endl;}// 生成日常任务std::unique_ptr<QuestTemplate> createDailyQuest() const {auto dailyQuest = std::make_unique<QuestTemplate>(*this);dailyQuest->title_ = "日常:" + title_;dailyQuest->requiredLevel_ = 1; // 日常任务等级要求低std::cout << " 创建日常任务: " << dailyQuest->title_ << std::endl;return dailyQuest;}};// 高级原型管理器class AdvancedPrototypeManager {private:std::unordered_map<std::string, std::unique_ptr<Prototype>> prototypes_;std::unordered_map<std::string, std::vector<std::string>> categories_;public:void registerPrototype(const std::string& key, std::unique_ptr<Prototype> prototype, const std::string& category = "default") {prototypes_[key] = std::move(prototype);categories_[category].push_back(key);std::cout << " 注册原型到分类 '" << category << "': " << key << std::endl;}std::unique_ptr<Prototype> clonePrototype(const std::string& key) {auto it = prototypes_.find(key);if (it != prototypes_.end()) {return it->second->clone();}return nullptr;}// 按分类克隆std::vector<std::unique_ptr<Prototype>> cloneByCategory(const std::string& category) {std::vector<std::unique_ptr<Prototype>> clones;auto it = categories_.find(category);if (it != categories_.end()) {for (const auto& key : it->second) {auto clone = clonePrototype(key);if (clone) {clones.push_back(std::move(clone));}}}return clones;}// 获取分类列表std::vector<std::string> getCategories() const {std::vector<std::string> categoryList;for (const auto& pair : categories_) {categoryList.push_back(pair.first);}return categoryList;}// 显示分类内容void displayCategory(const std::string& category) const {auto it = categories_.find(category);if (it != categories_.end()) {std::cout << "\n 分类 '" << category << "' 中的原型:" << std::endl;for (const auto& key : it->second) {auto prototypeIt = prototypes_.find(key);if (prototypeIt != prototypes_.end()) {std::cout << "   " << key << " -> " << prototypeIt->second->getType() << std::endl;}}}}};// 原型工厂:基于原型的对象创建class PrototypeFactory {private:AdvancedPrototypeManager manager_;public:PrototypeFactory() {initializePrototypes();}void initializePrototypes() {// 初始化角色原型std::vector<std::string> warriorSkills = {"重击", "防御姿态", "旋风斩"};manager_.registerPrototype("warrior",std::make_unique<GameCharacter>("战士模板", "战士", 1, 100, 20, warriorSkills, "训练用剑"), "characters");std::vector<std::string> mageSkills = {"火球术", "寒冰箭", "魔法盾"};manager_.registerPrototype("mage",std::make_unique<GameCharacter>("法师模板", "法师", 1, 60, 100, mageSkills, "学徒法杖"), "characters");// 初始化武器原型std::vector<std::string> swordEnchantments = {"锋利", "耐久"};manager_.registerPrototype("fire_sword",std::make_unique<MagicWeapon>("火焰剑", "剑", 25, 15, "火", swordEnchantments), "weapons");std::vector<std::string> staffEnchantments = {"智慧", "魔力恢复"};manager_.registerPrototype("ice_staff",std::make_unique<MagicWeapon>("寒冰法杖", "法杖", 10, 30, "冰", staffEnchantments), "weapons");// 初始化怪物原型std::vector<std::string> goblinAbilities = {"投掷", "逃跑"};manager_.registerPrototype("goblin",std::make_unique<MonsterTemplate>("哥布林", "普通", 50, 8, 3, goblinAbilities, "普通掉落", 10), "monsters");std::vector<std::string> dragonAbilities = {"喷火", "飞行", "威压"};manager_.registerPrototype("dragon",std::make_unique<MonsterTemplate>("巨龙", "BOSS", 500, 50, 20, dragonAbilities, "稀有掉落", 1000), "monsters");// 初始化任务原型std::vector<std::string> killObjectives = {"杀死10只哥布林", "收集5个战利品"};std::vector<std::string> killRewards = {"100金币", "经验值200"};manager_.registerPrototype("goblin_hunt",std::make_unique<QuestTemplate>("哥布林讨伐", "狩猎", "清理哥布林营地", killObjectives, killRewards, 5, "起始森林"), "quests");}// 创建角色std::unique_ptr<GameCharacter> createCharacter(const std::string& type, const std::string& name) {auto prototype = manager_.clonePrototype(type);if (auto character = dynamic_cast<GameCharacter*>(prototype.get())) {prototype.release(); // 避免双重删除auto uniqueChar = std::unique_ptr<GameCharacter>(character);uniqueChar->customize(name);return uniqueChar;}return nullptr;}// 创建怪物军团std::vector<std::unique_ptr<MonsterTemplate>> createMonsterArmy(const std::string& type, int count) {std::vector<std::unique_ptr<MonsterTemplate>> army;for (int i = 0; i < count; ++i) {auto prototype = manager_.clonePrototype(type);if (auto monster = dynamic_cast<MonsterTemplate*>(prototype.get())) {prototype.release();auto uniqueMonster = std::unique_ptr<MonsterTemplate>(monster);uniqueMonster->customize(uniqueMonster->getType() + std::to_string(i + 1));army.push_back(std::move(uniqueMonster));}}return army;}// 显示所有可用原型void displayAvailablePrototypes() {std::cout << "\n 可用的原型分类:" << std::endl;auto categories = manager_.getCategories();for (const auto& category : categories) {manager_.displayCategory(category);}}};

原型模式的招式解析

招式一:深拷贝与浅拷贝
// 演示深拷贝问题的类
class ShallowCopyExample {
private:
std::string* data_;
int size_;
public:
ShallowCopyExample(const std::string& data, int size) : size_(size) {
data_ = new std::string(data);
}
// 默认拷贝构造函数 - 浅拷贝(有问题)
ShallowCopyExample(const ShallowCopyExample& other)
: data_(other.data_), size_(other.size_) { // 错误:共享指针
}
// 正确的深拷贝构造函数
ShallowCopyExample(const ShallowCopyExample& other, bool deepCopy)
: size_(other.size_) {
if (deepCopy) {
data_ = new std::string(*other.data_); // 深拷贝:创建新字符串
} else {
data_ = other.data_; // 浅拷贝:共享字符串
}
}
~ShallowCopyExample() {
delete data_;
}
void display() const {
std::cout << "数据: " << (data_ ? *data_ : "空") << ", 大小: " << size_ << std::endl;
}
};
// 正确的原型实现示例
class CorrectPrototype {
private:
std::string name_;
std::vector<std::string>* items_; // 需要深拷贝的指针成员public:CorrectPrototype(const std::string& name, const std::vector<std::string>& items): name_(name) {items_ = new std::vector<std::string>(items); // 动态分配}// 拷贝构造函数 - 实现深拷贝CorrectPrototype(const CorrectPrototype& other): name_(other.name_) {items_ = new std::vector<std::string>(*other.items_); // 深拷贝向量}// 克隆方法std::unique_ptr<CorrectPrototype> clone() const {return std::make_unique<CorrectPrototype>(*this); // 使用拷贝构造函数}~CorrectPrototype() {delete items_;}void addItem(const std::string& item) {items_->push_back(item);}void display() const {std::cout << "名称: " << name_ << ", 物品: ";for (const auto& item : *items_) {std::cout << item << " ";}std::cout << std::endl;}};
招式二:原型注册表增强版
// 支持版本控制的原型管理器
class VersionedPrototypeManager {
private:
std::unordered_map<std::string, std::vector<std::unique_ptr<Prototype>>> versionedPrototypes_;std::unordered_map<std::string, int> currentVersions_;public:void registerPrototype(const std::string& key, std::unique_ptr<Prototype> prototype, int version = 1) {if (versionedPrototypes_.find(key) == versionedPrototypes_.end()) {versionedPrototypes_[key] = std::vector<std::unique_ptr<Prototype>>();}// 确保有足够的空间if (versionedPrototypes_[key].size() < static_cast<size_t>(version)) {versionedPrototypes_[key].resize(version);}versionedPrototypes_[key][version - 1] = std::move(prototype);currentVersions_[key] = version;std::cout << " 注册原型 " << key << " 版本 " << version << std::endl;}std::unique_ptr<Prototype> clonePrototype(const std::string& key, int version = -1) {auto it = versionedPrototypes_.find(key);if (it != versionedPrototypes_.end()) {int ver = (version == -1) ? currentVersions_[key] : version;if (ver > 0 && ver <= static_cast<int>(it->second.size()) && it->second[ver - 1]) {return it->second[ver - 1]->clone();}}return nullptr;}// 升级原型版本void upgradePrototype(const std::string& key, std::unique_ptr<Prototype> newVersion) {int newVersionNumber = currentVersions_[key] + 1;registerPrototype(key, std::move(newVersion), newVersionNumber);std::cout << "⬆️ 升级原型 " << key << " 到版本 " << newVersionNumber << std::endl;}// 获取所有版本信息void displayVersionInfo(const std::string& key) const {auto it = versionedPrototypes_.find(key);if (it != versionedPrototypes_.end()) {std::cout << "\n 原型 " << key << " 的版本信息:" << std::endl;for (size_t i = 0; i < it->second.size(); ++i) {if (it->second[i]) {std::cout << "  版本 " << (i + 1) << ": " << it->second[i]->getType()<< " (当前: " << ((i + 1) == static_cast<size_t>(currentVersions_.at(key)) ? "是" : "否") << ")" << std::endl;}}}}};

完整测试代码

// 测试原型模式
void testPrototypePattern() {
std::cout << "=== 原型模式测试开始 ===" << std::endl;
// 测试基础原型功能
std::cout << "\n--- 基础原型功能测试 ---" << std::endl;
// 创建原始角色
std::vector<std::string> skills = {"火球术", "治疗术", "传送"};auto originalMage = std::make_unique<GameCharacter>("大法师", "法师", 50, 200, 500, skills, "传说法杖");std::cout << "原始角色:" << std::endl;originalMage->display();// 克隆角色auto clonedMage = originalMage->clone();std::cout << "\n克隆后的角色:" << std::endl;if (clonedMage) {clonedMage->customize("克隆法师");clonedMage->setLevel(1);clonedMage->display();}// 测试原型管理器std::cout << "\n--- 原型管理器测试 ---" << std::endl;PrototypeManager manager;// 注册一些原型std::vector<std::string> warriorSkills = {"冲锋", "斩击",格挡"};manager.registerPrototype("basic_warrior",std::make_unique<GameCharacter>("基础战士", "战士", 1, 100, 30, warriorSkills, "铁剑"));std::vector<std::string> bowEnchantments = {"精准", "速射"};manager.registerPrototype("hunters_bow",std::make_unique<MagicWeapon>("猎人弓", "弓", 18, 5, "自然", bowEnchantments));// 显示所有原型manager.displayAllPrototypes();// 克隆多个对象std::cout << "\n--- 批量克隆测试 ---" << std::endl;auto warriorClones = manager.cloneMultiple("basic_warrior", 3);for (size_t i = 0; i < warriorClones.size(); ++i) {warriorClones[i]->customize("战士副本" + std::to_string(i + 1));warriorClones[i]->display();}// 测试高级原型系统std::cout << "\n--- 高级原型系统测试 ---" << std::endl;PrototypeFactory factory;factory.displayAvailablePrototypes();// 创建自定义角色auto customWarrior = factory.createCharacter("warrior", "我的战士");if (customWarrior) {customWarrior->setLevel(10);customWarrior->addSkill("狂暴");customWarrior->display();}// 创建怪物军团std::cout << "\n--- 怪物军团测试 ---" << std::endl;auto goblinArmy = factory.createMonsterArmy("goblin", 5);for (const auto& goblin : goblinArmy) {goblin->display();}// 测试版本控制原型管理器std::cout << "\n--- 版本控制原型测试 ---" << std::endl;VersionedPrototypeManager versionManager;// 注册不同版本的武器std::vector<std::string> v1Enchantments = {"基础锋利"};versionManager.registerPrototype("magic_sword",std::make_unique<MagicWeapon>("魔法剑v1", "剑", 20, 10, "无", v1Enchantments), 1);std::vector<std::string> v2Enchantments = {"高级锋利", "火焰"};versionManager.registerPrototype("magic_sword",std::make_unique<MagicWeapon>("魔法剑v2", "剑", 25, 15, "火", v2Enchantments), 2);// 显示版本信息versionManager.displayVersionInfo("magic_sword");// 克隆不同版本auto v1Sword = versionManager.clonePrototype("magic_sword", 1);auto v2Sword = versionManager.clonePrototype("magic_sword", 2);auto currentSword = versionManager.clonePrototype("magic_sword");std::cout << "\n版本1武器:" << std::endl;if (v1Sword) v1Sword->display();std::cout << "\n版本2武器:" << std::endl;if (v2Sword) v2Sword->display();std::cout << "\n当前版本武器:" << std::endl;if (currentSword) currentSword->display();std::cout << "\n=== 原型模式测试结束 ===" << std::endl;}// 实战应用:游戏对象复制系统class GameObjectDuplicationSystem {private:AdvancedPrototypeManager manager_;public:GameObjectDuplicationSystem() {setupPrototypes();}void setupPrototypes() {// 设置各种游戏对象原型std::vector<std::string> npcDialogs = {"你好,旅行者!", "需要帮助吗?", "小心前方的怪物!"};// 在实际游戏中,这些原型可以从配置文件加载}// 快速生成游戏场景void generateGameScene(const std::string& sceneType) {std::cout << "\n 生成游戏场景: " << sceneType << std::endl;if (sceneType == "forest") {generateForestScene();} else if (sceneType == "dungeon") {generateDungeonScene();} else if (sceneType == "town") {generateTownScene();}}private:void generateForestScene() {std::cout << " 生成森林场景..." << std::endl;// 生成树木、动物、NPC等auto forestProps = manager_.cloneByCategory("forest_props");std::cout << "✅ 生成 " << forestProps.size() << " 个森林道具" << std::endl;}void generateDungeonScene() {std::cout << " 生成地下城场景..." << std::endl;// 生成怪物、宝藏、陷阱等auto dungeonProps = manager_.cloneByCategory("dungeon_props");std::cout << "✅ 生成 " << dungeonProps.size() << " 个地下城道具" << std::endl;}void generateTownScene() {std::cout << " 生成城镇场景..." << std::endl;// 生成NPC、建筑、商店等auto townProps = manager_.cloneByCategory("town_props");std::cout << "✅ 生成 " << townProps.size() << " 个城镇道具" << std::endl;}};int main() {testPrototypePattern();// 运行游戏对象复制系统示例std::cout << "\n 游戏对象复制系统示例:" << std::endl;GameObjectDuplicationSystem gameSystem;gameSystem.generateGameScene("forest");gameSystem.generateGameScene("dungeon");gameSystem.generateGameScene("town");return 0;}

原型模式的武学心得

适用场景
优点
  • 性能提升:通过克隆避免重复的初始化过程
  • 简化创建:隐藏对象创建的复杂性
  • 动态性:可以在运行时动态添加或删除原型
  • 减少子类:不需要为每种对象类型创建子类
  • 状态保存:可以保存和恢复对象状态
缺点

武林高手的点评

Builder 赞叹道:“Prototype 兄的克隆大法确实精妙!在需要快速创建相似对象时,这种方法确实比我的逐步构建更加高效。”

Abstract Factory 也点头称赞:“Prototype 兄的方法在对象创建成本高昂时特别有用。我的抽象工厂需要知道所有产品类型,而原型模式可以在运行时动态添加新类型。”

Prototype 谦虚回应:“诸位过奖了。每个模式都有其适用场景。在需要快速创建相似对象且创建成本较高时,我的原型模式确实能发挥重要作用。但在需要精确控制构建过程时,Builder 兄的方法更加合适。”

下章预告

在Prototype展示完他的克隆大法后,Adapter 忙碌地走出,他身上挂着的各种转接头叮当作响。

“Prototype 兄的克隆确实高效,但在软件江湖中,我们常常需要让不兼容的接口能够协同工作。” Adapter 笑着说道,“下一章,我将展示如何通过适配器模式让原本接口不兼容的类能够一起工作!”

架构老人满意地点头:“善!接口的兼容确实是软件集成中的重要问题。下一章,就请 Adapter 展示他的转接艺术!”


欲知 Adapter 如何通过适配器模式解决接口不兼容问题,且听下回分解!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/942276.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

XMLType 测试记录

XMLType 测试记录Posted on 2025-10-21 14:33 大势趋007 阅读(0) 评论(0) 收藏 举报XMLType 学习测试-- 创建表 CREATE TABLE employee_xml (emp_id NUMBER PRIMARY KEY,emp_data XMLType );-- 插入数据 INSERT I…

2025年氢氧化镁厂家推荐排行榜,矿石氢氧化镁,水镁石氢氧化镁,阻燃剂氢氧化镁,改性氢氧化镁源头企业实力解析

2025年氢氧化镁厂家推荐排行榜,矿石氢氧化镁,水镁石氢氧化镁,阻燃剂氢氧化镁,改性氢氧化镁源头企业实力解析 氢氧化镁作为一种重要的无机化工产品,在阻燃材料、环保处理、医药制备等领域具有广泛应用。随着全球对…

vue控制页面不能复制

<template> <div @contextmenu.prevent @copy.prevent @cut.prevent @paste.prevent class="no-select"> <!-- 页面内容 --> </div> </template> <style scoped> .no-s…

MyEMS:开启智能化能源管理新篇章

在全球能源危机与 “双碳” 目标的双重驱动下,能源管理的精细化、智能化成为各行各业的核心需求。MyEMS(My Energy Management System,我的能源管理系统)作为一款聚焦用户实际需求的能源管理工具,凭借灵活适配、功…

开源能源管理系统 MyEMS:赋能企业降本增效,加速能源数字化转型

在 “双碳” 目标与能源成本攀升的双重驱动下,企业对能源管理的需求从 “被动统计” 转向 “主动优化”。而开源能源管理系统 MyEMS(My Energy Management System)凭借零授权成本、高度可定制、社区协同迭代的优势,…

深入解析:LabVIEW超声换能器成像

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025南京鑫铭机械厂家推荐:精密钣金加工与天文台圆顶定制专家

2025南京鑫铭机械厂家推荐:精密钣金加工与天文台圆顶定制专家 在当今制造业快速发展的背景下,精密钣金加工与天文台圆顶定制领域面临着前所未有的技术挑战。随着工业4.0时代的到来,传统加工方式已难以满足高精度、高…

计算redis key落在那个slot

python:2.7# -*- coding: utf-8 -*- import binascii# 完整的CRC16 XMODEM查找表 crc16_table = [0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad,…

2025工业臭氧检测仪实力厂家推荐,逸云天电子专业提供多场景精准监测方案

2025工业臭氧检测仪实力厂家推荐,逸云天电子专业提供多场景精准监测方案 随着工业化和城市化的快速发展,臭氧污染问题日益凸显。工业臭氧检测仪作为环境监测的重要工具,在保障生产安全、维护环境质量方面发挥着关键…

FMC学习笔记

非常好,这个问题已经进入到 FMC 外部 SRAM 实战 的核心。下面我会帮你完整梳理:从 硬件连接 → 时序理解 → 软件配置 → 示例代码,让你能真正理解“FMC 外扩 SRAM”的全流程。前置概念:SRAM、DRAM FMC和FSMC http…

【2025】怎么加快百度网盘下载速度?

【2025】怎么加快百度网盘下载速度?怎么加快百度网盘下载速度?今天就教给大家一非常好用的绝招。这一招我也是好久没交给大家了。地址获取:放在这里了,可以直接获取大家看看这个下载速度是不是非常的羡慕。这也是我…

MyEMS:赋能高效能源管理的核心工具

在全球能源短缺与环保意识日益提升的背景下,如何实现能源的精细化管理、降低能耗成本、减少碳排放,成为企业与社会关注的焦点。MyEMS(Energy Management System,能源管理系统)作为一款专注于能源监测、分析与优化…

详细介绍:【亲测免费】 ESP32 BLE HID 鼠标键盘项目推荐

详细介绍:【亲测免费】 ESP32 BLE HID 鼠标键盘项目推荐pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas…

小球碰撞 trick

具体来说就是有若干个小球在数轴上走动,碰撞会方向相反,这个 trick 只适用于速度都一样的情况下。 我们只需要比较强限制的刻画出这个东西就已经胜利了,较为硬做的做法比较不讲道理,也不是很优美,这里分享一种优美…

2025 年最新推荐!国内优质充电桩厂家排行榜,助您精准挑选适配性强、安全高效的充电设备电车/智能/新能源/电动车充电桩厂家推荐

引言 随着新能源汽车普及率大幅提升,充电桩作为核心配套设施,市场需求呈爆发式增长,但行业乱象也随之凸显。部分产品存在充电效率低下、安全防护缺失、车型适配性差等问题,且售后服务参差不齐,导致用户在选购时难…

优惠券测试用例

设计一个淘宝优惠券功能模块的测试用例,需要从用户、商家和平台等多个角度,系统地考虑其功能、业务逻辑、性能、安全及兼容性。 一、 测试分析与策略 在设计具体用例前,先明确测试范围和策略:功能模块拆解:后台(…

2025工装定制实力厂家推荐:贵格服饰专注防静电工装与POLO衫定制

2025工装定制实力厂家推荐:贵格服饰专注防静电工装与POLO衫定制 技术挑战与行业痛点 在现代工业生产环境中,特种工装的技术要求日益提高。防静电工装作为电子制造、航空航天等领域的必备防护装备,面临着严峻的技术挑…

2025 年MES服务商最新推荐榜单:聚焦行业痛点,精选优质服务商助力企业数智化转型

引言 在离散制造行业数智化转型加速推进的当下,电子、家电、五金加工等细分领域企业,正面临生产流程复杂、多工厂协同难、质量追溯精度低、异常响应迟缓等核心痛点,这些问题直接导致生产效率停滞、制造成本高企。而…

2025 年物流公司最新推荐排行榜:覆盖多线路运输,精选优质品牌助您高效选择

引言 在当前经济快速发展、商品流通日益频繁的背景下,物流服务的质量与效率直接影响企业运营成本和个人需求满足度。然而,杭州及周边地区物流市场品牌众多,服务水平参差不齐,既有深耕行业多年的老牌企业,也有新兴…

windows本地jenkins打包部署远端服务

插件:Publish Over SSH配置:系统管理 找到SSH Servers 1.jenkins新建item: Freestyle project2.General->高级使用:自定义工作空间3.Build Steps配置构建: mvn clean package -Dmaven.test.skip=true4.构建后操…