⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 GOF23种设计模式详细例子!附有详细的代码噢!
💻 CS
字号:
using System;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;

namespace PrototypeExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            Enemy enemyPrototype = new FootMan(5, 4, new Location(100, 200));
            GameScene gs = new GameScene();
            List<Enemy> enemyGroup = gs.CreateEnemyGroup(enemyPrototype);

            foreach (FootMan ft in enemyGroup)
            {
                ft.ShowInfo();
                ft.FootmanAttack();
            }
            Console.WriteLine(sw.ElapsedMilliseconds);
        }
    }

    class GameScene
    {
        public List<Enemy> CreateEnemyGroup(Enemy enemyPrototype)
        {
            List<Enemy> enemyGroup = new List<Enemy>();
            Enemy e1 = enemyPrototype.Clone(true);
            e1.Location.x = enemyPrototype.Location.x - 10;
            Enemy e2 = enemyPrototype.Clone(true);
            e2.Location.x = enemyPrototype.Location.x + 10;
            Enemy elite = enemyPrototype.Clone(true);
            elite.Power = enemyPrototype.Power * 2;
            elite.Speed = enemyPrototype.Speed * 2;
            elite.Location.x = enemyPrototype.Location.x;
            elite.Location.y = enemyPrototype.Location.y + 10;
            enemyGroup.Add(e1);
            enemyGroup.Add(e2);
            enemyGroup.Add(elite);
            return enemyGroup;
        }
    }

    [Serializable]
    class Location
    {
        public int x;
        public int y;

        public Location(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    [Serializable]
    abstract class Enemy
    {
        protected Location location;

        public Location Location
        {
            get { return location; }
            set { location = value; }
        }

        protected int power;

        public int Power
        {
            get { return power; }
            set { power = value; }
        }

        protected int speed;

        public int Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        public abstract Enemy Clone(bool isDeepCopy);
        public abstract void ShowInfo();

        public Enemy(int power, int speed, Location location)
        {
            Thread.Sleep(1000); // Construct method is assumed to be a high calc work.
            this.power = power;
            this.speed = speed;
            this.location = location;
        }
    }

    [Serializable]
    class FootMan : Enemy
    {
        private string model;

        public FootMan(int power, int speed, Location location)
            : base(power, speed, location)
        {
            model = "footman";
        }

        public override void ShowInfo()
        {
            Console.WriteLine("model:{0} power:{1} speed:{2} location:({3},{4})", model, power, speed, location.x, location.y);
        }

        public override Enemy Clone(bool isDeepCopy)
        {
            FootMan footman;
            if (isDeepCopy)
            {
                MemoryStream memoryStream = new MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(memoryStream, this);
                memoryStream.Position = 0;
                footman = (FootMan)formatter.Deserialize(memoryStream);
            }
            else
                footman = (FootMan)this.MemberwiseClone();
            return footman;
        }

        public void FootmanAttack()
        {
            Console.WriteLine("FootmanAttack");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -