DOTA版设计模式——原型

2023-10-16 21:30
文章标签 设计模式 原型 dota

本文主要是介绍DOTA版设计模式——原型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原型模式,说白了就是拷贝新对象,新对象是原来的对象一模一样。就像是孙悟空用毛吹出的N个孙悟空一样,他们都是一样的具有一样的状态。
UML图:

在C#中原型模式就是要实现Clone方法,完成对对象的拷贝,framework中已经有了这个接口我们不必去重新定义了(ICloneable)。
C#中分为深拷贝和浅拷贝,浅拷贝这样实现即可:
        public   object  Clone()
        {
            
return   this .MemberwiseClone();
        }

这样代码很简单,但是有一定弊端。C#中有两种类型,引用类型和值类型。引用类型是放在托管堆中的,值类型放在堆栈中。当执行 MemberwiseClone方法是返回的对象的确与原对象相同。但是两者却有很大的关联。对于值类型没有问题,而类中的引用类型成员返回的是同一对象,也就是说假如有个孙悟空类,类中包含的值类型没有问题,然而孙悟空类包含引用类型(外形类,自定义类型)。大家知道孙悟空会72般变化,那么假如我让N个孙悟空中的一个变为和尚,那么其他的孙悟空也会变为和尚,这是因为对于引用类型(外形类) MemberwiseClone传递的是引用,没有新建类。用C++语言来说就是传递给了新孙悟空对象了外形类指针,一个变化将引起多个变化。要想各个对象是互不关联的需要进行深拷贝。
测试代码:
            // 浅拷贝
            LandpyForm.Form.OutputResult( " 浅拷贝:MemberwiseClone " );
            DotaPatternLibrary.Protype.Hero hero 
=   new  DotaPatternLibrary.Protype.Terrorblade();
            LandpyForm.Form.OutputResult(
" 英雄名称: "   +  hero.Name);
            LandpyForm.Form.OutputResult(
" 英雄级别: "   +  hero.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        hero.RoleProperty.power,
                                        hero.RoleProperty.nimble,
                                        hero.RoleProperty.intelligence));
            DotaPatternLibrary.Protype.Hero heroNew 
=  hero.Clone()  as  DotaPatternLibrary.Protype.Hero;
            LandpyForm.Form.OutputResult(
" 复制英雄名称: "   +  heroNew.Name);
            LandpyForm.Form.OutputResult(
" 复制英雄级别: "   +  heroNew.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 复制英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroNew.RoleProperty.power,
                                        heroNew.RoleProperty.nimble,
                                        heroNew.RoleProperty.intelligence));

            LandpyForm.Form.OutputResult(
" 升级==> " );

            hero.Level 
+=   1 ;
            hero.RoleProperty.power 
+=   1.9m ;
            hero.RoleProperty.nimble 
+=   3.2m ;
            hero.RoleProperty.intelligence 
+=   1.75m ;
            LandpyForm.Form.OutputResult(
" 英雄名称: "   +  hero.Name);
            LandpyForm.Form.OutputResult(
" 英雄级别: "   +  hero.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        hero.RoleProperty.power,
                                        hero.RoleProperty.nimble,
                                        hero.RoleProperty.intelligence));
            LandpyForm.Form.OutputResult(
" 复制英雄名称: "   +  heroNew.Name);
            LandpyForm.Form.OutputResult(
" 复制英雄级别: "   +  heroNew.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 复制英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroNew.RoleProperty.power,
                                        heroNew.RoleProperty.nimble,
                                        heroNew.RoleProperty.intelligence));
            
// 深拷贝
            LandpyForm.Form.OutputResult( " 深拷贝:新对象 " );
            DotaPatternLibrary.Protype.HeroForDeepCopy heroDeep 
=   new  DotaPatternLibrary.Protype.Syllabear();
            LandpyForm.Form.OutputResult(
" 英雄名称: "   +  heroDeep.Name);
            LandpyForm.Form.OutputResult(
" 英雄级别: "   +  heroDeep.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroDeep.RoleProperty.power,
                                        heroDeep.RoleProperty.nimble,
                                        heroDeep.RoleProperty.intelligence));
            DotaPatternLibrary.Protype.HeroForDeepCopy heroNewDeep 
