源于个人网站:http://programmer.2008red.com/programmer/article_647_777_1.shtml
收到一封来自Exoweb的邮件,让去面试(败的很惨,呵呵。过了好长时间了,都忘记这事了,结果这公司来了邮件,说不能提供给我职位,还安慰我。不过就不过是了,弄这些做什么,害我出了一肚子火),就查了查这个公司。以前去面试过的前辈提到了他们会让我们设计医院,学校之类的东东,觉得应该先自己尝试做一下,就做了!
病人Patient分别在各个部门挂号,交费,然后检查身体,具体的动作由各个部门去做(operation)。
病人与部门分开,使用组合/聚合关系,个人觉得有点桥梁模式的意思!
假如将来医院增加新的部门,只要在Patient和Department下增加单层的子类就可以了。
具体代码:
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace Hospital { delegate void UpdateDelegate(); abstract class Patient { protected string name; public string Name { get { return this.name; } set { this.name = value; } } } class Register:Patient { } class Payer:Patient { } class Inspector:Patient { } interface Department { void Operation(); } class Registe:Department { public Patient patient; public Registe(Patient register) { this.patient = register; } public void Operation() { Console.WriteLine(this.patient.Name + "已登记!"); } } class Pay:Department { public Patient patient; public Pay(Patient payer) { this.patient = payer; } public void Operation() { Console.WriteLine(this.patient.Name+"已交钱!"); } } class Inspecte:Department { public Patient patient; public Inspecte(Patient inspector) { this.patient = inspector; } public Patient Patients { get { return this.patient; } set { this.patient = value; } } public void Operation() { Console.WriteLine(this.patient.Name + "已治疗!"); } } class Treatment { public Department[] dm=new Department[3]; public Treatment(Patient d1,Patient d2,Patient d3) { dm[0]=new Registe(d1); dm[1] = new Pay(d2); dm[2] = new Inspecte(d3); } public void run() { dm[0].Operation(); dm[1].Operation(); dm[2].Operation(); } } class Program { static void Main(string[] args) { string name = "zhong"; Register r1 = new Register(); Payer p1 = new Payer(); Inspector i1 = new Inspector(); r1.Name = name; p1.Name = name; i1.Name = name; Treatment tm = new Treatment(r1,p1,i1); tm.run(); Console.ReadLine(); } } } |
挂号,交费,检查可以设置成一个观察者模式,在C#里当然是用委托来做最好了。
还可以设置很多病人,排队来看病。这可以用一个Arraylist或哈希表来存储各个病人的信息,用循环输出。
由于这是我写的第一个有关模式的文章,时间上也有点紧,不但上面的想法没去实现,可能还有一些我不
知道的错误缺点列在其中,希望大家原谅并指正。
希望自己能尽快实现这些没完成的想法。
和宿舍的人出去吃了个饭,今天端午,呵呵,晚上十二点回到宿舍后一点也不想睡,又开始写了起来,呵呵!
下面为用一个ArrayList记录病人的修改。
{ *** class Treatment { public Department[] dm=new Department[3]; public ArrayList list = new ArrayList(); public void show(string name) { Register r1 = new Register(); Payer p1 = new Payer(); Inspector i1 = new Inspector(); r1.Name = name; p1.Name = name; i1.Name = name; dm[0] = new Registe(r1); dm[1] = new Pay(p1); dm[2] = new Inspecte(i1); dm[0].Operation(); dm[1].Operation(); dm[2].Operation(); } public void add(string name) { list.Add(name); } public void remove(string name) { list.Remove(name); } public void run() { foreach (string uh in list) { show(uh); } } } class Program { static void Main(string[] args) { Treatment tm = new Treatment(); string name = "zhong"; tm.add(name); tm.add("liu"); // tm.remove("zhong"); tm.run(); Console.ReadLine(); } } } |
这块是使用委托处理的程序:
{ *** class Subject { public event UpdateDelegate UpdateHandler; public void add(UpdateDelegate ud) { UpdateHandler += ud; } public void remove(UpdateDelegate ud) { UpdateHandler -= ud; } public void run() { if (UpdateHandler != null) UpdateHandler(); } } class Treatment { public Department[] dm=new Department[3]; public ArrayList list = new ArrayList(); public Treatment(string name) { Register r1 = new Register(); Payer p1 = new Payer(); Inspector i1 = new Inspector(); r1.Name = name; p1.Name = name; i1.Name = name; dm[0] = new Registe(r1); dm[1] = new Pay(p1); dm[2] = new Inspecte(i1); } public void show() { dm[0].Operation(); dm[1].Operation(); dm[2].Operation(); } } class Program { static void Main(string[] args) { Treatment t1 = new Treatment("zhong"); Treatment t2 = new Treatment("liu"); Subject sj = new Subject(); sj.add(new UpdateDelegate(t1.show)); sj.add(new UpdateDelegate(t2.show)); // sj.remove(new UpdateDelegate(t1.show)); sj.run(); Console.ReadLine(); } } } |
分别实现了ArrayList和委托来处理多人的情况。也算是运用了观察者模式了。