C#设计模式 笔记(一)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Ani_Sports
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Cat cat = new Cat("Kaisir");
cat.ShoutNum = 100;
MessageBox.Show(cat.Shout());
}
private void button2_Click(object sender, EventArgs e)
{
Dog dog = new Dog("Micky");
dog.ShoutNum = 2;
MessageBox.Show(dog.Shout());
}
private Animal[] arrayAnimal;
//动物报名按钮
private void button4_Click(object sender, EventArgs e)
{
arrayAnimal = new Animal[5];
arrayAnimal[0] = new Cat("cat1");
arrayAnimal[1] = new Dog("dog1");
arrayAnimal[2] = new Cattle("cattle1");
arrayAnimal[3] = new Cat("cat2");
arrayAnimal[4] = new Sheep("sheep1");
}
private void button3_Click(object sender, EventArgs e)
{
foreach (Animal tmp in arrayAnimal)
{
MessageBox.Show(tmp.Shout());
}
}
private void button5_Click(object sender, EventArgs e)
{
MachineCat mact = new MachineCat("叮当");
StoneMonkey wukong = new StoneMonkey("孙悟空");
IChange[] array = new IChange[2];
array[0] = mact;
array[1] = wukong;
MessageBox.Show(array[0].ChangeThing("各种各样的东西"));
MessageBox.Show(array[1].ChangeThing("各种各样的东西"));
}
}
//接口
interface IChange
{
string ChangeThing(string thing);
}
//父类-动物类
abstract class Animal
{
protected string;
protected int shoutNum = 3;
//ShortNum方法
public int ShoutNum
{
get
{
return shoutNum;
}
set
{
if (value <=10)
shoutNum = value;
else
shoutNum = 10;
}
}
//构造函数
public Animal(string name)
{
this.name = name;
}
//构造函数重构
public Animal()
{
this.name = "未命名";
}
//getShoutSound方法
protected abstract string getShoutSound();
//Shout方法
public  string Shout()
{
string result = "";
for (int i = 0; i < shoutNum; i++)
result += getShoutSound();
return("我的名字叫" + name + " " + result);
}
}
//继承Animal的Cat
class Cat : Animal
{
public Cat()
: base()
{ }
public Cat(string name)
: base(name)
{ }
protected override string getShoutSound()
{
return "喵 ";
}
/*        public override string Shout()
{
string result = "";
for (int i = 0; i < shoutNum; i++)
result += "喵 ";
return "我的名字叫" + name + " " + result;
}
*/
}
//继承Animal的Dog
class Dog : Animal
{
public Dog()
: base()
{ }
public Dog(string name)
: base(name)
{ }
protected override string getShoutSound()
{
return "汪 ";
}
/*        public override string Shout()
{
string result="";
for(int i=0;i< shoutNum; i++)
result += "咩 ";
return ("我的名字叫" + name + " " + result);
}
*/
}
class Cattle : Animal
{
public Cattle()
: base()
{ }
public Cattle(string name)
: base(name)
{ }
protected override string getShoutSound()
{
return "哞 ";
}
/*       public override string Shout()
{
string result = "";
for (int i = 0; i < shoutNum; i++)
result += "哞 ";
return ("我的名字叫" + name + " " + result);
}
*/
}
class Monkey : Animal
{
public Monkey()
: base()
{ }
public Monkey(string name)
: base(name)
{ }
protected override string getShoutSound()
{
return "吱 ";
}
}
class MachineCat : Cat, IChange
{
public MachineCat()
: base()
{ }
public MachineCat(string name)
: base(name)
{ }
public string ChangeThing(string thing)
{
return base.Shout() + "我有万能的口袋 我可以变出:" + thing;
}
}
class StoneMonkey : Monkey, IChange
{
public StoneMonkey()
: base()
{ }
public StoneMonkey(string name)
: base(name)
{ }
public string ChangeThing(string thing)
{
return base.Shout() + "我可以七十二变 我可以变成" + thing;
}
}
}