=  heroDeep.Clone()  as  DotaPatternLibrary.Protype.HeroForDeepCopy;
            LandpyForm.Form.OutputResult(
" 复制英雄名称: "   +  heroNewDeep.Name);
            LandpyForm.Form.OutputResult(
" 复制英雄级别: "   +  heroNewDeep.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 复制英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroNewDeep.RoleProperty.power,
                                        heroNewDeep.RoleProperty.nimble,
                                        heroNewDeep.RoleProperty.intelligence));

            LandpyForm.Form.OutputResult(
" 升级==> " );

            heroDeep.Level 
+=   1 ;
            heroDeep.RoleProperty.power 
+=   1.9m ;
            heroDeep.RoleProperty.nimble 
+=   3.2m ;
            heroDeep.RoleProperty.intelligence 
+=   1.75m ;
            LandpyForm.Form.OutputResult(
" 英雄名称: "   +  heroDeep.Name);
            LandpyForm.Form.OutputResult(
" 英雄级别: "   +  heroDeep.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroDeep.RoleProperty.power,
                                        heroDeep.RoleProperty.nimble,
                                        heroDeep.RoleProperty.intelligence));
            LandpyForm.Form.OutputResult(
" 复制英雄名称: "   +  heroNewDeep.Name);
            LandpyForm.Form.OutputResult(
" 复制英雄级别: "   +  heroNewDeep.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 复制英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroNewDeep.RoleProperty.power,
                                        heroNewDeep.RoleProperty.nimble,
                                        heroNewDeep.RoleProperty.intelligence));
            
// 深拷贝
            LandpyForm.Form.OutputResult( " 深拷贝:序列化 " );
            DotaPatternLibrary.Protype.HeroForSerializable heroSerializable 
=   new  DotaPatternLibrary.Protype.Puck();
            LandpyForm.Form.OutputResult(
" 英雄名称: "   +  heroSerializable.Name);
            LandpyForm.Form.OutputResult(
" 英雄级别: "   +  heroSerializable.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroSerializable.RoleProperty.power,
                                        heroSerializable.RoleProperty.nimble,
                                        heroSerializable.RoleProperty.intelligence));
            DotaPatternLibrary.Protype.HeroForSerializable heroNewSerializable 
=  heroSerializable.Clone()  as  DotaPatternLibrary.Protype.HeroForSerializable;
            LandpyForm.Form.OutputResult(
" 复制英雄名称: "   +  heroNewSerializable.Name);
            LandpyForm.Form.OutputResult(
" 复制英雄级别: "   +  heroNewSerializable.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 复制英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroNewSerializable.RoleProperty.power,
                                        heroNewSerializable.RoleProperty.nimble,
                                        heroNewSerializable.RoleProperty.intelligence));

            LandpyForm.Form.OutputResult(
" 升级==> " );

            heroSerializable.Level 
+=   1 ;
            heroSerializable.RoleProperty.power 
+=   1.9m ;
            heroSerializable.RoleProperty.nimble 
+=   3.2m ;
            heroSerializable.RoleProperty.intelligence 
+=   1.75m ;
            LandpyForm.Form.OutputResult(
" 英雄名称: "   +  heroSerializable.Name);
            LandpyForm.Form.OutputResult(
" 英雄级别: "   +  heroSerializable.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroSerializable.RoleProperty.power,
                                        heroSerializable.RoleProperty.nimble,
                                        heroSerializable.RoleProperty.intelligence));
            LandpyForm.Form.OutputResult(
" 复制英雄名称: "   +  heroNewSerializable.Name);
            LandpyForm.Form.OutputResult(
" 复制英雄级别: "   +  heroNewSerializable.Level);
            LandpyForm.Form.OutputResult(String.Format(
" 复制英雄属性---力量:{0},敏捷{1},智力{2} " ,
                                        heroNewSerializable.RoleProperty.power,
                                        heroNewSerializable.RoleProperty.nimble,
                                        heroNewSerializable.RoleProperty.intelligence));

完整代码:
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Runtime.Serialization;
using  System.Runtime.Serialization.Formatters.Binary;
using  System.IO;

using  DotaCommon;

