Beruflich Dokumente
Kultur Dokumente
Tutorial 9
Prepared by Wilson
Outline
Design Patterns
Creational Patterns
Structural Patterns
Behavioral Patterns
Project Phase 2
Design Patterns
HandleRequest() Receiver3
HandleRequest()
HandleRequest () HandleRequest ()
public abstract void HandleRequest(int request);
}
ConcreteCommand
Receiver receiver
Execute() receiver
Action() Action()
Example: delegate in C#
Design Patterns
Command Parameterize the command
by compositing different
// The abstract "Command“ class concrete receiver
abstract class Command
{
protected Receiver receiver;
public Command(Receiver receiver)
Command
{
this.receiver = receiver;
}
Execute()
public abstract void Execute();
}
// The "ConcreteCommand“ class
class ConcreteCommand : Command
{
public ConcreteCommand(Receiver receiver) : base(receiver)
{ }
ConcreteCommand
public override void Execute()
{
receiver.Action();
Execute() receiverAction()
}
}
Action is performed
for the compositing
receiver
Design Patterns
Command
// "Receiver"
class Receiver The method that Receiver
{ will finally be
public void Action()
{ called. Action()
Console.WriteLine(“Actual action to perform");
}
}
// "Invoker"
class Invoker
{
private Command command;
public void SetCommand(Command command)
{
this.command = command;
}
Invoker
public void ExecuteCommand()
{
command.Execute(); Accepting command
} and invoking command.
}
Design Patterns
Command
StoreCommand(aCommand)
Execute()
Action()
Design Patterns
Interpreter
Given a language, define a representation for
its grammar along with interpreter that uses
the representation to interpret sentences in the
language.
AbstractExpression
Interpret(Context)
TerminalExpression NonterminalExpression
Interpret(Context) Interpret(Context)
Design Patterns
Interpreter
// "AbstractExpression"
abstract class AbstractExpression
AbstractExpression
{
public abstract void Interpret(Context context); Interpret(Context)
}
// "TerminalExpression"
class TerminalExpression : AbstractExpression
{
public override void Interpret(Context context) TerminalExpression NonterminalExpression
{
Console.WriteLine("Called Terminal.Interpret()"); Interpret(Context) Interpret(Context)
}
}
// "NonterminalExpression"
class NonterminalExpression : AbstractExpression Should filled with interpret
{ operations with respect to
public override void Interpret(Context context)
{
the expression class
Console.WriteLine("Called Nonterminal.Interpret()");
}
}
}
Design Patterns
Iterator
Provide a way to access the elements of an
aggregate object sequentially without exposing
its underlying representation.
List ListIterator
First()
Next()
IsDone()
CurrentItem()
index
public override ListIterator CreateIterator() { // Constructor
return new ListIterator(this); public ListIterator(List aggregate) {
} this.aggregate = aggregate;
}
} The actual aggregate
instance public virtual object First() {….}
public virtual object Next() {….}
hides inside the iterator
public virtual object CurrentItem() {….}
public virtual bool IsDone() {….}
}
Mediator
Colleague
Design Patterns
Mediator
mediator
Mediator Colleague
ConcreteMediator
ConcreteColleague1
ConcreteColleague2
Design Patterns
Mediator
// "Mediator" public override void Send(string message,
abstract class Mediator Colleague colleague) Centralize control
{ { On objects interaction
public abstract void Send(string message, if (colleague == colleague1)
Colleague colleague); {
} colleague2.Notify(message);
}
// "ConcreteMediator" else
class ConcreteMediator : Mediator {
{ colleague1.Notify(message);
private ConcreteColleague1 colleague1; }
private ConcreteColleague2 colleague2; }
}
public ConcreteColleague1 Colleague1
{ Mediator
set{ colleague1 = value; }
} Reference is kept for
message forwarding
public ConcreteColleague2 Colleague2
{
set{ colleague2 = value; }
}
ConcreteMediator
Design Patterns
Mediator
public void Send(string message)
{
mediator.Send(message, this);
}
// "Colleague"
abstract class Colleague
public void Notify(string message)
{
{
protected Mediator mediator;
Console.WriteLine("Colleague1 gets message: "
+ message);
// Constructor
}
public Colleague(Mediator mediator)
}
{
this.mediator = mediator;
// "ConcreteColleague2"
}
class ConcreteColleague2 : Colleague
}
{…………..}
// "ConcreteColleague1"
class ConcreteColleague1 : Colleague
{ Colleague
// Constructor
public ConcreteColleague1(Mediator mediator)
: base(mediator)
{
}
ConcreteColleague1 ConcreteColleague2
Design Patterns
References
Gamma, Erich et al, “Design Patterns:
Elements of Reusable Object-Oriented
Software,” Addison-Wesley.
Design Patterns with C# sample code: http://
www.dofactory.com/Patterns/Patterns.aspx
Project Phase 2
You are required to implement:
Game controlling functions
Movement
Harpoon shooting