interface ZhifubaoInerface { zhifubaoShare ( ) : void ; } interface WeixinInterface { weixinShare ( ) : void ; } interface AdapterInterface { share ( ) : void ; } interface MergeSdk extends ZhifubaoInerface , WeixinInterface { } type platform = 'weixin' | 'zhifubao' ; class WeixinSdk implements WeixinInterface { weixinShare ( ) { console . log ( '微信分享' ) ; } } class ZhifubaoSdk implements ZhifubaoInerface { zhifubaoShare ( ) { console . log ( '支付宝分享' ) ; } } class Adapter implements AdapterInterface { constructor ( ) { this . sdk = this . getPlatfromSdk ( ) ; } private sdk : MergeSdk ; private getPlatform ( ) { return 'weixin' ; } private getPlatfromSdk ( ) { const map = { weixin : WeixinSdk , zhifubao : ZhifubaoSdk } ; const platform = this . getPlatform ( ) ; return new map [ platform ] ( ) as MergeSdk ; } public share ( ) { const platform = this . getPlatform ( ) ; switch ( platform ) { case 'weixin' : this . sdk . weixinShare ( ) ; break ; case 'zhifubao' : this . sdk . zhifubaoShare ( ) ; break ; default : console . log ( '此方法不存在' ) ; } } } const adapter = new Adapter ( ) ; adapter . share ( ) ;