Design Patterns --Adapter Pattern mode adapter

My first contact with design patterns, selected the four types of structures inside type, this type is characterized by a combination of interest between classes & objects (using inheritance), from which I select adapter mode specific learning.

An adapter mode (Adapter Pattern) is defined:

Adapter is the bridge mode as two incompatible interfaces between. This type of design pattern belongs structural model, which combines the functionality of two separate interfaces. This model involves a single class that is responsible for adding separate or incompatible interface functions.

Intent: to convert the interface of a class into another interface clients expect. Adapter pattern makes those classes otherwise because of incompatible interfaces can not work together to work together.

Mainly to solve: the main solution in the software system, often want some "existing object" into the new environment, new environmental requirements and the interface is now the object can not be met.

When to use:  1, the system requires the use of an existing class, and such an interface does not meet the needs of the system. 2, want to create a reusable class for some classes is not much correlation between each other, including some that may work together in the future introduction of the class, these sources do not necessarily have the same kind of interface. 3, through the interface conversion, one class into another class system.

 

The key code: adapter inherited or dependent objects that already exist, to achieve the desired target interface.

How to solve: inherited or dependent (recommended).

Application examples:  American Electric 110V, China 220V, there should be a 110V adapter into 220V.

Advantages:  can make any kind of run together two unrelated.

缺点: 1、过多地使用适配器,会让系统非常零乱,不易整体进行把握。因此如果不是很有必要,可以不使用适配器,而是直接对系统进行重构。 2.由于 JAVA 至多继承一个类,所以至多只能适配一个适配者类,而且目标类必须是抽象类。

注意事项:适配器不是在详细设计时添加的,而是解决正在服役的项目的问题。

 

二、适配器模式的实例:

我们有一个 MediaPlayer 接口和一个实现了 MediaPlayer 接口的实体类 AudioPlayer。默认情况下,AudioPlayer 可以播放 mp3 格式的音频文件。

我们还有另一个接口 AdvancedMediaPlayer 和实现了 AdvancedMediaPlayer 接口的实体类。该类可以播放 vlc 和 mp4 格式的文件。

我们想要让 AudioPlayer 播放其他格式的音频文件。为了实现这个功能,我们需要创建一个实现了 MediaPlayer 接口的适配器类 MediaAdapter,并使用 AdvancedMediaPlayer 对象来播放所需的格式。AudioPlayer 使用适配器类 MediaAdapter 传递所需的音频类型,不需要知道能播放所需格式音频的实际类。AdapterPatternDemo,我们的演示类使用 AudioPlayer 类来播放各种格式。

 

 

步骤 1

为媒体播放器和更高级的媒体播放器创建接口:

MediaPlayer.java

 

AdvancedMediaPlayer.java

 

步骤 2

创建实现了 AdvancedMediaPlayer 接口的实体类。

VlcPlayer.java

Mp4Player.java

 

步骤 3

创建实现了 MediaPlayer 接口的适配器类。

MediaAdapter.java

 

步骤 4

创建实现了 MediaPlayer 接口的实体类。

AudioPlayer.java

 

步骤 5

使用 AudioPlayer 来播放不同类型的音频格式。

步骤 6

执行程序,输出结果:

 

 

 

 整个package的内容:

 

总结:

       适配器模式是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。

       本例中的适配器就是MediaAdapter。首先AdvancedMediaPlayer创建新功能的接口,然后用MediaAdapter来实现它。然后用AudioPlayer来实现原始功能接口MediaPlayer,并使用适配器MediaAdapter的新功能,从而达到不改变原来的代码的基础上实现对新格式的兼容。

 

Guess you like

Origin www.cnblogs.com/qyf2199/p/12006108.html