namespace  DotaPatternLibrary.Protype
{
    
///   <summary>
    
///  角色属性
    
///   </summary>
    [Serializable]
    
public   class  RoleProperty
    {
        
public   decimal  power;
        
public   decimal  nimble;
        
public   decimal  intelligence;
    }

    
///   <summary>
    
///  英雄
    
///   </summary>
     public   abstract   class  Hero : ICloneable
    {
        
protected   int  _level;
        
protected   string  _name;
        
protected  RoleProperty _roleProperty;

        
public   string  Name
        {
            
get  {  return  _name; }
            
set  { _name  =  value; }
        }

        
public  RoleProperty RoleProperty
        {
            
get  {  return  _roleProperty; }
            
set  { _roleProperty  =  value; }
        }


        
public   int  Level
        {
            
get  {  return  _level; }
            
set  { _level  =  value; }
        }

        
public   object  Clone()
        {
            
return   this .MemberwiseClone();
        }
    }

    
///   <summary>
    
///  英雄
    
///   </summary>
     public   abstract   class  HeroForDeepCopy : ICloneable
    {
        
protected   int  _level;
        
protected   string  _name;
        
protected  RoleProperty _roleProperty;

        
public   string  Name
        {
            
get  {  return  _name; }
            
set  { _name  =  value; }
        }
        
public  RoleProperty RoleProperty
        {
            
get  {  return  _roleProperty; }
            
set  { _roleProperty  =  value; }
        }
        
public   int  Level
        {
            
get  {  return  _level; }
            
set  { _level  =  value; }
        }

        
public   abstract   object  Clone();
    }

    
///   <summary>
    
///  英雄
    
///   </summary>
    [Serializable]
    
public   abstract   class  HeroForSerializable : ICloneable
    {
        
protected   int  _level;
        
protected   string  _name;
        
protected  RoleProperty _roleProperty;

        
public   string  Name
        {
            
get  {  return  _name; }
            
set  { _name  =  value; }
        }
        
public  RoleProperty RoleProperty
        {
            
get  {  return  _roleProperty; }
            
set  { _roleProperty  =  value; }
        }
        
public   int  Level
        {
            
get  {  return  _level; }
            
set  { _level  =  value; }
        }

        
public   object  Clone()
        {
            
object  returnValue  =   null ;
            
using  (MemoryStream memoryStream  =   new  MemoryStream())
            {
                IFormatter formatter 
=   new  BinaryFormatter();
                formatter.Serialize(memoryStream, 
this );
                memoryStream.Seek(
0 , SeekOrigin.Begin);
                returnValue 
=  formatter.Deserialize(memoryStream);
            }
            
return  returnValue;
        }
    }

    
///   <summary>
    
///  灵魂守卫
    
///   </summary>
     public   class  Terrorblade : Hero
    {
        
public  Terrorblade()
        {
            
this ._level  =   1 ;
            
this ._name  =   " 灵魂守卫(Terrorblade) " ;
            RoleProperty roleProperty 
=   new  RoleProperty();
            roleProperty.power 
=   15 ;
            roleProperty.nimble 
=   22 ;
            roleProperty.intelligence 
=   15 ;
            
this ._roleProperty  =  roleProperty;
        }
    }

    
///   <summary>
    
///  德鲁伊
    
///   </summary>
     public   class  Syllabear : HeroForDeepCopy
    {
        
public  Syllabear()
        {
            
this ._level  =   1 ;
            
this ._name  =   " 德鲁伊(Syllabear) " ;
            RoleProperty roleProperty 
=   new  RoleProperty();
            roleProperty.power 
=   17 ;
            roleProperty.nimble 
=   24 ;
            roleProperty.intelligence 
=   13 ;
            
this ._roleProperty  =  roleProperty;
        }

        
public   override   object  Clone()
        {
            Syllabear syllabear 
=   new  Syllabear();
            syllabear.Name 
=  Name;
            syllabear.Level 
=  Level;
            RoleProperty roleProperty 
=   new  RoleProperty();
            roleProperty.intelligence 
=  RoleProperty.intelligence;
            roleProperty.nimble 
=  RoleProperty.nimble;
            roleProperty.power 
=  RoleProperty.power;
            syllabear.RoleProperty 
=  roleProperty;
            
return  syllabear;
        }
    }

    
///   <summary>
    
///  精灵龙
    
///   </summary>
    [Serializable]
    
public   class  Puck : HeroForSerializable
    {
        
public  Puck()
        {
            
this ._level  =   1 ;
            
this ._name  =   " 精灵龙(Puck) " ;
            RoleProperty roleProperty 
=   new  RoleProperty();
            roleProperty.power 
=   15 ;
            roleProperty.nimble 
=   22 ;
            roleProperty.intelligence 
=   25 ;
            
this ._roleProperty  =  roleProperty;
        }
    }
}

转载于:https://www.cnblogs.com/pangxiaoliang/archive/2009/07/26/1531574.html

这篇关于DOTA版设计模式——原型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/220921

