msgbartop
Around Software Development and Innovation
msgbarbottom

27 Feb 11 Head First Design Patterns: Template Method

In questo periodo come detto sto leggendo questo libro

Vi riporto un altro pattern che ho rivisto…

Template Method:

Defines the skeleton of an algorithm, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorith without changing the algorithm’s structure.

using System;
using System.Collections.Generic;
using System.Text;

namespace TemplateMethod
{
    /// <summary>
    /// Abstract class
    /// </summary>
    public abstract class CaffeineBeverageWithHook
    {
        public void prepareRecipe()
        {
            boilWater();
            braw();
            pourInCup();
            addCondiments();
        }

        public abstract void braw();
        public abstract void addCondiments();

        public void boilWater()
        {
            System.Console.WriteLine("Boiling water...");
        }

        public void pourInCup()
        {
            System.Console.WriteLine("Pouring into cup...");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace TemplateMethod
{
    /// <summary>
    /// Concrete class
    /// </summary>
    public class CoffeeWithHook : CaffeineBeverageWithHook
    {
        public override void braw()
        {
            System.Console.WriteLine("Dripping Coffee through filter...");
        }

        public override void addCondiments()
        {
            System.Console.WriteLine("Adding sugar and milk...");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using TemplateMethod;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            CaffeineBeverageWithHook obj = new CoffeeWithHook();
            obj.prepareRecipe();
            System.Console.ReadLine();
        }
    }
}

Bye
Antonio

27 Feb 11 Head First Design Pattern: Decorator

Ancora dal libro Head First Design Pattern in C#:

//Base abstract class
using System;
namespace Decorator
{
    /// <summary>
    /// Base class for Beverage. It represents the Component
    /// </summary>
    public abstract class Beverage
    {
        string _Description;

        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }

        public abstract double CalculateCost();
    }
}

//Concrete class
using System;
using System.Collections.Generic;
using System.Text;

namespace Decorator
{
    /// <summary>
    /// Concrete class for Component
    /// </summary>
    public class Coffee : Decorator.Beverage
    {

        public override double  CalculateCost()
        {
            return 1;
        }
    }
}

//Base abstract class for decorator
using System;
using System.Collections.Generic;
using System.Text;

namespace Decorator
{
    /// <summary>
    /// Class for Decorator
    /// </summary>
    public abstract class CondimentDecorator : Decorator.Beverage
    {
        public abstract string Description
        {
            get;
            set;
        }
    }
}

//Decorator concrete class
using System;
using System.Collections.Generic;
using System.Text;

namespace Decorator
{
    /// <summary>
    /// Concrete decorator
    /// </summary>
    public class Mocha : Decorator.CondimentDecorator
    {
        Beverage bev;

        public Mocha(Beverage b)
        {
            this.bev = b;
        }

        public override double CalculateCost()
        {
            return this.bev.CalculateCost() +0.20;
        }

        public override string Description
        {
            get
            {
                return "Mocha";
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }
    }
}

Bye
Antonio

27 Feb 11 Head First Design Patterns: Command Pattern

Ancoran Patterns, rivediamo un pò oggi il Command Pattern.

Command Pattern:Encapsulate a request as an object, thereby letting you parametrize clients with different requests,queue,or log requests, and support undoable operations.

Command Pattern in C#:

 

//Command interfaceusing System;using System.Collections.Generic;using System.Text;

namespace Command{    public interface ICommand    {        void Execute();    }}

using System;using System.Collections.Generic;using System.Text;

namespace Command{    //Concrete Command    public class LightOffCommand : ICommand    {

        #region ICommand Members

        public void Execute()        {            throw new Exception("The method or operation is not implemented.");        }

        #endregion    }}

using System;using System.Collections.Generic;using System.Text;

namespace Command{    /// <summary>    /// Concrete Command    /// </summary>    public class LightOnCommand : ICommand    {        Light lg;        public LightOnCommand(Light my)        {            this.lg = my;        }        #region ICommand Members

        public void Execute()        {            this.lg.On();        }

