Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
FlyWithWings
IFlyBehavior
y()
y()
FlyRocketPowered
Duck
public Duck {}
IFlyBehavior yBehavior
setFlyBehavior()
swim()
display()
FlyNoWay
y()
y()
performFly()
Main
MallardDuck
RubberDuck
display()
display()
class Program {
static void main(string[] args) {
public MallardDuck() {
yBehavior = new FlyWithWings();
}
public RubberDuck() {
yBehavior = new FlyNoWay();
}
mallard.performFly();
rubberDuckie.performFly();
rubberDuckie.setFlyBehavir(new FlyRocketPowered());
rubberDuckie.performFly();
}
}
Output
Im ying!
I cant y!
Im ying with a rocket!
Observer Pattern
Subject
Observer
DisplayElement
registerObserver()
removeObserver()
notifyObservers()
update()
display()
WeatherData
CurrentConditionsDisplay
registerObserver()
removeObserver()
notifyObservers()
getTemperature()
getHumidity()
getPressure()
update()
display()
class Program
{
static void Main(string[] args)
{
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentDisplay
= new CurrentConditionsDisplay(weatherData);
Main
subject
Output
Current conditions: 80 F degrees,
Humidity : 65 % and
Pressure: 30,4 P.
Current conditions: 82 F degrees,
Humidity : 70 % and
Pressure: 29,2 P.
Current conditions: 78 F degrees,
Humidity : 90 % and
Pressure: 27,7 P.