adapter mode and appearance mode of head design mode
Objective:
Before we've talked about decorator patterns, wrapping objects up and giving them new responsibilities, we also wrap objects in this chapter, just to make them look like they're not something else. This allows the interface of the class to be converted into the desired interface in the design to implement the same interface, and another pattern will be described, wrapping the object to simplify the interface. 1. Introduction to the adapter 1.1 object-oriented adapters
Real-world adapters such as AC adapters between American plugs and European-style sockets. What is an object-oriented adapter?
An object-oriented adapter translates an interface into another interface to meet customer expectations.
For example, an existing software system, hopefully it will work with a new vendor class library, but the interface is different from the old one, and you don't want to change the existing code, so this time you need an adapter to do the docking of the existing system and the new vendor class library. 1.2 adapter in use
We give examples of ducks in the strategy model. Look at a simplified version of the example. 1.2.1 Duck Interface
Publicinterface Duck
{
void quack ();//ducks are called
void Fly ();//Ducks Fly
}
1.2.2 Duck Sub Class (Green Head duck) Realization
Publicclass Mallardduck:duck
{public
void quack ()
{
Console.WriteLine ("Quack");
}
public void Fly ()
{
Console.WriteLine ("Fly");
}
1.2.3 New Bird interface and concrete implementation
Publicinterface Turkey
{
void gobble ();/giggle
void Fly ();//Fly
}
publicclass Wildturkey: Turkey
{public
void gobble ()
{
Console.WriteLine ("Gobblegobble");
}
public void Fly ()
{
Console.WriteLine ("I ' m Flyinga short distance");
}
1.2.4 Adapter
Publicclass turkeyadapter:duck//implements the type interface
{
Turkey Turkey that you want to convert;
Public Turkeyadapter (Turkey Turkey)//Gets the object reference of the adapter and uses the construct to get the reference
{
this.turkey = Turkey;
}
The public void quack ()//implements all methods {Turkey in the interface
. Gobble ();
}
public void Fly ()
{for
(var i=0;i<5;i++)
Turkey. Fly ();
}
1.2.5 Test
Mallardduckduck = new Mallardduck ();
Wildturkey Turkey = Newwildturkey ();
Duck Turkeyadapter = Newturkeyadapter (Turkey);
Console.WriteLine ("The Turkeysyas ...");
Turkey. Gobble ();
Turkey. Fly ();
Console.WriteLine ("The Ducksays ...");
Turkeyadapter.quack ();
Turkeyadapter.fly ();
Console.read ();
The results are as follows:
1.3 Adapter Mode Resolution
The client uses the adapter in the following process:
L The client invokes the adapter through the target interface to make a request to the adapter.
The L adapter converts the request to one or more calling interfaces of the adapter using the adaptor interface.
L The customer receives the result of the call.
2. Define Adapter Mode
Adapter Mode: Converts the interface of a class into another interface that the customer expects. Adapters enable classes that are incompatible with the original interface to work seamlessly.
This pattern can be transformed by creating adapters to allow incompatible interfaces to be compatible, allowing the client to decouple from the implemented interface. Its class diagram is as follows:
This adapter is filled with the principles of OO design: Use object composition to modify the interface to package the adapter. Any subclass that is a companion can be used with the adapter. The pattern is to bind the customer and the interface, not to implement the binding.
There are actually two types of adapters: Object adapters and Class adapters. The class adapter is implemented through multiple inheritance, and the object adapter passes the request to the adapter using a combination of methods.
Class Adapter diagram:
3. Define the appearance pattern
Appearance mode: Provides a unified interface for accessing a group of interfaces of a subsystem. The appearance defines a high-level interface that makes the subsystem easier to use.
The appearance pattern allows us to avoid tight coupling between the customer and the subsystem. The class diagram is as follows:
4. The principle of minimum knowledge
Minimum Knowledge principle: Only talk to your close friends.
The minimum knowledge principle tells us to reduce the interaction between objects. The principle is that when designing, do not allow too many classes to be coupled together, lest you modify a part of the system and affect other parts.
How to avoid having too many classes coupled together. The main guidelines are as follows:
In the case of objects, in the method of the object, we should only invoke methods that belong to the following ranges:
L The object itself
L An object that is passed in as a parameter to a method
L any object created or instantiated by this method
Any component of the L object
The following example:
(Don't use this principle) get the thermometer object from the weather station and then get the problem from the thermometer object
Publicfloat gettemp ()
{
thermometerthermometer=station. Getthermometer ();
return thermometer. Gettemperature ();
}
(using this principle) we add a method to the weather station to request the temperature from the thermometer. Can reduce the data of the classes we rely on.
Publicfloat gettemp ()
{return
station. Gettemperature ();
}
5. Summary
L You need to use an adapter when you need to use an existing class and its interface does not meet your needs.
L Use appearance when it is necessary to simplify and unify a large interface or a group of responsible interfaces.
L Adapters change the interface to meet customer expectations.
The L-Look will decouple the customer from a complex subsystem.
L The difficulty of implementing an adapter is the size and complexity of the target interface.
L Implement a look, you need to combine subsystems into skins, and then delegate work to subsystem execution.
The L adapter is divided into object adapters and class adapters, and class adapters require multiple inheritance.
L can achieve more than one appearance for a subsystem.
The L adapter wraps an object to change its interface; The adorner wraps an object to add new behavior and responsibility, while the appearance "wraps" the object to simplify its interface.