        #endregion    }}

using System;using System.Collections.Generic;using System.Text;

namespace Command{    /// <summary>    /// Null command, concrete command    /// </summary>    public class NoCommand : ICommand    {        #region ICommand Members

        public void Execute()        {            System.Console.WriteLine("No command");        }

        #endregion    }}

using System;using System.Collections.Generic;using System.Text;

namespace Command{    //Receiver    public class Light    {        private string _DescLight="Light";

        public string DescLight        {            get { return _DescLight; }            set { _DescLight = value; }        }

        public Light(string place)        {            this._DescLight = place;        }        public void On()        {            System.Console.WriteLine(this.DescLight + " is ON.");        }        public void Off()        {            System.Console.WriteLine(this.DescLight + " is OFF.");        }    }}

using System;using System.Collections.Generic;using System.Text;

namespace Command{    /// <summary>    /// Invoker    /// </summary>    public class RemoteControl    {        ICommand[] onCommands;        ICommand[] offCommands;

        public RemoteControl()        {            this.onCommands = new ICommand[7];            this.offCommands = new ICommand[7];            for(int i=0;i<7;i++){                this.offCommands[i] = new NoCommand();                this.offCommands[i] = new NoCommand();            }        }

        public void stCommand(int slot, ICommand on,ICommand off)        {            this.onCommands[slot]=on;            this.offCommands[slot]=off;        }

        public void onButtonPushed(int slot)        {            this.onCommands[slot].Execute();        }

        public void offButtonPushed(int slot)        {            this.offCommands[slot].Execute();        }    }}

Bye Antonio

27 Feb 11 Head First Design Patterns: Observer Pattern

Sto leggendo da un pò questo libro che mi permette di rivedere i GOF Design Patterns. Ma purtroppo il codice sorgente è in Java, ma io preferisco C#…

quindi come esercizio non resta che implementare il tutto in C#…

Partiamo dal pattern Observer:

//Subject
using System;
using System.Collections.Generic;
using System.Text;

namespace Observer
{
public interface ISubject
{
void RegisterObserver(IObserver o);
void RemoveObserver(IObserver o);
void NotifyObservers();
}
}

using System;
using System.Collections.Generic;
using System.Text;

namespace Observer
{
public interface IObserver
{
void Update(double temp, double humidity, double preasure);
}
}

using System;
using System.Collections.Generic;
using System.Text;

namespace Observer
{
public interface IDisplay
{
void Dispay();
}
}

//Subject Code
using System;
using System.Collections.Generic;
using System.Text;

namespace Observer
{
public class WeatherData : ISubject
{
double _humidity;

public double Humidity
{
get { return _humidity; }
set { _humidity = value; }
}
double _preassure;

public double Preassure
{
get { return _preassure; }
set { _preassure = value; }
}
double _temperature;

public double Temperature
{
get { return _temperature; }
set { _temperature = value; }
}

///
/// Observers
///
System.Collections.Generic.IList objs = new System.Collections.Generic.List();
#region ISubject Members

public void RegisterObserver(IObserver o)
{
objs.Add(o);
}

public void RemoveObserver(IObserver o)
{
objs.Remove(o);
}

public void NotifyObservers()
{
foreach (IObserver obj in objs)
obj.Update(this._temperature, this._humidity, this._preassure);
}

#endregion
}
}

//Observer code
using System;
using System.Collections.Generic;
using System.Text;

namespace Observer
{
public class CurrentDispaly : IObserver,IDisplay
{
double _humidity;
double _preassure;
double _temperature;
ISubject subject;

public CurrentDispaly(ISubject sub)
{
//Istanzia l’oggetto e lo aggiunge come Observer
subject = sub;
subject.RegisterObserver(this);
}
#region IObserver Members

public void Update(double temp, double humidity, double preasure)
{
this._temperature = temp;
this._preassure = preasure;
this._humidity = humidity;
this.Dispay();
}

#endregion

#region IDisplay Members

public void Dispay()
{
System.Console.WriteLine(this._humidity + ” ” + this._preassure + ” ” + this._temperature);
}

#endregion
}
}

Bye

Antonio