相关文章

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

设计模式之工厂模式(通俗易懂--代码辅助理解【Java版】)

文章目录 1、工厂模式概述1)特点:2)主要角色:3)工作流程:4)优点5)缺点6)适用场景 2、简单工厂模式(静态工厂模式)1) 在简单工厂模式中,有三个主要角色:2) 简单工厂模式的优点包括:3) 简单工厂模式也有一些限制和考虑因素:4) 简单工厂模式适用场景:5) 简单工厂UML类图:6) 代码示例: 3、工厂方法模式1) 在工厂方法模式中,有4个主要角色:2) 工厂方法模式的工作流程

C#设计模式(1)——单例模式(讲解非常清楚)

一、引言 最近在学设计模式的一些内容,主要的参考书籍是《Head First 设计模式》,同时在学习过程中也查看了很多博客园中关于设计模式的一些文章的,在这里记录下我的一些学习笔记,一是为了帮助我更深入地理解设计模式,二同时可以给一些初学设计模式的朋友一些参考。首先我介绍的是设计模式中比较简单的一个模式——单例模式(因为这里只牵涉到一个类) 二、单例模式的介绍 说到单例模式,大家第一

漫谈设计模式 [12]:模板方法模式

引导性开场 菜鸟:老大,我最近在做一个项目,遇到了点麻烦。我们有很多相似的操作流程,但每个流程的细节又有些不同。我写了很多重复的代码,感觉很乱。你有啥好办法吗? 老鸟:嗯,听起来你遇到了典型的代码复用和维护问题。你有没有听说过“模板方法模式”? 菜鸟:模板方法模式?没听过。这是什么? 老鸟:简单来说,模板方法模式让你在一个方法中定义一个算法的骨架,而将一些步骤的实现延迟到子类中。这样,你可

漫谈设计模式 [9]:外观模式

引导性开场 菜鸟:老鸟,我最近在做一个项目,感觉代码越来越复杂,我都快看不懂了。尤其是有好几个子系统,它们之间的调用关系让我头疼。 老鸟:复杂的代码确实让人头疼。你有没有考虑过使用设计模式来简化你的代码结构? 菜鸟:设计模式?我听说过一些,但不太了解。你觉得我应该用哪个模式呢? 老鸟:听起来你的问题可能适合用**外观模式(Facade Pattern)**来解决。我们可以一起探讨一下。

设计模式大全和详解,含Python代码例子

若有不理解,可以问一下这几个免费的AI网站 https://ai-to.cn/chathttp://m6z.cn/6arKdNhttp://m6z.cn/6b1quhhttp://m6z.cn/6wVAQGhttp://m6z.cn/63vlPw 下面是设计模式的简要介绍和 Python 代码示例,涵盖主要的创建型、结构型和行为型模式。 一、创建型模式 1. 单例模式 (Singleton

PMP–一、二、三模–分类–14.敏捷–技巧–原型MVP

文章目录 技巧一模14.敏捷--原型法--项目生命周期--迭代型生命周期,通过连续的原型或概念验证来改进产品或成果。每个新的原型都能带来新的干系人新的反馈和团队见解。题目中明确提到需要反馈,因此原型法比较好用。23、 [单选] 一个敏捷团队的任务是开发一款机器人。项目经理希望确保在机器人被实际建造之前,团队能够收到关于需求的早期反馈并相应地调整设计。项目经理应该使用以下哪一项来实现这个目标?

漫谈设计模式 [6]:适配器模式

引导性开场 菜鸟:老鸟,我最近在项目中遇到一个问题,我们的系统需要集成一个新的第三方库,但这个库的接口和我们现有的代码完全不兼容。我该怎么办? 老鸟:这是个常见的问题,很多开发者都会遇到这种情况。你有没有听说过适配器模式? 菜鸟:适配器模式?没有,能详细说说吗? 老鸟:当然可以!这就是我们今天要讨论的主题。适配器模式是一个设计模式,可以帮助我们解决你现在遇到的问题。 渐进式介绍概念 老

2 观察者模式(设计模式笔记)

2 观察者模式(别名:发布-订阅) 概念 定义对象间的一种一对多的依赖关系,当一个对象状态发生变化时,所以依赖于它的对象都得到通知并被自动更新。 模式的结构与使用 角色 主题(Subject)观察者(Observer)具体主题(ConcreteSubject)具体观察者(ConcreteObserver) 结构 Subject依赖于Observer最重要!!! package