表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
2:类图
3:代码
namespace Bll.Visitor
{
//实体
public class EntityMessage
{
private string msg;
private DateTime dateTime;
public EntityMessage()
{
}
public string MSG
{
get { return msg; }
set { msg = value; }
}
public DateTime DateTime
{
get { return dateTime; }
set { dateTime = value; }
}
}
//抽象访问者
public abstract class AbstractVistor
{
public abstract string Vistor(AbstractElement abstractElement);
}
//抽象元素
public abstract class AbstractElement
{
public abstract string Accept(AbstractVistor abstractVistor);
}
//操作Message抽象类
public abstract class Message : AbstractElement
{
private EntityMessage entityMessage;
public Message(EntityMessage message)
{
this.entityMessage = message;
}
public override string Accept(AbstractVistor abstractVistor)
{
return abstractVistor.Vistor(this);
}
public abstract IList<EntityMessage> Get();
public abstract bool Insert();
}
//SQL方式操作Message
public class SqlMessage : Message
{
public SqlMessage(EntityMessage eMsg)
: base(eMsg)
{ }
public override IList<EntityMessage> Get()
{
List<EntityMessage> list = new List<EntityMessage>();
list.Add(new EntityMessage());
return list;
}
public override bool Insert()
{
return true;
}
}
//Oracle方式操作Message
public class OracleMessage : Message
{
public OracleMessage(EntityMessage eMsg)
: base(eMsg)
{ }
public override IList<EntityMessage> Get()
{
List<EntityMessage> list = new List<EntityMessage>();
list.Add(new EntityMessage());
return list;
}
public override bool Insert()
{
return true;
}
}
//Insert访问者
public class InsertVisitor : AbstractVistor
{
public override string Vistor(AbstractElement abstractElement)
{
Message message = abstractElement as Message;
return message.Insert().ToString() + "<br />";
}
}
//Get访问者
public class GetVisitor : AbstractVistor
{
public override string Vistor(AbstractElement abstractElement)
{
Message message = abstractElement as Message;
return message.Insert().ToString() + "<br />";
}
}
//Message集合
public class Messages
{
private List<Message> list = new List<Message>();
//添加一个新元素
public void Attach(Message message)
{
list.Add(message);
}
public void Detach(Message message)
{
list.Remove(message);
}
public string Visitor(AbstractVistor vidtor)
{
string msg = string.Empty;
foreach (Message m in list)
{
msg += m.Accept(vidtor);
}
return msg;
}
}
}
//调用
Messages m = new Messages(); m.Attach(new SqlMessage(new EntityMessage()));
m.Attach(new OracleMessage(new EntityMessage()));
{
//实体
public class EntityMessage
{
private string msg;
private DateTime dateTime;
public EntityMessage()
{
}
public string MSG
{
get { return msg; }
set { msg = value; }
}
public DateTime DateTime
{
get { return dateTime; }
set { dateTime = value; }
}
}
//抽象访问者
public abstract class AbstractVistor
{
public abstract string Vistor(AbstractElement abstractElement);
}
//抽象元素
public abstract class AbstractElement
{
public abstract string Accept(AbstractVistor abstractVistor);
}
//操作Message抽象类
public abstract class Message : AbstractElement
{
private EntityMessage entityMessage;
public Message(EntityMessage message)
{
this.entityMessage = message;
}
public override string Accept(AbstractVistor abstractVistor)
{
return abstractVistor.Vistor(this);
}
public abstract IList<EntityMessage> Get();
public abstract bool Insert();
}
//SQL方式操作Message
public class SqlMessage : Message
{
public SqlMessage(EntityMessage eMsg)
: base(eMsg)
{ }
public override IList<EntityMessage> Get()
{
List<EntityMessage> list = new List<EntityMessage>();
list.Add(new EntityMessage());
return list;
}
public override bool Insert()
{
return true;
}
}
//Oracle方式操作Message
public class OracleMessage : Message
{
public OracleMessage(EntityMessage eMsg)
: base(eMsg)
{ }
public override IList<EntityMessage> Get()
{
List<EntityMessage> list = new List<EntityMessage>();
list.Add(new EntityMessage());
return list;
}
public override bool Insert()
{
return true;
}
}
//Insert访问者
public class InsertVisitor : AbstractVistor
{
public override string Vistor(AbstractElement abstractElement)
{
Message message = abstractElement as Message;
return message.Insert().ToString() + "<br />";
}
}
//Get访问者
public class GetVisitor : AbstractVistor
{
public override string Vistor(AbstractElement abstractElement)
{
Message message = abstractElement as Message;
return message.Insert().ToString() + "<br />";
}
}
//Message集合
public class Messages
{
private List<Message> list = new List<Message>();
//添加一个新元素
public void Attach(Message message)
{
list.Add(message);
}
public void Detach(Message message)
{
list.Remove(message);
}
public string Visitor(AbstractVistor vidtor)
{
string msg = string.Empty;
foreach (Message m in list)
{
msg += m.Accept(vidtor);
}
return msg;
}
}
}
//调用
Messages m = new Messages(); m.Attach(new SqlMessage(new EntityMessage()));
m.Attach(new OracleMessage(new EntityMessage()));