设计模式里面的深考坝(无废话设计模式)加计算代码运行时间语句
来源:互联网 发布:windows phone最新手机 编辑:程序博客网 时间:2024/05/21 12:49
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 enemyGroup = gs.CreateEnemyGroup(enemyPrototype);
foreach (FootMan ft in enemyGroup)
{
ft.ShowInfo();
ft.FootmanAttack();
}
Console.WriteLine(sw.ElapsedMilliseconds);
}
}
class GameScene
{
public List CreateEnemyGroup(Enemy enemyPrototype)
{
List enemyGroup = new List();
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");
}
}
}
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 enemyGroup = gs.CreateEnemyGroup(enemyPrototype);
foreach (FootMan ft in enemyGroup)
{
ft.ShowInfo();
ft.FootmanAttack();
}
Console.WriteLine(sw.ElapsedMilliseconds);
}
}
class GameScene
{
public List CreateEnemyGroup(Enemy enemyPrototype)
{
List enemyGroup = new List();
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");
}
}
}
- 设计模式里面的深考坝(无废话设计模式)加计算代码运行时间语句
- 无废话设计模式:开篇
- 无废话C#设计模式之一:开篇
- 无废话C#设计模式之一:开篇
- 无废话C#设计模式之一:开篇
- 无废话C#设计模式之一:开篇
- 无废话C#设计模式之一:开篇
- 无废话C#设计模式之一:开篇
- 无废话C#设计模式之二:Singleton
- 无废话C#设计模式之二:Singleton
- 无废话C#设计模式之三:Abstract Factory
- 无废话C#设计模式之二:Singleton
- 无废话C#设计模式之九:Proxy
- 无废话C#设计模式之二十一:Visitor
- 无废话C#设计模式之二十:Mediator
- 无废话C#设计模式之十八:Command
- 无废话C#设计模式之十七:Chain Of Resp.
- 无废话C#设计模式之十六:State
- Anti-SPAM.CN的ISCBL黑名单在Exchange Server 2003 SP2中的使用方法
- 利用压缩网页来提升网站浏览速度
- ViewState的用法
- MGX 8850 系列IP+ATM多服务交换机
- Asp.net 触发器入门
- 设计模式里面的深考坝(无废话设计模式)加计算代码运行时间语句
- Webservice中如何实现方法重载--(方法名同名时出现的问题)
- 页面打印问题
- PHP <<<EOT定界符的使用详解
- C#中常用的语句(非常棒)
- 理解collate Chinese_PRC_CI_AS NULL
- 深入研究java.lang.Runtime类
- asp.net动态生成TreeView
- log4net配置文件解析