设计模式作业三源代码江苏大学版本.docx
《设计模式作业三源代码江苏大学版本.docx》由会员分享,可在线阅读,更多相关《设计模式作业三源代码江苏大学版本.docx(15页珍藏版)》请在冰豆网上搜索。
![设计模式作业三源代码江苏大学版本.docx](https://file1.bdocx.com/fileroot1/2022-11/23/66fe213b-5286-4b48-b3d7-4d9fbb84b51e/66fe213b-5286-4b48-b3d7-4d9fbb84b51e1.gif)
设计模式作业三源代码江苏大学版本
(1)贷款:
模板方法模式
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
publicabstractclassexamine//抽象模板类
{
publicvoidFillItem1()
{
Console.WriteLine("近来收支情况:
");
Console.WriteLine(Item1()+"RMB");
}
publicvoidFillItem2()
{
Console.Write("信用情况:
");
Console.WriteLine(Item2());
}
publicvoidFillItem3()
{
Console.WriteLine("未来收支情况:
");
Console.WriteLine(Item3()+"RMB");
}
protectedvirtualstringItem1()
{
return"";
}
protectedvirtualstringItem2()
{
return"";
}
protectedvirtualstringItem3()
{
return"";
}
}
publicclassCommonCustomer:
examine
{
protectedoverridestringItem1()
{
return"月收入3000RMB,支出2000";
}
protectedoverridestringItem2()
{
return"良好";
}
protectedoverridestringItem3()
{
return"月收入5000RMB,支出2500";
}
}
publicclassCompanyCustomer:
examine
{
protectedoverridestringItem1()
{
return"年收入1000万";
}
protectedoverridestringItem2()
{
return"良好";
}
protectedoverridestringItem3()
{
return"年收入1500万";
}
}
namespaceloan
{
classProgram
{
staticvoidMain(string[]args)
{
Console.WriteLine("普通客户:
");
CommonCustomers1=newCommonCustomer();
s1.FillItem1();
s1.FillItem2();
s1.FillItem3();
System.Console.WriteLine("\n");
Console.WriteLine("企业客户:
");
CommonCustomers2=newCommonCustomer();
s2.FillItem1();
s2.FillItem2();
s2.FillItem3();
}
}
}
(2)天气预报:
观察着模式
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
publicabstractclassSubject
{
privateListobservers=newList();
publicvoidattach(Observero)//注册一个观察者
{
observers.Add(o);
}
publicvoiddetach(Observero)
{
observers.Remove(o);
}
publicvoidnotice()
{
foreach(Observerobserverinobservers)
{
observer.Update();
}
}
}
publicabstractclassObserver
{
publicabstractvoidUpdate();
publicabstractvoidShowDate();
}
publicclasssystem:
Subject
{
privatestringweather;
publicstringWeather
{
get
{
returnweather;
}
set
{
weather=value;
}
}
}
publicclasscomputer:
Observer
{
privatestringweather;
privatesystemsys;
publiccomputer(systemsys)
{
this.sys=sys;
}
publicoverridevoidUpdate()
{
weather=sys.Weather;
}
publicoverridevoidShowDate()
{
Console.WriteLine("电脑上最新天气预报是:
"+weather);
}
}
publicclassmobilephone:
Observer
{
privatestringweather1;
privatesystemsys1;
publicmobilephone(systemsys)
{
this.sys1=sys;
}
publicoverridevoidUpdate()
{
weather1=sys1.Weather;
}
publicoverridevoidShowDate()
{
Console.WriteLine("手机上最新天气预报是:
"+weather1);
}
}
publicclassIpad:
Observer
{
privatestringweather2;
privatesystemsys2;
publicIpad(systemsys)
{
this.sys2=sys;
}
publicoverridevoidUpdate()
{
weather2=sys2.Weather;
}
publicoverridevoidShowDate()
{
Console.WriteLine("Ipad最新天气预报是:
"+weather2);
}
}
namespaceweatherforecast
{
classProgram
{
staticvoidMain(string[]args)
{
systems=newsystem();
computerc=newcomputer(s);
mobilephonem=newmobilephone(s);
Ipadpad=newIpad(s);
s.attach(c);
s.attach(m);
s.attach(pad);
s.Weather="今天晴转多云,最高温度26度,最低温度20度";
s.notice();
c.ShowDate();
m.ShowDate();
pad.ShowDate();
}
}
}
(3)天气预报:
事件委托改写观察着模式
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
publicdelegatevoidNotifyEventHandler(objectsender);
publicclassWeather
{
publicNotifyEventHandlerNotifyEvent;
privateString_symbol;
publicWeather(Stringsymbol)
{
this._symbol=symbol;
}
publicvoidUpdate()
{
OnNotifyChange();
}
publicvoidOnNotifyChange()
{
if(NotifyEvent!
=null)
{
NotifyEvent(this);
}
}
publicStringSymbol
{
get{return_symbol;}
}
}
publicclassComputer
{
string_name;
publicComputer(stringname)
{
this._name=name;
}
publicvoidSendData(objectobj)
{
if(objisWeather)
{
Weatherweather=(Weather)obj;
Console.WriteLine("{0}显示的天气是:
{1}",_name,weather.Symbol);
}
}
}
namespaceweather_delegate_
{
classProgram
{
staticvoidMain(string[]args)
{
Weatherweather=newWeather("今天晴转多云,最高温度26度,最低温度20度");
Computerinvestor=newComputer("电脑");
Computerinvestor1=newComputer("手机");
Computerinvestor2=newComputer("Ipad");
weather.NotifyEvent+=newNotifyEventHandler(investor.SendData);
weather.NotifyEvent+=newNotifyEventHandler(investor1.SendData);
weather.NotifyEvent+=newNotifyEventHandler(investor2.SendData);
weather.Update();
Console.ReadLine();
}
}
}
(4)玉皇大帝下旨传美猴王上天:
命令模式
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
publicabstractclassimperial//Command
{
protectedmonkeymk;
publicimperial(monkeym)
{
this.mk=m;
}
publicabstractvoidExecute();
}
publicclassreport:
imperial//ConcreteCommand
{
publicreport(monkeym):
base(m){}
publicoverridevoidExecute()
{
mk.Fly();
}
}
publicclasstaibai//Invoker
{
privateimperialimp;
publicvoidSetCommand(imperialim)
{
Console.WriteLine("玉皇大帝下旨:
传美猴王觐见!
");
this.imp=im;
}
publicvoidExecuteCommand()
{
Console.WriteLine("传旨进行中····");
imp.Execute();
}
}
publicclassmonkey//Reciver
{
publicvoidFly()
{
Console.WriteLine("美猴王接旨上天!
@玉皇大帝");
}
}
namespaceCommand
{
classProgram
{
staticvoidMain(string[]args)
{
monkeym=newmonkey();
imperialimp=newreport(m);
taibaitb=newtaibai();
tb.SetCommand(imp);
tb.ExecuteCommand();
}
}
}
(5)TCPConnection:
状态模式
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
publicclassTcpConnecion
{
privateTcpStatestate;
publicTcpConnecion(TcpStateState)
{
state=State;
}
publicvoidChangeM(stringst)
{
state.ChangeM(this,st);
}
publicboolChangeState(TcpStatets)
{
state=ts;
returntrue;
}
}
publicabstractclassTcpState
{
publicabstractvoidChangeM(TcpConneciontc,stringst);
publicboolChangeState(TcpConneciontc,TcpStatets)
{
tc.ChangeState(ts);
returntrue;
}
}
publicclassTCPEstablished:
TcpState
{
publicoverridevoidChangeM(TcpConneciontc,stringst)
{
if(st=="Listen")
{
Console.WriteLine("由Established状态变为Listen状态");
TcpStatees=newTCPEstablished();
ChangeState(tc,es);
}
elseif(st=="Close")
{
Console.WriteLine("由Established状态变为Close状态");
TcpStatecs=newTCPEstablished();
ChangeState(tc,cs);
}
}
}
publicclassTCPListen:
TcpState
{
publicoverridevoidChangeM(TcpConneciontc,stringst)
{
if(st=="Established")
{
Console.WriteLine("由Listen状态变为Established状态");
TcpStatecs=newTCPListen();
ChangeState(tc,cs);
}
elseif(st=="Close")
{
Console.WriteLine("由Listen状态变为Close状态");
TcpStatecs=newTCPListen();
ChangeState(tc,cs);
}
}
}
publicclassTCPClosed:
TcpState
{
publicoverridevoidChangeM(TcpConneciontc,stringst)
{
if(st=="Established")
{
Console.WriteLine("由Close状态变为Established状态");
TcpStatecs=newTCPClosed();
ChangeState(tc,cs);
}
elseif(st=="Listen")
{
Console.WriteLine("由Close状态变为Listen状态");
TcpStatecs=newTCPClosed();
ChangeState(tc,cs);
}
}
}
namespaceTCPconnection
{
classProgram
{
staticvoidMain(string[]args)
{
stringst="Established";
TcpStatetcs=newTCPEstablished();
TcpConneciontcn=newTcpConnecion(tcs);
st="Listen";
tcn.ChangeM(st);
st="Close";
tcn.ChangeM(st);
Console.WriteLine("\n");
TcpStatet2=newTCPClosed();
TcpConneciontc2=newTcpConnecion(t2);
strings2="Close";
s2="Established";
tc2.ChangeM(s2);
}
}
}