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

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

问题描述

我似乎无法理解松散耦合"的概念.我想松散"一词通常具有否定含义并没有帮助,所以我总是忘记松散耦合是的事情.

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类来处理购买.订单需要确定购物车中内容的总价值,它可能会像这样:

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类)如何取决于CartContents和CartEntry类的实现细节.如果我们试图更改此逻辑以允许折扣,则可能必须更改所有三个类别.另外,如果我们更改为使用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 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天全站免登陆