1、设计模式作业三源代码江苏大学版本(1) 贷款 :模板方法模式using System;using System.Collections.Generic;using System.Linq;using System.Text;public abstract class examine/抽象模板类 public void FillItem1() Console.WriteLine(近来收支情况:); Console.WriteLine(Item1() + RMB); public void FillItem2() Console.Write(信用情况:); Console.WriteLine(It
2、em2(); public void FillItem3() Console.WriteLine(未来收支情况:); Console.WriteLine(Item3()+RMB); protected virtual string Item1() return ; protected virtual string Item2() return ; protected virtual string Item3() return ; public class CommonCustomer : examine protected override string Item1() return 月收入3
3、000 RMB,支出2000 ; protected override string Item2() return 良好; protected override string Item3() return 月收入5000 RMB,支出2500 ; public class CompanyCustomer : examine protected override string Item1() return 年收入1000万; protected override string Item2() return 良好; protected override string Item3() return
4、年收入1500万; namespace loan class Program static void Main(string args) Console.WriteLine(普通客户:); CommonCustomer s1 = new CommonCustomer(); s1.FillItem1(); s1.FillItem2(); s1.FillItem3(); System.Console.WriteLine(n); Console.WriteLine(企业客户:); CommonCustomer s2 = new CommonCustomer(); s2.FillItem1(); s2
5、.FillItem2(); s2.FillItem3(); (2) 天气预报:观察着模式using System;using System.Collections.Generic;using System.Linq;using System.Text;public abstract class Subject private List observers = new List(); public void attach(Observer o)/注册一个观察者 observers.Add(o); public void detach(Observer o) observers.Remove(o)
6、; public void notice() foreach (Observer observer in observers) observer.Update(); public abstract class Observer public abstract void Update(); public abstract void ShowDate();public class system : Subject private string weather; public string Weather get return weather; set weather = value; public
7、 class computer : Observer private string weather; private system sys; public computer(system sys) this.sys = sys; public override void Update() weather = sys.Weather; public override void ShowDate() Console.WriteLine(电脑上最新天气预报是:+weather); public class mobilephone : Observer private string weather1;
8、 private system sys1; public mobilephone(system sys) this.sys1 = sys; public override void Update() weather1 = sys1.Weather; public override void ShowDate() Console.WriteLine(手机上最新天气预报是: + weather1); public class Ipad : Observer private string weather2; private system sys2; public Ipad(system sys) t
9、his.sys2 = sys; public override void Update() weather2 = sys2.Weather; public override void ShowDate() Console.WriteLine(Ipad最新天气预报是: + weather2); namespace weatherforecast class Program static void Main(string args) system s = new system(); computer c = new computer(s); mobilephone m = new mobileph
10、one(s); Ipad pad = new Ipad(s); s.attach(c); s.attach(m); s.attach(pad); s.Weather = 今天晴转多云,最高温度26度,最低温度20度; s.notice(); c.ShowDate(); m.ShowDate(); pad.ShowDate(); (3) 天气预报:事件委托改写观察着模式using System;using System.Collections.Generic;using System.Linq;using System.Text;public delegate void NotifyEventH
11、andler(object sender);public class Weather public NotifyEventHandler NotifyEvent; private String _symbol; public Weather(String symbol) this._symbol = symbol; public void Update() OnNotifyChange(); public void OnNotifyChange() if (NotifyEvent != null) NotifyEvent(this); public String Symbol get retu
12、rn _symbol; public class Computer string _name; public Computer(string name) this._name = name; public void SendData(object obj) if (obj is Weather ) Weather weather= (Weather)obj; Console.WriteLine(0显示的天气是: 1 ,_name , weather.Symbol); namespace weather_delegate_ class Program static void Main(strin
13、g args) Weather weather = new Weather(今天晴转多云,最高温度26度,最低温度20度); Computer investor = new Computer(电脑); Computer investor1 = new Computer(手机); Computer investor2 = new Computer(Ipad); weather.NotifyEvent += new NotifyEventHandler(investor.SendData); weather.NotifyEvent += new NotifyEventHandler(investo
14、r1.SendData); weather.NotifyEvent += new NotifyEventHandler(investor2.SendData); weather.Update(); Console.ReadLine(); (4) 玉皇大帝下旨传美猴王上天:命令模式 using System;using System.Collections.Generic;using System.Linq;using System.Text;public abstract class imperial/Command protected monkey mk; public imperial(m
15、onkey m) this.mk = m; public abstract void Execute();public class report : imperial/ConcreteCommand public report(monkey m) : base(m) public override void Execute() mk.Fly(); public class taibai/Invoker private imperial imp; public void SetCommand(imperial im) Console.WriteLine(玉皇大帝下旨:传美猴王觐见!); this
16、.imp = im; public void ExecuteCommand() Console.WriteLine(传旨进行中); imp.Execute(); public class monkey/Reciver public void Fly() Console.WriteLine(美猴王接旨上天!玉皇大帝); namespace Command class Program static void Main(string args) monkey m = new monkey(); imperial imp = new report(m); taibai tb = new taibai(
17、); tb.SetCommand(imp); tb.ExecuteCommand(); (5) TCP Connection:状态模式 using System;using System.Collections.Generic;using System.Linq;using System.Text;public class TcpConnecion private TcpState state; public TcpConnecion(TcpState State) state = State; public void ChangeM(string st) state.ChangeM (thi
18、s,st); public bool ChangeState(TcpState ts) state=ts; return true; public abstract class TcpState public abstract void ChangeM(TcpConnecion tc, string st); public bool ChangeState(TcpConnecion tc,TcpState ts) tc.ChangeState(ts); return true; public class TCPEstablished:TcpState public override void
19、ChangeM(TcpConnecion tc, string st) if (st = Listen) Console.WriteLine(由Established状态变为Listen状态); TcpState es = new TCPEstablished(); ChangeState(tc,es); else if (st = Close) Console.WriteLine(由Established状态变为Close状态); TcpState cs = new TCPEstablished(); ChangeState(tc, cs); public class TCPListen :
20、 TcpState public override void ChangeM(TcpConnecion tc, string st) if (st = Established) Console.WriteLine(由Listen状态变为Established状态); TcpState cs = new TCPListen(); ChangeState(tc, cs); else if (st = Close) Console.WriteLine(由Listen状态变为Close状态); TcpState cs = new TCPListen(); ChangeState(tc, cs); pu
21、blic class TCPClosed : TcpState public override void ChangeM(TcpConnecion tc, string st) if (st = Established) Console.WriteLine(由Close状态变为Established状态); TcpState cs = new TCPClosed(); ChangeState(tc, cs); else if (st = Listen) Console.WriteLine(由Close状态变为Listen状态); TcpState cs = new TCPClosed(); C
22、hangeState(tc, cs); namespace TCPconnection class Program static void Main(string args) string st = Established; TcpState tcs= new TCPEstablished(); TcpConnecion tcn = new TcpConnecion(tcs); st = Listen; tcn.ChangeM(st); st = Close; tcn.ChangeM(st); Console.WriteLine(n); TcpState t2 = new TCPClosed(); TcpConnecion tc2 = new TcpConnecion(t2); string s2 = Close; s2 = Established; tc2.ChangeM(s2);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1