【发布时间】:2017-03-14 21:53:17
【问题描述】:
双向适配器和可插入适配器都可以访问这两个类,还可以更改需要更改的方法的行为。以下是我的代码:
双向适配器
public interface IAircraft
{
bool Airborne { get; }
void TakeOff();
int Height { get; }
}
// Target
public sealed class Aircraft : IAircraft
{
int height;
bool airborne;
public Aircraft()
{
height = 0;
airborne = false;
}
public void TakeOff()
{
Console.WriteLine("Aircraft engine takeoff");
airborne = true;
height = 200; // Meters
}
public bool Airborne
{
get { return airborne; }
}
public int Height
{
get { return height; }
}
}
// Adaptee interface
public interface ISeacraft
{
int Speed { get; }
void IncreaseRevs();
}
// Adaptee implementation
public class Seacraft : ISeacraft
{
int speed = 0;
public virtual void IncreaseRevs()
{
speed += 10;
Console.WriteLine("Seacraft engine increases revs to " + speed + " knots");
}
public int Speed
{
get { return speed; }
}
}
// Adapter
public class Seabird : Seacraft, IAircraft
{
int height = 0;
// A two-way adapter hides and routes the Target's methods
// Use Seacraft instructions to implement this one
public void TakeOff()
{
while (!Airborne)
IncreaseRevs();
}
// Routes this straight back to the Aircraft
public int Height
{
get { return height; }
}
// This method is common to both Target and Adaptee
public override void IncreaseRevs()
{
base.IncreaseRevs();
if (Speed > 40)
height += 100;
}
public bool Airborne
{
get { return height > 50; }
}
}
class Experiment_MakeSeaBirdFly
{
static void Main()
{
// No adapter
Console.WriteLine("Experiment 1: test the aircraft engine");
IAircraft aircraft = new Aircraft();
aircraft.TakeOff();
if (aircraft.Airborne) Console.WriteLine(
"The aircraft engine is fine, flying at "
+ aircraft.Height + "meters");
// Classic usage of an adapter
Console.WriteLine("\nExperiment 2: Use the engine in the Seabird");
IAircraft seabird = new Seabird();
seabird.TakeOff(); // And automatically increases speed
Console.WriteLine("The Seabird took off");
// Two-way adapter: using seacraft instructions on an IAircraft object
// (where they are not in the IAircraft interface)
Console.WriteLine("\nExperiment 3: Increase the speed of the Seabird:");
(seabird as ISeacraft).IncreaseRevs();
(seabird as ISeacraft).IncreaseRevs();
if (seabird.Airborne)
Console.WriteLine("Seabird flying at height " + seabird.Height +
" meters and speed " + (seabird as ISeacraft).Speed + " knots");
Console.WriteLine("Experiments successful; the Seabird flies!");
Console.Read();
}
}
可插拔模式
class Adaptee
{
public double Precise(double a, double b)
{
return a / b;
}
}
// New standard for requests
class Target
{
public string Estimate(int i)
{
return "Estimate is " + (int)Math.Round(i / 3.0);
}
}
// Implementing new requests via old
class Adapter : Adaptee
{
public Func<int, string> Request;
// Different constructors for the expected targets/adaptees
// Adapter-Adaptee
public Adapter(Adaptee adaptee)
{
// Set the delegate to the new standard
Request = x =>
{
return "Estimate based on precision is " +
(int)Math.Round(Precise(x, 3));
};
}
// Adapter-Target
public Adapter(Target target)
{
// Set the delegate to the existing standard
Request = target.Estimate;
}
}
class Client
{
static void Main()
{
Adapter adapter1 = new Adapter(new Adaptee());
Console.WriteLine(adapter1.Request(5));
Adapter adapter2 = new Adapter(new Target());
Console.WriteLine(adapter2.Request(5));
Console.Read();
}
}
在上面的两个代码示例中,我没有发现关于模式功能的任何不同之处。那么这些模式有什么区别呢?谁能帮我理解一下?我一直在参考这个Design Pattern C# 3.0
更新 1
我看不懂这个参考中给出的例子,所以我更新了一个简单的代码,我想根据代码从场景中实现双向适配器
interface Ibike {
void Ride(int energy,int time);
}
class Bike : Ibike {
public void Ride(int energy,int time) {
Console.WriteLine("riding bike with calories of energy "+energy+" spend time "+time);
}
}
interface Imotorcycle {
void Ride(int fuel);
}
class Motorcycle : Imotorcycle {
public void Ride(int fuel) {
Console.WriteLine("riding motorbike with fuel "+fuel);
}
}
class Client {
static void Main() {
Ibike bike = new Bike();
Imotorcycle motorBike = new Motorcycle();
bike.Ride(50, 2);
motorBike.Ride(3);
Console.Read();
}
}
现在在这种情况下,我怎样才能将其作为双向适配器。 双向适配器解决了两个系统的问题,其中 一个系统的特性必须在另一个系统中使用,反之亦然 反之亦然。设置了一个 Adapter 类来吸收重要的公共 两种方法,并为两者提供适应。所结果的 双方都可以接受适配器对象
【问题讨论】:
-
接口。可插拔,您不必有接口或具体类型。如果您愿意,您的 Pluggable 适配器可以接收委托而不是类。您在此处拥有的 MSDN 代码只是根据我的研究创建 Pluggable 适配器的一种方式。
-
@TyCobb 我无法理解你所说的。现在,如果我使用adaptee.Precise(x,3) 调用该方法,那么它会被称为双向适配器而不是可插拔适配器?
-
没有。 Pluggable 可以接受任何需要做的事情(类、接口、委托等)。双向使用接口。使用双向,您正在定义一个可以利用的合约。可插拔,您可以准确地为其提供所需的内容,例如将两个数字相加的方法 - 您不需要完整的对象/接口。
-
@TyCobb 来自第一个示例,如果我使用委托调用 increaseRevs() 方法,那么它将是可插入适配器吗?
-
你应该考虑引用C# 3.0 Design Patterns这本书作为你的来源。通过访问本书的全部内容,它可能会很有用并帮助其他人回答您的问题。
标签: c# design-patterns adapter