什么是“松耦合"?请举例 [英] What is "loose coupling?" Please provide examples

查看:45
本文介绍了什么是“松耦合"?请举例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法理解松散耦合"的概念.我想松散"这个词通常有负面含义也无济于事,所以我总是忘记松散耦合是一件好事.

I can't seem to grok the concept of "loose coupling." I suppose it doesn't help that the word "loose" usually has a negative connotation, so I always forget that loose coupling is a good thing.

有人可以展示一些说明这个概念的之前"和之后"代码(或伪代码)吗?

Will somebody please show some "before" and "after" code (or pseudocode) that illustrates this concept?

推荐答案

考虑一个简单的购物车应用程序,它使用 CartContents 类来跟踪购物车中的商品和一个 Order 类用于处理购买.Order 需要确定购物车中内容的总价值,它可能会这样做:

Consider a simple shopping cart application that uses a CartContents class to keep track of the items in the shopping cart and an Order class for processing a purchase. The Order needs to determine the total value of the contents in the cart, it might do that like so:

紧耦合示例:

public class CartEntry
{
    public float Price;
    public int Quantity;
}

public class CartContents
{
    public CartEntry[] items;
}

public class Order
{
    private CartContents cart;
    private float salesTax;

    public Order(CartContents cart, float salesTax)
    {
        this.cart = cart;
        this.salesTax = salesTax;
    }

    public float OrderTotal()
    {
        float cartTotal = 0;
        for (int i = 0; i < cart.items.Length; i++)
        {
            cartTotal += cart.items[i].Price * cart.items[i].Quantity;
        }
        cartTotal += cartTotal*salesTax;
        return cartTotal;
    }
}

请注意 OrderTotal 方法(以及 Order 类)如何依赖于 CartContentsCartEntry 类的实现细节.如果我们尝试更改此逻辑以允许折扣,我们可能必须更改所有 3 个类.此外,如果我们改为使用 List 集合来跟踪项目,我们也必须更改 Order 类.

Notice how the OrderTotal method (and thus the Order class) depends on the implementation details of the CartContents and the CartEntry classes. If we were to try to change this logic to allow for discounts, we'd likely have to change all 3 classes. Also, if we change to using a List<CartEntry> collection to keep track of the items we'd have to change the Order class as well.

现在有一个更好的方法来做同样的事情:

Now here's a slightly better way to do the same thing:

低耦合示例:

public class CartEntry
{
    public float Price;
    public int Quantity;

    public float GetLineItemTotal()
    {
        return Price * Quantity;
    }
}

public class CartContents
{
    public CartEntry[] items;

    public float GetCartItemsTotal()
    {
        float cartTotal = 0;
        foreach (CartEntry item in items)
        {
            cartTotal += item.GetLineItemTotal();
        }
        return cartTotal;
    }
}

public class Order
{
    private CartContents cart;
    private float salesTax;

    public Order(CartContents cart, float salesTax)
    {
        this.cart = cart;
        this.salesTax = salesTax;
    }

    public float OrderTotal()
    {
        return cart.GetCartItemsTotal() * (1.0f + salesTax);
    }
}

特定于购物车订单项或购物车集合或订单的实现的逻辑仅限于该类.因此,我们可以更改任何这些类的实现,而无需更改其他类.我们可以通过改进设计、引入接口等来进一步解耦,但我认为你明白了这一点.

The logic that is specific to the implementation of the cart line item or the cart collection or the order is restricted to just that class. So we could change the implementation of any of these classes without having to change the other classes. We could take this decoupling yet further by improving the design, introducing interfaces, etc, but I think you see the point.

这篇关于什么是“松耦合"?请举例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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