4. Creational Patterns
Abstract Factory Creates an instance
of several families of classes
Builder Separates object construction
from its representation
Factory Method Creates an instance
of several derived classes
Prototype A fully initialized instance to
be copied or cloned
Singleton A class of which only a
single instance can exist
5. Structural Patterns
Adapter Match interfaces of different classes
Bridge Separates an object’s interface from
its implementation
Composite A tree structure of simple and
composite objects
Decorator Add responsibilities to objects
dynamically
Façade A single class that represents an
entire subsystem
Flyweight A fine-grained instance used for
efficient sharing
Proxy An object representing another
object
6. Behavioral Patterns
Chain of Resp. A way of passing a request
between a chain of objects
Command Encapsulate a command request
as an object
Interpreter A way to include language
elements in a program
Iterator Sequentially access the elements of a
collection
Mediator Defines simplified communication
between classes
Memento Capture and restore an object's
internal state
7. Behavioral Patterns(continue)
Observer A way of notifying change to
a number of classes
State Alter an object's behavior when
its state changes
Strategy Encapsulates an algorithm
inside a class
Template Method Defer the exact
steps of an algorithm to a subclass
Visitor Defines a new operation to a
class without change
11. Sample Code
using System;
namespace DoFactory.GangOfFour.Proxy.Structural
{
/// <summary>
/// MainApp startup class for Structural
/// Proxy Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
static void Main()
{
// Create proxy and request a service
Proxy proxy = new Proxy();
proxy.Request();
// Wait for user
Console.ReadKey();
}
}
20. Use of Static Proxy
Situation: when do add, save the result
to DB
public double Add(double x, double y)
{
var result = _math.Add(x, y);
// Implement the method to save the
result to db
save(result);
return result;
}
21. Static Proxy
Advantage: easy to revise the original
method
Disadvantage: just only revise the
method that use the declared proxy
23. Dynamic Proxy(Impl)
Public Object Invoke(Object proxy,
System.Reflection.MethodInfo method,
Object[] parameters)
{
Object retVal = null;
// is invoked, otherwise an exception is thrown indicating they
// do not have permission
if ( SecurityManager.IsMethodInRole( userRole, method.Name ) )
{
// The actual method is invoked
retVal = method.Invoke( obj, parameters );
} else
{
throw new IllegalSecurityException( "Invalid permission to invoke " +
method.Name );
}
return retVal;
}
24. Dynamic Proxy(Impl)
Public Object Invoke(Object proxy,
System.Reflection.MethodInfo method,
Object[] parameters)
{
Object retVal = null;
// is invoked, otherwise an exception is thrown indicating they
// do not have permission
if ( SecurityManager.IsMethodInRole( userRole, method.Name ) )
{
// The actual method is invoked
retVal = method.Invoke( obj, parameters );
} else
{
throw new IllegalSecurityException( "Invalid permission to invoke " +
method.Name );
}
return retVal;
}
25. Use of Dynamic Proxy
public interface ITest
{
void TestFunctionOne();
Object TestFunctionTwo( Object a, Object b );
}
public class TestImpl : ITest
{
public void TestFunctionOne()
{
Console.WriteLine( "In TestImpl.TestFunctionOne()" );
}
public Object TestFunctionTwo( Object a, Object b )
{
Console.WriteLine( "In TestImpl.TestFunctionTwo( Object a, Object b
)" );
return null;
}
26. Use of Dynamic
Proxy(continue)
public class TestBed
{
static void Main( string[] args )
{
ITest test = (ITest)SecurityProxy.NewInstance( new
TestImpl() );
test.TestFunctionOne();
test.TestFunctionTwo( new Object(), new Object() );
}
}