在解耦方面接口和抽象类的区别? [英] Difference between Interface and Abstract class in terms of Decoupling?

查看:157
本文介绍了在解耦方面接口和抽象类的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我们所知,基本上有两种接口和抽象类之间的重要区别。

As we know there are basically two important difference between Interface and Abstract class.


  1. 我们可以在抽象类函数定义。当我们要在无需添加类功能来跟踪它的所有实现这是有利的。

  1. We can have function definitions in abstract class. This is advantageous when we want to add a function in a class without need to track down it's all implementations.

我们可以有多个接口的实现。

We can have multiple interface implementation.

我刚刚才知道,我们可以去耦方面区分它们?

I just came to know that we can differentiate between them in terms of Decoupling?

您的意见...

此外,如果你可以提供一个解释接口和抽象类解耦一个非常基本的链接?

我们通常使用业务逻辑层 数据访问层(包含抽象函数)和 DataAccess.SqlServer层。对?尽管事实是,我们意识到的业务需求,我们为什么要创建数据访问层(包含抽象函数),为什么不能业务逻辑层直接访问<强> DataAccess.SqlServer图层

We normally use Business Logic Layer, Data Access Layer(contains abstract functions) and DataAccess.SqlServer Layer. Right? Despite of the fact that we aware of the Business needs, why are we creating Data Access Layer(contains abstract functions), Why can't Business Logic layer directly access DataAccess.SqlServer Layer?

推荐答案

去耦

在编程和设计,这通常是使code这是可以重复使用尽可能少的依赖尽可能的行为。

In programming and design, this is generally the act of making code which is re-usable with as few dependencies as possible.

工厂模式在这方面

在使用工厂模式,你有一个集中的工厂,可以,而不必定义它们本身创建对象。这将是到对象的定义

When using the Factory Pattern, you have a centralized factory which can create objects without necessarily defining them itself. That would be up to the object's definition.

抽象和接口

接口

定义接口是最佳实践,因为它允许一个重量轻的类型被用于推断,并且还提供了其中所有继承的类必须遵守的蓝图。例如,的IDisposable 必须实现的Dispose 方法。请注意,这是从接口的分离,因为每一个类继承的IDisposable 将定义其自身的处置功能方法。

Defining an interface is best practice, as it allows for a light weight type to be used for inference, and also provides a blueprint which all inheriting classes must abide by. For example, IDisposable must implement the Dispose method. Note that this is decoupled from the interface, as each class inheriting IDisposable will define its own function of the Dispose method.

摘要

摘要是接口,它是用于继承和推理相似,但它包含了所有的类将继承定义。事到每一个汽车的程度将有一个引擎,汽车一个很好的抽象类能包括predefined设置的用于发动机的方法。

Abstract is similar to interface in that it is used for inheritance and inference, but it contains definitions which all classes will inherit. Something to the extent of every automobile will have an engine so a good abstract class for automobile could include a predefined set of methods for an engine.

说明

在这里你会看到使用接口和抽象类继承的简单例子。当接口由一个抽象类继承,然后它的方法是定制时,会出现脱钩。这允许类继承抽象类,仍然有相同类型的接口。的优点是,在继承抽象类的类可以在预期的类型是原始接口被使用。

Here you will see a simple example of inheritance using an interface and an abstract class. The decoupling occurs when the interface is inherited by an abstract class and then it's methods are customized. This allows for a class to inherit the abstract class and still have the same type as the interface. The advantage is that the class inheriting the abstract class can be used when the expected type is the original interface.

去耦

这优势允许要使用的任何实现这符合预期的接口。因此,许多不同的重载可写入和传递。下面是一个例子。

That advantage allows for any implementation to be used which conforms to the expected interface. As such, many different overloads can be written and passed in. Here is an example of one.

示例

接口定义

public interface IReady
{
    bool ComputeReadiness();
}

继承

public abstract class WidgetExample : IReady
{
    public int WidgetCount { get; set; }
    public int WidgetTarget { get; set; }
    public bool WidgetsReady { get; set; }

    public WidgetExample()
    {
        WidgetCount = 3;
        WidgetTarget = 45;
    }

    public bool ComputeReadiness()
    {
        if (WidgetCount < WidgetTarget)
        {
            WidgetsReady = false;
        }
        return WidgetsReady;
    }
}


public class Foo : WidgetExample
{
    public Foo()
    {
        this.WidgetTarget = 2;
    }
}

public class Bar : IReady
{
    public bool ComputeReadiness()
    {
        return true;
    }
}

去耦

public class UsesIReady
{
    public bool Start { get; set; }
    public List<string> WidgetNames { get; set; }

    //Here is the decoupling. Note that any object passed
    //in with type IReady will be accepted in this method
    public void BeginWork(IReady readiness)
    {
        if (readiness.ComputeReadiness())
        {
            Start = true;
            Work();
        }
    }

    private void Work()
    {
        foreach( var name in WidgetNames )
        {
            //todo: build name
        }
    }
}

多态性

public class Main
{
    public Main()
    {
        //Notice that either one of these implementations 
        //is accepted by BeginWork

        //Foo uses the abstract class
        IReady example = new Foo();
        UsesIReady workExample = new UsesIReady();
        workExample.BeginWork(example);

        //Bar uses the interface
        IReady sample = new Bar();
        UsesIReady workSample = new UsesIReady();
        workSample.BeginWork(sample);
    }
}

这篇关于在解耦方面接口和抽象类的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