已发布和公共方法/属性之间有什么区别?

发布于 2024-07-10 19:53:37 字数 1531 浏览 6 评论 0原文

根据马丁·福勒的说法,“有些东西可以公开,但这并不意味着你已经发布了它。” 这是否意味着这样的事情:

public interface IRollsRoyceEngine
{
    void Start();
    void Stop();
    String GenerateEngineReport();
}

public class RollsRoyceEngine : IRollsRoyceEngine
{
    public bool EngineHasStarted { get; internal set; }

    public bool EngineIsServiceable { get; internal set; }

    #region Implementation of IRollsRoyceEngine

    public void Start()
    {
        if (EngineCanBeStarted())
            EngineHasStarted = true;
        else
            throw new InvalidOperationException("Engine can not be started at this time!");
    }

    public void Stop()
    {
        if (EngineCanBeStopped())
            EngineHasStarted = false;
        else
            throw new InvalidOperationException("Engine can not be started at this time!");
    }

    public string GenerateEngineReport()
    {
        CheckEngineStatus();
        return EngineIsServiceable ? "Engine is fine for now" : "Hmm...there may be some problem with the engine";
    }

    #endregion

    #region Non published methods

    public bool EngineCanBeStarted()
    {
        return EngineIsServiceable ? true : false;
    }

    public bool EngineCanBeStopped()
    {
        return EngineIsServiceable ? true : false;
    }

    public void CheckEngineStatus()
    {
        EngineIsServiceable = true;
        //_EngineStatus = false;
    }

    #endregion

}

可以说这个发布的接口是 IRollsRoyceEngine 而不是 RollsRoyceEngine 中的任何内容吗?

如果是这样,公开方法和发布方法之间的真正区别是什么?

According to Martin Fowler "Something can be public but that does not mean you have published it." Does this mean something like this:

public interface IRollsRoyceEngine
{
    void Start();
    void Stop();
    String GenerateEngineReport();
}

public class RollsRoyceEngine : IRollsRoyceEngine
{
    public bool EngineHasStarted { get; internal set; }

    public bool EngineIsServiceable { get; internal set; }

    #region Implementation of IRollsRoyceEngine

    public void Start()
    {
        if (EngineCanBeStarted())
            EngineHasStarted = true;
        else
            throw new InvalidOperationException("Engine can not be started at this time!");
    }

    public void Stop()
    {
        if (EngineCanBeStopped())
            EngineHasStarted = false;
        else
            throw new InvalidOperationException("Engine can not be started at this time!");
    }

    public string GenerateEngineReport()
    {
        CheckEngineStatus();
        return EngineIsServiceable ? "Engine is fine for now" : "Hmm...there may be some problem with the engine";
    }

    #endregion

    #region Non published methods

    public bool EngineCanBeStarted()
    {
        return EngineIsServiceable ? true : false;
    }

    public bool EngineCanBeStopped()
    {
        return EngineIsServiceable ? true : false;
    }

    public void CheckEngineStatus()
    {
        EngineIsServiceable = true;
        //_EngineStatus = false;
    }

    #endregion

}

Can it be said that published interface of this is IRollsRoyceEngine not whatever is in RollsRoyceEngine?

If so what is the real difference between public and published methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-07-17 19:53:37

我认为他的意思是契约为王——仅仅因为你的类中的方法是公共的,并不意味着客户就可以假设他们可以调用它,或者他们知道它的作用,或者它会在下一个中出现版本。 API 不是由源定义的,它们是由合同定义的,通常以文档的形式。

客户端有责任不调用未记录(未发布)的函数,而不是实现者有责任隐藏不应调用的方法。

有些人可能不同意这一点 - 通常是那些不信任文档的人,他们宁愿通过查看源代码来了解事物的工作原理,看看它实际上做什么,而不是作者做什么>声称确实如此。 他们很可能有一定的道理,尤其是在处理文档不足的代码的实践中。 但我认为这与福勒所说的相反,福勒所说的功能应该被正式定义,而不是通过检查特定实现来推断。

I assume he means that the contract is king - just because a method in your class is public, doesn't entitle clients to assume that they can call it, or that they know what it does, or that it will be there in the next version. APIs are not defined by the source, they're defined by a contract, usually in the form of documentation.

It is the responsibility of the client not to call undocumented (unpublished) functions, not the responsibility of the implementer to hide methods which shouldn't be called.

Some people might disagree with this - typically those who don't trust documentation, and would rather find out how things work by looking at the source to see what it actually does, rather that what the author claims it does. They may well have a point, especially in practice when dealing with under-documented code. But I think that's in opposition to what Fowler is saying, which is that functionality should be formally defined, rather than inferred by examination of the particular implementation.

得不到的就毁灭 2024-07-17 19:53:37

在我看来,提到的白皮书讨论的是 API 的目标受众,而不是接口及其实现之间的区别。

您可以在框架设计指南中找到类比,其中表示一旦您的 API 交付给您与消费者有合同。 例如,如果您在框架 IService 接口的 v1 中发布,则无法在 v2 中更改它,因为它会给最终开发人员带来重大更改。 相反,您必须创建继承自 IService 的新接口 IService2 并随 v2 一起发布。

因此,一旦您与最终开发人员“签订合同”,基本上公共 API 就会发布。

回到您的代码 - 例如,当您将其发送到开发社区时,它将被发布。

希望这个解释会有所帮助。

In my opinion mentioned white paper talks about target audience of the API rather than the distinction between interface and its implementation.

You can find analogy in Framework Design Guidelines which says that once your API shipped you have a contract with consumers. For example, if you shipped in v1 of your framework IService interface you cannot change it in v2 because it will introduce breaking changes to end developers. Instead you must create new interface IService2 inhereted from IService and ship it with v2.

So basically public API becomes published once you "sign a contract" with end developers.

Returning back to your code - it will be published when you ship it to development community for example.

Hope this explanation will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文