Design Patterns
كل سنة وأنتم طيبين :)
Agenda
Behavioral Pattern
- Command
Review
- Factory
- Singleton
- Adapter
- Composite
- Observer
- State
- Strategy
- MVC
GraFCIS Design Discussion
Command Pattern
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Command Pattern Example
UML Diagram

let's Implement it
// Command pattern -- Structural example
using System;
namespace DoFactory.GangOfFour.Command.Structural
{
/// <summary>
/// MainApp startup class for Structural
/// Command Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
static void Main()
{
// Create receiver, command, and invoker
Receiver receiver = new Receiver();
Command command = new ConcreteCommand(receiver);
Invoker invoker = new Invoker();
// Set and execute command
invoker.SetCommand(command);
invoker.ExecuteCommand();
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'Command' abstract class
/// </summary>
abstract class Command
{
protected Receiver receiver;
// Constructor
public Command(Receiver receiver)
{
this.receiver = receiver;
}
public abstract void Execute();
}
/// <summary>
/// The 'ConcreteCommand' class
/// </summary>
class ConcreteCommand : Command
{
// Constructor
public ConcreteCommand(Receiver receiver) :
base(receiver)
{
}
public override void Execute()
{
receiver.Action();
}
}
/// <summary>
/// The 'Receiver' class
/// </summary>
class Receiver
{
public void Action()
{
Console.WriteLine("Called Receiver.Action()");
}
}
/// <summary>
/// The 'Invoker' class
/// </summary>
class Invoker
{
private Command _command;
public void SetCommand(Command command)
{
this._command = command;
}
public void ExecuteCommand()
{
_command.Execute();
}
}
}When to Use
- Specify, queue, and execute requests at different times.
- Support Undoable operations.
- Load and Store.
Review
Factory:
- Define an interface for creating an object, but let subclasses decide which class to instantiate.
When To Use:
- A class can't anticipate the class of object must create.
- A class wants its subclasses to specify the objects it creates.
Review
Singleton:
- Ensure a class has only one instance and provide a global point of access to it.
When To Use:
- There must be exactly one instance of a class, and it must be accessible to the clients from a well-known access point.
Review
Composite:
- Compose objects into tree structures to represent part-whole hierarchies.
When To Use:
- You want to represent part-whole hierarchies of objects.
- you want clients to be able to ignore the difference between compositions of objects and individual objects.
Review
State:
- Allow an object to alter its behavior when its internal state changes. The object will appear to change its class
When To Use:
- An object's behavior depends on its state, and it must change its behavior at run-time depending on the state.
- Operations have large, multipart conditional statements that depend on the object's state.
REVIEW
Strategy:
- Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
When To Use:
- Many related classes differ only in their behavior.
GraFCIS
Demo.

Any Questions ?!
Thank You :)
The End
presented by Anas A. Ismail / _aaismail
Design Patterns[Command Pattern, Review, Project Discussion]
By anas_awad
Design Patterns[Command Pattern, Review, Project Discussion]
The last Session in Design Pattern's Summer Course, FCIS'13
- 953