Java

Topic: Design pattern

Explain the Adapter pattern?

The Adapter pattern is typically used to allow the reuse of a class that is similar, butnot the same, as the class the client class would like to see. Typically the originalclass is capable of supporting the behavior the client class needs, but does not havethe interface the client class expects, and it is not possible or practical to alter theoriginal class. Perhaps the source code is not available, or it is used elsewhere andchanging the interface is inappropriate.Here is an example that wraps OldClass so a client class can call it using amethod, NewMethod() defined in NewInterface:public class OldClassAdapter implements NewInterface {private OldClass ref;public OldClassAdapter(OldClass oc){ref = oc;}public void NewMethod(){ref.OldMethod();}}

Browse random answers: