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");
}
}
}