【クリエイティブデザインパターン】C#デザインパターンのプロトタイプパターン

プロトタイプ パターンは、インスタンス化せずに既存のオブジェクトをコピーして新しいオブジェクトを作成する創造的なデザイン パターンです。既存のオブジェクトをブループリントとして使用して新しいオブジェクトを作成できるため、同様のオブジェクトの繰り返しの初期化が回避され、オブジェクト作成の効率が向上します。

ここであなたに質問です。
戦士、魔術師、射手などのさまざまなタイプのキャラクターを含むゲーム キャラクター ジェネレーター システムを設計しているとします。このシステムのキャラクタージェネレーターを設計するには、プロトタイプパターンを使用してください。文字ジェネレータには次の機能が必要です。

既存のキャラクター プロトタイプに基づいて新しいキャラクター オブジェクトを生成します。
さまざまなタイプのキャラクター オブジェクトには、名前、レベル、スキルなどのさまざまな属性があります。
ユーザーは、必要に応じてさまざまなタイプの役割を選択し、対応する役割オブジェクトを生成できます。
プロトタイプ モードを使用して、上記の要件に基づいてキャラクタ ジェネレータ システムを設計し、設計上のアイデアを簡単に説明してください。

コード:


// 角色原型接口
interface ICharacterPrototype
{
    
    
    ICharacterPrototype Clone();
    void ShowInfo();
}

// 战士角色原型
class Warrior : ICharacterPrototype
{
    
    
    public string Name {
    
     get; set; }
    public int Level {
    
     get; set; }
    public List<string> Skills {
    
     get; set; }

    public ICharacterPrototype Clone()
    {
    
    
        return (ICharacterPrototype)MemberwiseClone();
    }

    public void ShowInfo()
    {
    
    
        Console.WriteLine($"战士角色: {
      
      Name} (等级: {
      
      Level})");
        Console.WriteLine("技能列表:");
        foreach (string skill in Skills)
        {
    
    
            Console.WriteLine($" - {
      
      skill}");
        }
    }
}

// 法师角色原型
class Mage : ICharacterPrototype
{
    
    
    public string Name {
    
     get; set; }
    public int Level {
    
     get; set; }
    public List<string> Spells {
    
     get; set; }

    public ICharacterPrototype Clone()
    {
    
    
        return (ICharacterPrototype)MemberwiseClone();
    }

    public void ShowInfo()
    {
    
    
        Console.WriteLine($"法师角色: {
      
      Name} (等级: {
      
      Level})");
        Console.WriteLine("法术列表:");
        foreach (string spell in Spells)
        {
    
    
            Console.WriteLine($" - {
      
      spell}");
        }
    }
}

// 射手角色原型
class Archer : ICharacterPrototype
{
    
    
    public string Name {
    
     get; set; }
    public int Level {
    
     get; set; }
    public int Arrows {
    
     get; set; }

    public ICharacterPrototype Clone()
    {
    
    
        return (ICharacterPrototype)MemberwiseClone();
    }

    public void ShowInfo()
    {
    
    
        Console.WriteLine($"射手角色: {
      
      Name} (等级: {
      
      Level})");
        Console.WriteLine($"箭矢数量: {
      
      Arrows}");
    }
}

class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        // 初始化角色原型
        Warrior warriorPrototype = new Warrior
        {
    
    
            Name = "战士",
            Level = 10,
            Skills = new List<string> {
    
     "近身攻击", "重击" }
        };

        Mage magePrototype = new Mage
        {
    
    
            Name = "法师",
            Level = 8,
            Spells = new List<string> {
    
     "火球术", "闪电术" }
        };

        Archer archerPrototype = new Archer
        {
    
    
            Name = "射手",
            Level = 6,
            Arrows = 50
        };

        // 根据原型克隆生成新角色对象
        ICharacterPrototype warrior = warriorPrototype.Clone();
        ICharacterPrototype mage = magePrototype.Clone();
        ICharacterPrototype archer = archerPrototype.Clone();

        // 显示角色信息
        warrior.ShowInfo();
        mage.ShowInfo();
        archer.ShowInfo();
    }
}

这段代码中的 Clone() 方法是用于复制角色原型对象的方法。在这里使用了 MemberwiseClone() 方法来执行浅拷贝,即创建一个与原对象相同的新对象,并将原对象的值类型成员和引用类型成员的引用复制给新对象。

MemberwiseClone() 方法是 C# 中的内置方法,它会创建对象的浅表副本,即对于值类型成员,会直接复制其值;对于引用类型成员,只会复制引用,而不会创建新的对象。这意味着,如果原对象的引用类型成员发生了改变,克隆对象的对应成员也会受到影响。

需要注意的是,MemberwiseClone() 方法是浅拷贝,对于包含复杂对象的成员,可能需要实现自定义的深拷贝逻辑来确保对象的完全复制。

在这个示例中,由于角色原型的成员都是基本数据类型和字符串,因此浅拷贝已足够满足需求,并且使用简单方便。

おすすめ

転載: blog.csdn.net/csdn2990/article/details/132212812
おすすめ