Zookeeper
The following describes the adapter mode:
Definition: converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
Applicable: in software development, that is, when the data and behavior of the system are correct, but the interface is inconsistent, we should consider using the adapter, the purpose is to match an existing object out of the control scope with an interface. The adapter mode is mainly used to reuse some existing classes, but the interfaces are inconsistent with the requirements of the Reuse Environment.
Note: Use adapter mode adaptation when both parties are not easy to modify.
Open-closed principle: software entities (classes, modules, functions, etc.) should be extensible, but cannot be modified. This principle has two features: one is 'open for extension' and the other is 'Closed for change '. When we first wrote the code, we assumed that the change would not happen. When a change occurs, we create an abstraction to isolate similar changes that occur later. That is, in the face of requirements, changes to the program are made by adding code, rather than changing the existing code. This is the spirit of the open-closed principle. Following this principle can bring about the huge benefits that object-oriented technology claims, that is, it can be maintained, scalable, reusable, and flexible.
Structure:
Basic code:
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace adapter Mode
{
Class Program
{
Static void main (string [] ARGs)
{
Target target = new adapter ();
Target. Request ();
Console. Read ();
}
}
Class target
{
Public Virtual void request ()
{
Console. writeline ("common request ");
}
}
Class adaptee
{
Public void specificrequest ()
{
Console. writeline ("special request ");
}
}
Class adapter: Target
{
Private adaptee = new adaptee ();
Public override void request ()
{
Adaptee. specificrequest ();
}
}
}