This is the fifth day of my participation in the August Wen Challenge.More challenges in August

Preface: why to explain these two modes once, say some words: because it is relatively simple (^_^), in fact, they are similar and sometimes we are easy to confuse the concept.

While these two design patterns are similar to another decorator pattern, all three of them are classified as structural patterns, so let’s look at what the adapter pattern and the appearance pattern are.

Another decorating pattern can be seen in my other post, Head First Design Pattern – Decorator Pattern.

Adapter mode

Adapters correspond to examples in our real life, the most typical is plug interface adapter, for example, we buy some Hong Kong version of mobile phone charging head is round triangle plug, while the mainland triangle power plug board can not plug into the Hong Kong version of the plug.

At this time, we will buy a adapter on a treasure for conversion, and this adapter is the adapter, with it to adapt the Hong Kong version of the mobile phone charging head so that he can plug into our power plug board inside.

Let me give you an example of what this adapter is in design mode and how it behaves programmatically: We have a duck, a chicken, and how we can convert duck and chicken through the adapter.

There are many kinds of ducks, so let’s define a duck interface and take mallard as an example. The mallard is also useful in strategic mode, check out my other post on how mallard stils strategic mode →Head First design Mode — Strategic mode

Public interface Duck {// call public void Quack(); // Fly public void Fly(); } public class GreenDuck: Duck {public void Fly() {Console.WriteLine(" GreenDuck "); } public void Quack() {console. WriteLine(" Quack "); }}Copy the code

Also we define a chicken interface, and a hen class

Public interface Chicken {// call public void Gobble(); // Fly public void Fly(); } public class Hen: Chicken {public void Gobble() {console. WriteLine(" Hen, clack "); } public void Fly() {Console.WriteLine(" Fly "); }}Copy the code

A duck and a hen sound different, now let’s let the hen pretend to be a duck, using the adapter mode how to do it. Let’s just look at the code

// </summary> public class HenAdapter: Duck {Chicken; public HenAdapter(Chicken chicken) { this.chicken = chicken; } public void Quack() {// call chicken.gobble (); } public void Fly() {// call chicken.fly (); }}Copy the code

Test hen adapter

As above, we used the hen adapter to match the hen to the duck, and the duck can also match the duck to the hen with the adapter. The adapter mode is defined as follows:

Adapter pattern: Replace the interface of one class with another interface that the customer expects. Adapters allow classes with incompatible interfaces to work together.

The decorator pattern, which looks similar to that of the adapter, is the behavior or responsibility of the wrapping object. Decorators may continue to be wrapped after they are wrapped. They do not replace interfaces, whereas adapters must transform interfaces.

The job of adaptation is to transform one interface into another, and while most adapters take the example of having one adapter wrap one adaptor, sometimes we need to have one adapter wrap multiple adaptors.

This actually refers to another pattern, the appearance pattern, and we often confuse the adapter pattern with the appearance pattern, so let’s talk about appearance pattern.

Second, appearance mode

Take home theater as an example. Home theater consists of many components, such as display screen, DVD, sound, light and so on.

When we want to watch a movie, we should turn on the display screen, turn on the DVD, turn on the sound, turn off the light and a series of actions, and write these actions into the invocation of class methods

            Screen screen = new Screen();
            DVD dvd = new DVD();
            SoundEngineer sound = new SoundEngineer();
            Light light = new Light();

            screen.Down();
            dvd.PlayDVD();
            sound.TurnOn();
            light.TurnOff();
Copy the code

As you can see, we have to call one of these methods every time we want to use them, and if we want to close them, we need to call one as well. What we need is a facade: by implementing a facade class that provides a more reasonable interface.

Let’s look at the code

public class HomeThreaterFacade { Screen screen; DVD dvd; SoundEngineer sound; Light light; public HomeThreaterFacade(Screen screen, DVD dvd, SoundEngineer sound, Light light) { this.screen = screen; this.dvd = dvd; this.sound = sound; this.light = light; } public void WatchMovie() {console. WriteLine("......") ); screen.Down(); dvd.PlayDVD(); sound.TurnOn(); light.TurnOff(); }}Copy the code

I won’t list the other class because it’s a printout, but the closing method is also very easy to implement.

Let’s test the effect:

Appearance pattern definition

Appearance pattern: Provides a unified interface for accessing a set of interfaces in a subsystem. The facade defines a high-level interface that makes the subsystem easier to use.

Appearance patterns follow a design principle

Rule of least knowledge: Talk to your closest friends.

This principle wants us to design so that we don’t couple too many classes together that changing one part of the system will affect other parts. The facade mode lets the user not care about all the subsystem components, making the customer simple and flexible. We can upgrade components in the facade without affecting the customer, who has only one friend, the facade.

Differences between adapter mode and appearance mode

From the examples above we might conclude that the difference between an adapter and a facade pattern is that an adapter wraps one class, while a facade can represent many classes

The adapter pattern turns one or more interfaces into the desired one. We generally fit one class, but special requirements can fit multiple classes to provide one interface. As classes are, a facade can also provide a simplified interface to a class that only has a complex interface. The difference between the two models lies in their intent. The intent of the adapter pattern is to replace the interface with a different interface, and the intent of the facade is to simplify the interface.