是否存在将第三方库提供的两个类组合在一起的模式?

时间:2018-07-25 02:17:04

标签: c# design-patterns

我有来自第三方库的两个类,它们具有非常相似的功能,可以将它们称为Car类和Bike类。我不能直接对类进行更改,但是我希望能够对其进行抽象,以便可以像这样使用它们

public bool drive(IDriveable vehicle)
{
    vehicle.Start();
    vehicle.Shift(Gear.Drive);
    vehicle.Accelerate();
    //so on...
}

反正我能做到吗?

4 个答案:

答案 0 :(得分:1)

也许您正在寻找decorator pattern

  

在面向对象的编程中,装饰器模式是一种设计模式,它允许将行为静态或动态地添加到单个对象中,而不会影响同一类中其他对象的行为。1装饰器模式通常对于遵守“单一职责原则”很有用,因为它允许将功能划分到具有唯一关注领域的类之间。[2]装饰器模式在结构上与责任链模式几乎相同,不同之处在于在责任链中,恰好一个类处理请求,而对于装饰器,所有类都处理请求。

我将创建同时实现BikeDecorator的{​​{1}}和CarDecorator

答案 1 :(得分:0)

You need to create new interface IDriveable which contains all functions that you need to call. In this case are, for example, Start and Accelerate. Then, creating 2 classes which are Car and Bike inherit BaseCar, BaseBike and implement IDriveable. For example:

using System;
using System.Collections.Generic;

public class Car
{
	public void Start()
	{
		Console.WriteLine("Start Car");
	}
	public void Accelerate()
	{
		Console.WriteLine("Accelerate Car");
	}
}

public class Bike
{
	public void Start()
	{
		Console.WriteLine("Start Bike");
	}
	public void Accelerate()
	{
		Console.WriteLine("Accelerate Bike");
	}
}

interface IDriveable
{
	void Start();	
	void Accelerate();		
}

class CarChild : Car, IDriveable
{
	//To hide the parent's function we use 'new'. We can call parent's functio by base.
	public new void Accelerate()
	{
		base.Accelerate();
		Console.WriteLine("Accelerate CarChild");
	}
}

class BikeChild: Bike, IDriveable
{
	
}
					
public class Program
{
	public static void Main()
	{
		List<IDriveable> devices = new List<IDriveable>();
		devices.Add(new CarChild());
		devices.Add(new BikeChild());
		SkimAll(devices);
	}
	private static void SkimAll(List<IDriveable> devices)
	{
		foreach(var device in devices)
		{
			device.Start();
			device.Accelerate();
		}
	}
}

答案 2 :(得分:0)

To my knowledge it is the Adapter pattern that you need to solve your problem. As per wikipedia, Adapter pattern solves the following problems:

  • How can a class be reused that does not have an interface that a client requires?
  • How can classes that have incompatible interfaces work together?
  • How can an alternative interface be provided for a class?

The Adapter design pattern describes how to solve such problems:

  • Define a separate Adapter class that converts the (incompatible) interface of a class (Adaptee) into another interface (Target) clients require.

  • Work through an Adapter to work with (reuse) classes that do not have the required interface.

答案 3 :(得分:0)

首先让我们假设第三方代码

public enum Gear
{
    Drive,
    Stop
}

以及下面的Car第三方代码

public class Car
{
    public void Start()
    {
        Console.Write($"Start Car");
    }

    public void Shift(Gear gear)
    {
        Console.Write($" Shift {gear}");
    }

    public void Accelerate()
    {
        Console.Write($" Accelerate Car");
    }
}

以及类似的如下所示的Bike第三方代码

    public class Bike
{
    public void Start()
    {
        Console.Write($"Start Bike");
    }

    public void Shift(Gear gear)
    {
        Console.Write($" Shift {gear}");
    }

    public void Accelerate()
    {
        Console.Write($" Accelerate Bike");
    }
}

然后我们可以如下定义接口

interface IDriveable
{
    void Start();

    void Shift(Gear gear);

    void Accelerate();
} 

现在我们可以为Car定义一个类,如下所示:

 class DriveableCar : IDriveable
{
    private Car _car;

    public DriveableCar()
    {
        _car = new Car();
    }
    public void Start()
    {
        _car.Start();
    }        
    public void Accelerate()
    {
        _car.Accelerate();
    }

    public void Shift(Gear gear)
    {
        _car.Shift(gear);
    }
}

对于自行车,我们可以如下定义另一个类

class DriveableBike : IDriveable
{
    private Bike _bike;

    public DriveableBike()
    {
        _bike = new Bike();
    }
    public void Start()
    {
        _bike.Start();
    }
    public void Shift(Gear gear)
    {
        _bike.Shift(gear);
    }
    public void Accelerate()
    {
        _bike.Accelerate();
    }
}

现在我们可以使用以下第三方代码

class Program
{
    private static void Main(string[] args)
    {

        // USING THE CAR

        var car = new DriveableCar();
        car.Start();
        car.Shift(Gear.Drive);
        car.Accelerate();


        //USING THE BIKE
        var bike = new DriveableBike();
        bike.Start();
        bike.Shift(Gear.Drive);
        bike.Accelerate();



        Console.Read();
    }
}

@ProgrammerAdept,希望这会有所帮助。