How Does Adapter Pattern Work?
I recently read about Adapter pattern in my semester course. I have understood the concept of providing the common interface so that the third party APIs can work in our system.
But how does it will work in coding, lets say that i have two third party SDK, such as:
A(String name)
A1(String Firstname,String Lastname)开发者_运维问答
Now i produce the common interface called as, say, IAAdapater
. And now write the code in that, now my question is, how does one call A(String name)
or A1(String Firstname,String Lastname)
through IAAdapater
?
Correct me if I'm wrong!
Edit I think the other answer is closer to what you are looking for - Im actually describing a Facade here.
Your IAAdapter needs to expose the methods A(String name) and A1(String Firstname, String Lastname)
You then need an implementation of this interface that delegates to the underlying objects.
IAAdaptor{
A(string name);
A1(string firstname, string lastnae);
}
Adaptor:IAAdaptor{
A(string name){}
A1(string firstname, string lastnae){}
}
interface IAAdapater {
A(firstname, lastname)
}
// Adapter for A SDK
A_adapter implements IAAdapter {
A(firstname, lastname) {
call A(firstname + ' ' + lastname)
}
}
// Adapter for A1 SDK
A1_adapter implements IAAdapter {
A(firstname, lastname) {
call A1(firstname, lastname)
}
}
Now you only have to worry about one API: firstname, lastname. The different adapters will handle the rest.
call A_adapter.A('will', 'smith');
call A1_adapter.A('will', 'smith');
Please let me know if I misunderstood the question.
精彩评论