Head First Design Patterns: The Observer Pattern

OO Principles: Strive for loosely coupled designs between objects that interact.

OO Patterns: Observer – defines a one to many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Subjects/Observables update Observers using a common interface.

public interface ISubject
{
void RegisterObserver(IObserver observer);
void RemoveObserver(IObserver observer);
void NotifyObservers();
}

public class WeatherData : ISubject
{
private List<IObserver> _observers = new List<IObserver>();
private float _temperature;
private float _humidity;
private float _pressure;

public void RegisterObserver(IObserver observer)
{
_observers.Add(observer);
}

public void RemoveObserver(IObserver observer)
{
_observers.Remove(observer);
}

public void NotifyObservers()
{
foreach (IObserver observer in _observers)
{
observer.Update(_temperature, _humidity, _pressure);
}
}

public void MeasurementsChanged()
{
NotifyObservers();
}

public void SetMeasurements(float temperature, float humidity, float pressure)
{
this._temperature = temperature;
this._humidity = humidity;
this._pressure = pressure;
MeasurementsChanged();
}
}

 

public interface IObserver
{
void Update(float temperature, float humidity, float pressure);
}

public interface IDisplayElement
{
void Display();
}

public class CurrentConditionsDisplay : IObserver, IDisplayElement
{
private float _temperature;
private float _humidity;
private ISubject _weatherData;

public CurrentConditionsDisplay(ISubject weatherData)
{
this._weatherData = weatherData;
weatherData.RegisterObserver(this);
}

public void Update(float temperature, float humidity, float pressure)
{
this._temperature = temperature;
this._humidity = humidity;
Display();
}

public void Display()
{
Console.WriteLine(“Current conditions: ” + _temperature
+ “F degrees and ” + _humidity + “% humidity”);
}
}

Advertisement

Head First Design Patterns: Intro to Design Patterns

OO Basics: Abstraction, Encapsulation, Polymorphism, Inheritance

OO Principles: Encapsulate what varies, Favour composition over inheritance, Program to interfaces, not implementations.

OO Patterns: Strategy – defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

 

public abstract class Duck
{
public IFlyBehavior FlyBehavior { get; set; }
public IQuackBehavior QuackBehavior { get; set; }

public abstract void Display();

public void PerformFly()
{
FlyBehavior.Fly();
}

public void PerformQuack()
{
QuackBehavior.Quack();
}

public void Swim()
{
Console.WriteLine(“All ducks float, even decoys!”);
}

}

public class MallardDuck : Duck
{
public MallardDuck()
{
QuackBehavior = new LoudQuack();
FlyBehavior = new FlyWithWings();
}

override public void Display()
{
Console.WriteLine(“I’m a real Mallard duck”);
}
}

 

public interface IFlyBehavior
{
void Fly();
}

public class FlyWithWings : IFlyBehavior
{
public void Fly()
{
Console.WriteLine(“I’m flying!!”);
}
}

 

public interface IQuackBehavior
{
void Quack();
}

// Name it LoadQuack to avoid conflict with method name
public class LoudQuack : IQuackBehavior
{
public void Quack()
{
Console.WriteLine(“LoudQuack”);
}
}

 

apple tv and firecore

 

Seas0nPass provides an UNTETHERED jailbreak of the 2nd gen Apple TV running the latest 5.0.2 (iOS 5.1.1 – 9B830) software that was released on June 6th, 2012.

http://files.firecore.com/SP/Seas0nPass.zip

Launch Seas0nPass and choose Create IPSW. When complete, connect your Apple TV to your Mac/PC using a micro-USB cable (leave power cable disconnected). Once the light on the front of the Apple TV begins to flash rapidly, point the remote at the Apple TV and hold both the MENU and PLAY/PAUSE buttons for 7 seven seconds.

Once complete, connect the Apple TV to a TV. There should be a red F logo in place of Setup on the Apple TV.
Run FireCore from your mac and install the firecore files to the Apple TV.

 

 

http://support.firecore.com/entries/387605

 

Website setup

  1. Register domain name e.g. lemonrace.com
  2. Register email address with gmail with the same name e.g. lemonracedotcom@gmail.com
  3. Register with tumblr or host with dotnet hosting
  4. If using tumblr, setup the A record in the DNS
  5. Register with ping.fm to post to multiple locations at once
  6. Create twitter account e.g. lemonracedotcom
  7. Create flickr account e.g. lemonracedotcom using gmail account as login