Ответы к экзамену 0
.4.pdfpublic Colleague(Mediator med) { this.mediator = med; this.isON = true;
}
}
class Mediator
{
Colleague colleague1, colleague2;
internal Colleague Colleague2
{
get { return colleague2; } set { colleague2 = value; }
}
internal Colleague Colleague1
{
get { return colleague1; } set { colleague1 = value; }
}
public void Send(Colleague colleague, bool value)
{
Colleague col = colleague == colleague1 ? colleague2 : colleague1; if (value)
{
col.Info = "My colleague doesn't sleep!";
}
else
{
col.Info = "My colleague is asleep";
}
}
}
class Program
{
static void Main(string[] args)
{
Mediator mediator = new Mediator(); Colleague col1 = new Colleague(mediator); Colleague col2 = new Colleague(mediator); mediator.Colleague1 = col1; mediator.Colleague2 = col2;
col1.IsON = true; col2.IsON = false;
Console.WriteLine(col1.Info); Console.WriteLine(col2.Info);
col1.IsON = false; col2.IsON = true;
Console.WriteLine(col1.Info); Console.WriteLine(col2.Info);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace Memento
{
public interface IBankAccount
{
object getSavePoint();
void setSavePoint(object point);
}
public class BankAccount : IBankAccount
{
public int numbLastTrans { get; set; } public int balance { get; set; }
public BankAccount(int NumbLastTrans = 0, int Balance = 0)
{
this.balance = Balance; this.numbLastTrans = NumbLastTrans;
}
public void Print()
{
Console.WriteLine("=======================");
Console.WriteLine("Последняя успешно завершенная транзакция: {0}", this.numbLastTrans); Console.WriteLine("Ваш баланс: {0}", this.balance); Console.WriteLine("=======================");
}
object IBankAccount.getSavePoint()
{
return new SavePoint { numbLastTrans = this.numbLastTrans, balance = this.balance };
}
void IBankAccount.setSavePoint(object savepoint)
{
if (Object.ReferenceEquals(savepoint, null))
throw new ArgumentNullException("savepoint is null"); if (!(savepoint is SavePoint))
throw new ArgumentException("savepoint is not SavePoint"); numbLastTrans = ((SavePoint)savepoint).numbLastTrans;
balance = ((SavePoint)savepoint).balance;
}
}
class SavePoint
{
public int numbLastTrans { get; set; } public int balance { get; set; }
}
public class Saver
{
private object savepoint;
public void SaveState(IBankAccount bankaccount)
{
if (bankaccount == null)
throw new ArgumentNullException("bankaccount"); savepoint = bankaccount.getSavePoint();
}
public void RestoreState(IBankAccount bankaccount)
{
if (bankaccount == null)
throw new ArgumentNullException("bankaccount"); if (savepoint == null)
throw new InvalidOperationException("bankaccount == null"); bankaccount.setSavePoint(savepoint);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Создан банковский счет"); BankAccount myAccount = new BankAccount(); Saver saver = new Saver(); saver.SaveState(myAccount); myAccount.Print();
Console.ReadKey();
Random rand = new Random(); int sum = 0, p = 0;
for (int i = 1; i <= 5; i++)
{
sum = rand.Next(10000) - 5000; myAccount.numbLastTrans = i; myAccount.balance += sum;
Console.WriteLine("Готовится к выполнению банковская операция");
Console.WriteLine("Номер операции : {0}, сумма : {1} руб.", i, sum);
Console.ReadKey();
p = rand.Next(10); if (p % 2 == 0)
{
Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Финансовая операция проведена успешно"); Console.ResetColor();
saver.SaveState(myAccount);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("При выполнении финансовой операции произошел сбой"); Console.ResetColor();
saver.RestoreState(myAccount); Console.WriteLine("Баланс после восстановления:");
}
myAccount.Print(); Console.ReadKey();
}
}
}
}
public class ConsoleTipsWriter : ITipsObserver
{
public ConsoleTipsWriter(TipsPublisher aPublisher)
{
mPublisher = aPublisher;
}
public void onNewTip()
{
Console.WriteLine("NEW TIP IS " + mPublisher.getTipOfTheDay());
}
TipsPublisher mPublisher;
}
public interface ITipsObserver
{
void onNewTip();
}
public class TipsBeeper : ITipsObserver
{
public void onNewTip()
{
Console.Beep();
}
}
public class TipsPublisher
{
public TipsPublisher()
{
mTipOfTheDay = "DEFAULT TIP";
}
public void subscribe(ITipsObserver aObserver)
{
mObservers.Add(aObserver);
}
public void unsubscribe(ITipsObserver aObserver)
{
mObservers.Remove(aObserver);
}
public String getTipOfTheDay()
{
return mTipOfTheDay;
}
public void setTipOfTheDay(String aTipOfTheDay)
{
mTipOfTheDay = aTipOfTheDay; notify();
}
public void notify()
{
foreach (ITipsObserver observer in mObservers)
{
observer.onNewTip();
}
}
String mTipOfTheDay;
List<ITipsObserver> mObservers = new List<ITipsObserver>();
}
class Program
{
static void Main(string[] args)
{
testObserver(); Console.ReadLine();
}
static void testObserver()
{
Console.Out.WriteLine(">>>>TEST OBSERVER<<<<<"); TipsPublisher publisher = new TipsPublisher();
ConsoleTipsWriter writer = new ConsoleTipsWriter(publisher); publisher.subscribe(writer);
publisher.setTipOfTheDay("DONT WORRY!"); System.Threading.Thread.Sleep(2000);
TipsBeeper beeper = new TipsBeeper(); publisher.subscribe(beeper); publisher.setTipOfTheDay("BE HAPPY!"); System.Threading.Thread.Sleep(2000);
publisher.unsubscribe(writer); publisher.setTipOfTheDay("BE HUGGIES!");
}
}
using System;
namespace StatePattern
{
class DKA
{
private IState state;
private IState stateS; private IState stateQ; private IState stateF;
public DKA()
{
stateS = new S(); stateQ = new Q(); stateF = new F(); state = stateS;
}
public bool check(string s)
{
state = stateS;
char[] symbols = s.ToCharArray();
foreach(char symbol in symbols) {
if (!Transition(Int32.Parse(symbol.ToString()))) return false;
}
bool result = state.Finish(); state = stateS;
return result;
}
public bool Transition(int s)
{
if (state is F) {
if (s >= 1 && s <= 9) { state = stateQ; return true;
}
return (s == 0);
}
if (state is Q) { if (s == 0) {
state = stateF; return true;
}
return false;
}
else // state is S
{
if (s >= 1 && s <= 9) { state = stateQ; return true;
}
return (s == 0);
}
}
}
interface IState
{
bool Finish();
}
abstract class State { }
class S : State, IState
{
public bool Finish()
{
return false;
}
}
class Q : State, IState
{
public bool Finish()
{
return false;
}
}
class F : State, IState
{
public bool Finish()
{
return true;
}
}
}