You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * The Target defines the domain-specific interface used by the client code. */classTarget{publicrequest(): string{return'Target: The default target\'s behavior.';}}functionclientCode(target: Target){console.log(target.request());}
同时原来的代码还存在这样的类,可能会被其他 clinet 使用,如下:
/** * The Adaptee contains some useful behavior, but its interface is incompatible * with the existing client code. The Adaptee needs some adaptation before the * client code can use it. */classAdaptee{publicspecificRequest(): string{return'.eetpadA eht fo roivaheb laicepS';}}functionclientCodeOther(target: Adaptee){console.log(target.specificRequest());}
/** * The Adapter makes the Adaptee's interface compatible with the Target's * interface. */classAdapterextendsTarget{privateadaptee: Adaptee;constructor(adaptee: Adaptee){super();this.adaptee=adaptee;}publicrequest(): string{constresult=this.adaptee.specificRequest().split('').reverse().join('');return`Adapter: (TRANSLATED) ${result}`;}}constadaptee=newAdaptee();constadapter=newAdapter(adaptee);clientCode(adapter);
The text was updated successfully, but these errors were encountered:
在原有的代码片段中,这样使用 Target 类的:
同时原来的代码还存在这样的类,可能会被其他 clinet 使用,如下:
现在,我的客户端代码中需要在 clientCode 中使用 Adaptee 的 specificRequest 的输出结果,那么在不修改 clientCode 的情况下,我们可以使用 适配器模式 来实现:
The text was updated successfully, but these errors were encountered: