DOTA版设计模式——模板方法
模板方法比较简单,实际上就是在一个抽象类中写N个方法,其中包括abstract方法和已经实现的方法。模板方法就是已经实现的方法,而abstract方法是交由子类实现的。
UML图:
本文中的例子是创建Dota模型的例子,英雄和等级低的野怪有什么区别?区别在英雄有技能,等级低的野怪没有技能。下面会是创建模型的过程:
CreateModel(创建3D模型)->SetSkill(设置技能)->SetPosition(设置位置)->Maps(贴图)。
可以看出,除了设置技能外其他的过程都是公用的,所以抽象出模板方法BuildModel,SetSkill方法在子类中实现:
LandpyForm.Form.OutputResult("Hero");
DotaPatternLibrary.TemplateMethod.Model model = new DotaPatternLibrary.TemplateMethod.Hero();
model.BuildModel();
LandpyForm.Form.OutputResult("BotWildNPC");
model = new DotaPatternLibrary.TemplateMethod.SmallBotWildNPC();
model.BuildModel();
完整代码不用贴出,上边的代码均已包括。
以下是变形模板方法,数组排序,包括Framework实现和自己的实现:
UML图:
本文中的例子是创建Dota模型的例子,英雄和等级低的野怪有什么区别?区别在英雄有技能,等级低的野怪没有技能。下面会是创建模型的过程:
CreateModel(创建3D模型)->SetSkill(设置技能)->SetPosition(设置位置)->Maps(贴图)。
可以看出,除了设置技能外其他的过程都是公用的,所以抽象出模板方法BuildModel,SetSkill方法在子类中实现:
public abstract class Model
{
public void CreateModel()
{
LandpyForm.Form.OutputResult("CreateModel");
}
public abstract void SetSkill();
public void SetPosition()
{
LandpyForm.Form.OutputResult("SetPosition");
}
public void Maps()
{
LandpyForm.Form.OutputResult("Maps");
}
//模板方法
public void BuildModel()
{
CreateModel();
SetSkill();
SetPosition();
Maps();
}
}
SetSkill方法由于英雄和等级低的野怪不同所以由子类来实现:{
public void CreateModel()
{
LandpyForm.Form.OutputResult("CreateModel");
}
public abstract void SetSkill();
public void SetPosition()
{
LandpyForm.Form.OutputResult("SetPosition");
}
public void Maps()
{
LandpyForm.Form.OutputResult("Maps");
}
//模板方法
public void BuildModel()
{
CreateModel();
SetSkill();
SetPosition();
Maps();
}
}
public class Hero : Model
{
public override void SetSkill()
{
LandpyForm.Form.OutputResult("SetSkill");
}
}
public class SmallBotWildNPC : Model
{
public override void SetSkill()
{
LandpyForm.Form.OutputResult("NoSkill");
}
}
测试代码:{
public override void SetSkill()
{
LandpyForm.Form.OutputResult("SetSkill");
}
}
public class SmallBotWildNPC : Model
{
public override void SetSkill()
{
LandpyForm.Form.OutputResult("NoSkill");
}
}
LandpyForm.Form.OutputResult("Hero");
DotaPatternLibrary.TemplateMethod.Model model = new DotaPatternLibrary.TemplateMethod.Hero();
model.BuildModel();
LandpyForm.Form.OutputResult("BotWildNPC");
model = new DotaPatternLibrary.TemplateMethod.SmallBotWildNPC();
model.BuildModel();
完整代码不用贴出,上边的代码均已包括。
以下是变形模板方法,数组排序,包括Framework实现和自己的实现:
using System;
class Starter
{
static void Main()
{
//Person personPxl = new Person("pxl", 27);
//Person personLjc = new Person("ljc", 26);
//Person personWwh = new Person("wwh", 25);
//Person personYkl = new Person("ykl", 24);
//Person[] persons = { personWwh, personPxl, personLjc, personYkl };
//Array.Sort(persons);
//foreach (Person person in persons)
//{
// Console.WriteLine("Name:{0}-Age:{1}", person.Name, person.Age);
//}
LandpyPerson personPxl = new LandpyPerson("pxl", 27);
LandpyPerson personLjc = new LandpyPerson("ljc", 26);
LandpyPerson personWwh = new LandpyPerson("wwh", 25);
LandpyPerson personYkl = new LandpyPerson("ykl", 24);
LandpyPerson[] persons = { personWwh, personPxl, personLjc, personYkl };
LandpyArray<LandpyPerson>.Sort(persons);
foreach (LandpyPerson person in persons)
{
Console.WriteLine("Name:{0}-Age:{1}", person.Name, person.Age);
}
Console.ReadKey();
}
}
class Person : IComparable<Person>
{
private string _name;
private int _age;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public Person(string name, int age)
{
_name = name;
_age = age;
}
public int CompareTo(Person other)
{
//调用Int的CompareTo方法
//return this.Age.CompareTo(other.Age);
//自己写Int的CompareTo方法
if (this.Age < other.Age)
{
return -1;
}
else if (this.Age > other.Age)
{
return 1;
}
else
{
return 0;
}
}
}
interface ILandpyComparable<T>
{
int CompareTo(T t);
}
class LandpyPerson : ILandpyComparable<LandpyPerson>
{
private string _name;
private int _age;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public LandpyPerson(string name, int age)
{
_name = name;
_age = age;
}
public LandpyPerson()
{
}
/// <summary>
/// 1为大于;0为等于;-1为小于
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(LandpyPerson other)
{
if (this.Age > other.Age)
{
return 1;
}
else if (this.Age < other.Age)
{
return -1;
}
else
{
return 0;
}
}
}
class LandpyArray<T>
{
public static void Sort(T[] comparers)
{
int tmp;
//冒泡法排序
for (int step = 0; step < comparers.Length - 1; step++)
{
for (int index = 0; index < comparers.Length - 1 - step; index++)
{
tmp = (comparers[index] as ILandpyComparable<T>).CompareTo(comparers[index + 1]);
if (tmp == 1)
{
T t = Activator.CreateInstance<T>();
t = comparers[index];
comparers[index] = comparers[index + 1];
comparers[index + 1] = t;
}
}
}
}
}
class Starter
{
static void Main()
{
//Person personPxl = new Person("pxl", 27);
//Person personLjc = new Person("ljc", 26);
//Person personWwh = new Person("wwh", 25);
//Person personYkl = new Person("ykl", 24);
//Person[] persons = { personWwh, personPxl, personLjc, personYkl };
//Array.Sort(persons);
//foreach (Person person in persons)
//{
// Console.WriteLine("Name:{0}-Age:{1}", person.Name, person.Age);
//}
LandpyPerson personPxl = new LandpyPerson("pxl", 27);
LandpyPerson personLjc = new LandpyPerson("ljc", 26);
LandpyPerson personWwh = new LandpyPerson("wwh", 25);
LandpyPerson personYkl = new LandpyPerson("ykl", 24);
LandpyPerson[] persons = { personWwh, personPxl, personLjc, personYkl };
LandpyArray<LandpyPerson>.Sort(persons);
foreach (LandpyPerson person in persons)
{
Console.WriteLine("Name:{0}-Age:{1}", person.Name, person.Age);
}
Console.ReadKey();
}
}
class Person : IComparable<Person>
{
private string _name;
private int _age;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public Person(string name, int age)
{
_name = name;
_age = age;
}
public int CompareTo(Person other)
{
//调用Int的CompareTo方法
//return this.Age.CompareTo(other.Age);
//自己写Int的CompareTo方法
if (this.Age < other.Age)
{
return -1;
}
else if (this.Age > other.Age)
{
return 1;
}
else
{
return 0;
}
}
}
interface ILandpyComparable<T>
{
int CompareTo(T t);
}
class LandpyPerson : ILandpyComparable<LandpyPerson>
{
private string _name;
private int _age;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public LandpyPerson(string name, int age)
{
_name = name;
_age = age;
}
public LandpyPerson()
{
}
/// <summary>
/// 1为大于;0为等于;-1为小于
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(LandpyPerson other)
{
if (this.Age > other.Age)
{
return 1;
}
else if (this.Age < other.Age)
{
return -1;
}
else
{
return 0;
}
}
}
class LandpyArray<T>
{
public static void Sort(T[] comparers)
{
int tmp;
//冒泡法排序
for (int step = 0; step < comparers.Length - 1; step++)
{
for (int index = 0; index < comparers.Length - 1 - step; index++)
{
tmp = (comparers[index] as ILandpyComparable<T>).CompareTo(comparers[index + 1]);
if (tmp == 1)
{
T t = Activator.CreateInstance<T>();
t = comparers[index];
comparers[index] = comparers[index + 1];
comparers[index + 1] = t;
}
}
}
}
}