What is adapter mode?
The adapter mode makes it possible for those classes that cannot work together because of incompatible interfaces.
Simply put, it is the things that need to be in front of, do not use, and short time can not transform it, so we will find a way to adapt it. The adapter pattern is primarily used in situations where you want to reuse some existing classes, but the interfaces are inconsistent with the reuse environment requirements.
code example:
<summary>///Define client-expected interface///</summary> public class Target {///<summary> Use virtual adornments so that subclasses can rewrite//</summary> public virtual void Request () {Console.wri Teline ("This is a common request"); }}///<summary>///define the classes that need to be adapted////</summary> public class Adaptee {public void Spec Ificrequest () {Console.WriteLine ("This is a special request."); }}///<summary>//define Adapter///</summary> public class Adapter:target {//Establish a private ad Eptee Object Private Adaptee adaptee = new Adaptee (); <summary>/////By rewriting the request () method on the surface, it becomes the actual call to Specificrequest ()///</summary> public ov erride void Request () {adaptee. Specificrequest (); }}//client code class program {static void Main (string[] args) {//For the client, the call is the target's RequeST () Target target = new Adapter (); Target. Request (); Console.read (); } }
Applicable scenarios:1, the system needs to use the existing classes, and these classes of interfaces do not conform to the interface of the system.
2. Want to create a reusable class to work with some classes that are not much related to each other, including some that might be introduced in the future.
3, two classes do things the same or similar, but with different interfaces.
4, the old system development class has implemented some functions, but the client can only be accessed in the form of a different interface, but we do not want to manually change the original class.
5, the use of third-party components, component interface definitions and their own definition of the different, do not want to modify their own interfaces, but to use the functionality of the third-party component interface
Other design modes:
Design mode-agent mode (proxy)
Design pattern-Decorative mode (Decorator)
Design pattern--Dimitri Law (LOD/LKP)
Design Patterns-Dependency reversal principle
Design mode-open/Close principle (OCP)
Design Patterns-Single Responsibility principle (SRP)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Design mode-Adapter mode (Adapter)