Oct 23, 2011

C# and Design Patterns




© 2004 Microsoft Corporation. All rights reserved.
Figure 1 Singleton 
class Singleton
{
    private static Singleton singleton = null;
    public static Singleton Instance()
    {
        if (null == singleton)
            singleton = new Singleton();

        return singleton;
    }

    private Singleton()
    {
    }
}
Figure 2 Transfer Class 
class FileTransfer
{
    public virtual void Download(string url, byte[] data, int size)
    {
        // download the requested resource
    }

    public virtual void Upload(string url, byte[] data, int size)
    {
        // upload the requested resource
    }
}
Figure 3 Derive from FileTransfer 
// decorated file transfer
class Decorator : FileTransfer 
{
    private FileTransfer ft = new FileTransfer();

    private bool IsAccessAllowed(string url)
    {
        bool result = true;
        // determine if access to the 
        // requested URL should be granted
        return result;
    }

    private void LogAccess(string url)
    {
        // log URL, time, user identity, etc.
        // to some database
        Console.WriteLine("Logging access to {0}", url);
    }

    public override void Download(string url, byte[] data, int size)
    {
        if (!IsAccessAllowed(url))
            return;

        ft.Download(url, data, size);
        LogAccess(url);
    }

    // similarly for Upload()
}
Figure 4 LineClass Derives From Shape 
class Line : Shape
{
    private double x1, y1, x2, y2;

    public Line(double x1, double y1, 
                double x2, double y2)
    {
        this.x1 = x1; this.y1 = y1;
        this.x2 = x2; this.y2 = y2;
    }

    public void Draw()
    {
        // draw a line from (x1, y1) to (x2, y2)
    }
}
Figure 5 Drawing Class 
class Drawing : Shape
{
    private ArrayList shapes;

    public Drawing()
    {
        shapes = new ArrayList();
    }

    public void Add(Shape s)
    {
        shapes.Add(s);
    }

    public void Draw()
    {
        IEnumerator enumerator = shapes.GetEnumerator();
        while (enumerator.MoveNext())
            ((Shape) enumerator.Current).Draw();
    }
}
Figure 6 State Class 
abstract class State
{
    public virtual void AddNickel(VendingMachine vm)     
     { }
    public virtual void AddDime(VendingMachine vm)       
     { }
    public virtual void AddQuarter(VendingMachine vm)    
     { }

    protected virtual void ChangeState(VendingMachine vm, State s)
    {
        vm.ChangeState(s);
    }
}
Figure 7 State Implementations 
class Start : State
{
    private static State state = new Start();
    private Start()
    {
    }

    public static State Instance()
    {
        // singleton logic
        Console.WriteLine("Credit: 0c");
        return state;
    }

    public override void AddNickel(VendingMachine vm)
    {
        ChangeState(vm, Five.Instance());
    }

    public override void AddDime(VendingMachine vm)
    {
        ChangeState(vm, Ten.Instance());
    }

    public override void AddQuarter(VendingMachine vm)
    {
        vm.Vend();    // start over
    }
}

class Five : State
{
    private static State state = new Five();
    private Five()
    {
    }
    
    public static State Instance()
    {
        // singleton logic
        Console.WriteLine("Credit: 5c");
        return state;
    }

    public override void AddNickel(VendingMachine vm)
    {
        ChangeState(vm, Ten.Instance());
    }

    public override void AddDime(VendingMachine vm)
    {
        ChangeState(vm, Fifteen.Instance());
    }

    public override void AddQuarter(VendingMachine vm)
    {
        vm.Vend();
        ChangeState(vm, Start.Instance());    
          // no change returned :-)
    }
}
Figure 8 VendingMachine Class 
class VendingMachine
{
    private State state;

    public VendingMachine()
    {
        Console.WriteLine("The Vending Machine is now online: product 
                          costs 25c");
        state = Start.Instance();
    }

    public void ChangeState(State to)
    {
        state = to;
    }

    public void Vend()
    {
        // deliver merchandise
        Console.WriteLine("Dispensing product...Thank you!");
    }

    public void AddNickel()
    {
        state.AddNickel(this);
    }

    public void AddDime()
    {
        state.AddDime(this);
    }

    public void AddQuarter()
    {
        state.AddQuarter(this);
    }
}

0 comments:

Post a Comment

Nam Le © 2014 - Designed by Templateism.com, Distributed By Templatelib