Overview
In software systems, due to changes in the application environment, we often need to put "some existing objects" in the new environment for application, however, the interfaces required by the new environment are not met by these existing objects. So how should we deal with this "Migration change "? How can we make good use of the existing object and meet the interfaces required by the new application environment? This is the Adapter mode described in this article.
Intention
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.
Structure chart
Figure 1 Structure of the Adapter Mode
Figure 2 Structure of the object's Adapter Mode
Examples in life
The adapter mode allows you to convert an interface of a class into another interface that the customer expects, so that classes that originally cannot work together due to interface incompatibility can work together. The wrench provides an example of an adapter. A hole is mounted on the spine and each side of the spine is of the same size. In the United States, the typical side lengths are 1/2 and 1/4. Obviously, if no adapter is used, 1/2 of the spine teeth cannot fit 1/4 of holes. A 1/2 to 1/4 adapter has a 1/2 overcast slot to pin a 1/2 tooth, while a 1/4 positive slot comes into the 1/4 wrench.
Figure 3 examples of adapter objects using wrenches
Example diagram Use Case Design:
Create User. cs First
public class User { public int ID { get; set; } public string Name { get; set; } public string Age { get; set; } }
Then create IDBHelper. cs
Public interface IDBHelper {// <summary> // connection string /// </summary> /// <returns> </returns> string DBConnectString (); /// <summary> /// returned result set /// </summary> /// <param name = "strsql"> </param> /// <returns> </returns> DataSet GetUserGroup (string strsql ); /// <summary> /// return the number of inserted rows /// </summary> /// <param name = "user"> </param> /// <returns ></returns> int InsertUser (User user );}
Then create SQLHelper. cs:
Public class SQLHelper: IDBHelper {# region IDBHelper member public string DBConnectString () {return "SQL Connect String";} public DataSet GetUserGroup (string strsql) {DataSet ds = new DataSet (); return ds;} public int InsertUser (User user User) {return 1 ;}# endregion}
Then create OracleHelper. cs:
Public class OracleHelper: IDBHelper {# region IDBHelper member public string DBConnectString () {return "Oracle Connect String. ";}public DataSet GetUserGroup (string strsql) {DataSet ds = new DataSet (); return ds;} public int InsertUser (User user) {return 1 ;}# endregion}
Then create DataContent. cs:
Public class DataContent: IDBHelper {private IDBHelper DbHelper = GetDBHelper (); public static IDBHelper GetDBHelper () {string strClass = ConfigurationSettings. appSettings ["DBHeper"]. toString (); Assembly assembly = Assembly. load ("Adapter"); IDBHelper dbHelper = assembly. createInstance (strClass) as IDBHelper; return dbHelper;} # region IDBHelper member public string DBConnectString () {return DbHelper. DBConnectString ();} public System. data. dataSet GetUserGroup (string strsql) {return DbHelper. getUserGroup (strsql);} public int InsertUser (User user) {return DbHelper. insertUser (user) ;}# endregion}
Add the following code to App. config:
<appSettings> <add key="DBHeper" value="Adapter.SQLHelper"/> <!--<add key="DBHeper" value="Adapter.OracleHelper"/>--> </appSettings>
Let's look at our calling program:
public partial class Run : Form { public Run() { InitializeComponent(); } private void btnRun_Click(object sender, EventArgs e) { DataContent dataContent = new DataContent(); rtbResult.AppendText(dataContent.DBConnectString()); } }
Result: