设计模式--工厂模式

2020-12-30  本文已影响0人  caoww

简述

实现方式

// 提供产品的抽象类
public class IShareService {
    fun share(context : Context, shareText: String, callback ShareCallback)
    
    fun destroy()
}

// 分享结果的回调
public interface ShareCallback{
    fun success(text: String)
    fun feild(text: String)
    fun cancel()
}

// 分享渠道 如:微信,QQ
public enum ShareType {
    WeChat,
    QQ // 此处可以任意扩展
}

// 实现QQ分享
public class QQShareService : IShareService {
    override fun share(context: Context, shareText: String, callback: ShareCallback) {
          // 此处做分享操作. 关注分享回调
          QQShare.share().callback() {
                fun success(text: String) {
                    callback.success(text)
                }
                fun feild(text: String) {
                    callback.feild(text)
                }
                fun cancel() {
                     callback.cancel()
                }
          }
    }
    
      override fun destroy() {
        // 释放资源
      }
}

// 实现微信分享
public class WeChatShareService : IShareService {
    override fun share(context: Context, shareText: String, callback: ShareCallback) {
          // 此处做分享操作. 关注分享回调
          WeChat.share().callback() {
                fun success(text: String) {
                    callback.success(text)
                }
                fun feild(text: String) {
                    callback.feild(text)
                }
                fun cancel() {
                     callback.cancel()
                }
          }
    }
    
      override fun destroy() {
        // 释放资源
      }
}

// 提供分享实例
public object IShareFactory {
      fun createShareService(shareType: ShareType): IShareService {
          var iShareType:IShareService? = null
          when(shareType) {
                ShareType.QQ -> iShareType = QQShareService()
                ShareType.WeChat -> iShareType = WeChatShareService()
          }
          if (iShareType == null) {
              throw RuntimeException("创建实例失败,检查分享类型")
          }
          return iShareType!!
      }
}


// 使用方式

class MainActivity : AppCompatActivity() {
    private var iShareServie: IShareService? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onClick(view: View?) {
        when(view?.id) {
            R.id.qq->{
                share(ShareType.QQ)
            }
             R.id.wechat->{
                share(ShareType.WeChat)
            }
        }
    }

    private share(shareType: ShareType) {
        iShareServie = IShareFactory. createShareService(shareType)
        iShareServie?.share(this, "分享内容", object: ShareCallback {
                fun success(text: String) {
                    // 分享成功
                }
                fun feild(text: String) {
                    // 分享失败
                }
                fun cancel() {
                     // 分享取消
                }
        })
    }

    override fun onDestroy() {
          // 销毁
          iShareServie?.destroy()
    }
}
上一篇下一篇

猜你喜欢

热点阅读