学习高校课程-软件设计模式-适配器模式和桥接模式(lec6)
Adaptor: Problem
Imagine that you’re creating a stock market monitoring app. The app downloads the stock data from multiple sources in XML format and then displays nice-looking charts and diagrams for the user.
想象一下您正在创建一个股票市场监控应用程序。该应用程序以 XML 格式从多个来源下载股票数据,然后为用户显示漂亮的图表和图表。
At some point, you decide to improve the app by integrating a smart 3rd-party analytics library. But there’s a catch: the analytics library only works with data in JSON format.
在某些时候,您决定通过集成智能第三方分析库来改进应用程序。但有一个问题:分析库仅适用于 JSON 格式的数据。
Adaptor: Solution
You can create an adapter. This is a special object that converts the interface of one object so that another object can understand it.
您可以创建一个适配器。这是一种特殊的对象,它转换一个对象的接口,以便另一个对象可以理解它。
An adapter wraps one of the objects to hide the complexity of conversion happening behind the scenes. The wrapped object isn’t even aware of the adapter. For example, you can wrap an object that operates in meters and kilometers with an adapter that converts all of the data to imperial units such as feet and miles.
适配器包装其中一个对象以隐藏幕后发生的转换的复杂性。被包装的对象甚至不知道适配器。例如,您可以使用适配器包装以米和公里为单位的对象,该适配器将所有数据转换为英制单位,例如英尺和英里。
Adapters can not only convert data into various formats but can also help objects with different interfaces collaborate. Here’s how it works:
适配器不仅可以将数据转换为各种格式,还可以帮助不同接口的对象进行协作。它的工作原理如下:
The adapter gets an interface, compatible with one of the existing objects.
适配器获取一个与现有对象之一兼容的接口。
Using this interface, the existing object can safely call the adapter’s methods.
使用此接口,现有对象可以安全地调用适配器的方法。
Upon receiving a call, the adapter passes the request to the second object, but in a format and order that the second object expects.
收到调用后,适配器将请求传递给第二个对象,但采用第二个对象期望的格式和顺序。
Bridge: Problem
Say you have a geometric Shape class with a pair of subclasses: Circle and Square. You want to extend this class hierarchy to incorporate colors, so you plan to create Red and Blue shape subclasses. However, since you already have two subclasses, you’ll need to create four class combinations such as BlueCircle and RedSquare.
假设您有一个几何Shape类,它有一对子类: Circle和Square 。您想要扩展该类层次结构以合并颜色,因此您计划创建Red和Blue形状子类。但是,由于您已经有两个子类,因此您需要创建四个类组合,例如BlueCircle和RedSquare 。
Adding new shape types and colors to the hierarchy will grow it exponentially. For example, to add a triangle shape you’d need to introduce two subclasses, one for each color. And after that, adding a new color would require creating three subclasses, one for each shape type. The further we go, the worse it becomes.
向层次结构中添加新的形状类型和颜色将使层次结构呈指数级增长。例如,要添加三角形,您需要引入两个子类,每个子类对应一种颜色。之后,添加一种新颜色需要创建三个子类,每个子类对应一种形状类型。我们走得越远,情况就越糟糕。
Bridge: Solution
This problem occurs because we’re trying to extend the shape classes in two independent dimensions: by form and by color. That’s a very common issue with class inheritance.
出现此问题的原因是我们试图在两个独立的维度上扩展形状类:按形状和按颜色。这是类继承的一个非常常见的问题。
The Bridge pattern attempts to solve this problem by switching from inheritance to the object composition. What this means is that you extract one of the dimensions into a separate class hierarchy, so that the original classes will reference an object of the new hierarchy, instead of having all of its state and behaviors within one class.
桥接模式试图通过从继承切换到对象组合来解决这个问题。这意味着您将其中一个维度提取到一个单独的类层次结构中,以便原始类将引用新层次结构的一个对象,而不是在一个类中包含其所有状态和行为
Following this approach, we can extract the color-related code into its own class with two subclasses: Red and Blue. The Shape class then gets a reference field pointing to one of the color objects. Now the shape can delegate any color-related work to the linked color object. That reference will act as a bridge between the Shape and Color classes. From now on, adding new colors won’t require changing the shape hierarchy, and vice versa.
按照这种方法,我们可以将与颜色相关的代码提取到它自己的类中,该类具有两个子类: Red和Blue 。然后, Shape类获取指向颜色对象之一的引用字段。现在,形状可以将任何与颜色相关的工作委托给链接的颜色对象。该引用将充当Shape和Color类之间的桥梁。从现在开始,添加新颜色不需要更改形状层次结构,反之亦然。