理解:元素是固定的,作用于元素的状态是可以变化的。就好比一日三餐(元素)是固定的,但是每一餐吃的食物(状态)又是可以不断变化的。
代码:
using System.Windows.Forms;
namespace DesignMode.Visitor
{
//不变的元素
public class Breakfast
{
public Breakfast()
{
_name = "早餐";
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Have(Food food)
{
food.HaveBreakfast(this);
}
}
public class Lunch
{
public Lunch()
{
_name = "午餐";
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Have(Food food)
{
food.HaveLunch(this);
}
}
public class Dinner
{
public Dinner()
{
_name = "晚餐";
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Have(Food food)
{
food.HaveDinner(this);
}
}
//抽象变化点
public abstract class Food
{
//把每一个不变的元素都要考虑进去
public abstract void HaveBreakfast(Breakfast breakfast);
public abstract void HaveLunch(Lunch lunch);
public abstract void HaveDinner(Dinner dinner);
}
public class Noodle : Food
{
public override void HaveBreakfast(Breakfast breakfast)
{
MessageBox.Show(breakfast.Name + "吃面条");
}
public override void HaveLunch(Lunch lunch)
{
MessageBox.Show(lunch.Name + "吃面条");
}
public override void HaveDinner(Dinner dinner)
{
MessageBox.Show(dinner.Name + "吃面条");
}
}
public class Rice : Food
{
public override void HaveBreakfast(Breakfast breakfast)
{
MessageBox.Show(breakfast.Name + "吃大米");
}
public override void HaveLunch(Lunch lunch)
{
MessageBox.Show(lunch.Name + "吃大米");
}
public override void HaveDinner(Dinner dinner)
{
MessageBox.Show(dinner.Name + "吃大米");
}
}
public class KFC : Food
{
public override void HaveBreakfast(Breakfast breakfast)
{
MessageBox.Show(breakfast.Name + "吃KFC");
}
public override void HaveLunch(Lunch lunch)
{
MessageBox.Show(lunch.Name + "吃KFC");
}
public override void HaveDinner(Dinner dinner)
{
MessageBox.Show(dinner.Name + "吃KFC");
}
}
}
namespace DesignMode.Visitor
{
//不变的元素
public class Breakfast
{
public Breakfast()
{
_name = "早餐";
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Have(Food food)
{
food.HaveBreakfast(this);
}
}
public class Lunch
{
public Lunch()
{
_name = "午餐";
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Have(Food food)
{
food.HaveLunch(this);
}
}
public class Dinner
{
public Dinner()
{
_name = "晚餐";
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Have(Food food)
{
food.HaveDinner(this);
}
}
//抽象变化点
public abstract class Food
{
//把每一个不变的元素都要考虑进去
public abstract void HaveBreakfast(Breakfast breakfast);
public abstract void HaveLunch(Lunch lunch);
public abstract void HaveDinner(Dinner dinner);
}
public class Noodle : Food
{
public override void HaveBreakfast(Breakfast breakfast)
{
MessageBox.Show(breakfast.Name + "吃面条");
}
public override void HaveLunch(Lunch lunch)
{
MessageBox.Show(lunch.Name + "吃面条");
}
public override void HaveDinner(Dinner dinner)
{
MessageBox.Show(dinner.Name + "吃面条");
}
}
public class Rice : Food
{
public override void HaveBreakfast(Breakfast breakfast)
{
MessageBox.Show(breakfast.Name + "吃大米");
}
public override void HaveLunch(Lunch lunch)
{
MessageBox.Show(lunch.Name + "吃大米");
}
public override void HaveDinner(Dinner dinner)
{
MessageBox.Show(dinner.Name + "吃大米");
}
}
public class KFC : Food
{
public override void HaveBreakfast(Breakfast breakfast)
{
MessageBox.Show(breakfast.Name + "吃KFC");
}
public override void HaveLunch(Lunch lunch)
{
MessageBox.Show(lunch.Name + "吃KFC");
}
public override void HaveDinner(Dinner dinner)
{
MessageBox.Show(dinner.Name + "吃KFC");
}
}
}
客户端代码:
private void btn_Visitor_Click(object sender, EventArgs e)
{
Noodle noodle = new Noodle();
Rice rice = new Rice();
KFC kfc = new KFC();
Breakfast breakfast = new Breakfast();
breakfast.Have(noodle);
Lunch lunch = new Lunch();
lunch.Have(kfc);
lunch.Have(rice);
Dinner dinner = new Dinner();
dinner.Have(rice);
dinner.Have(noodle);
}