, # 3.0 Design Patterns, , , .
.
:
Adaptee ( ITarget), Adaptee. , Adaptee , Adaptee, . , ITARTet Adaptee. , ; , #, .
, , . , . . , , : .
, , ( ) , . :
IAircraft seabird = new Seabird( );
seabird.TakeOff( );
(seabird as ISeacraft).IncreaseRevs( );
, :
, , ITarget . . Adaptee , ITarget. (...)
, . , , , .. : , . ( , ).
, , , .
, , , , .
, :
Adapter adapter1 = new Adapter (new Adaptee( ));
adapter1.Request(5);
Adapter adapter2 = new Adapter (new Target( ));
Console.WriteLine(adapter2.Request(5));
:
, , , Adaptee ITARTet, , , Adaptee Pluggable Target, Adaptee .
, .
1. :
, , . , Adaptee, Target , .
( , Ride), .
, .
interface IBike {
void Pedal();
}
class Bike : IBike {
public void Pedal() {
Console.WriteLine("Moving my vehicle with my body");
}
}
interface IMotorcycle {
void Accelerate();
}
class Motorcycle : IMotorcycle {
public virtual void Accelerate() {
Console.WriteLine("Moving my vehicle with a hydrocarbon fuel engine");
}
}
class ElectricBike : Motorcycle, IBike {
bool _isAccelerating = false;
public override void Accelerate() {
_isAccelerating = true;
Console.WriteLine("Moving my vehicle with a electric engine");
}
public void Pedal() {
if (!_isAccelerating)
Console.WriteLine("Moving my vehicle with my body");
else
Console.WriteLine("Occupying my body with senseless effort, for my vehicle is already moving");
}
}
class MovingMyVehicle {
static void Main() {
IMotorcycle motorBike = new Motorcycle();
motorBike.Accelerate();
IBike newBike = new ElectricBike();
newBike.Pedal();
(newBike as IMotorcycle).Accelerate();
Console.Read();
}
}