Skip to content

Instantly share code, notes, and snippets.

@evnpr
Forked from omayib/StrategyPatterin.swift
Created December 23, 2016 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evnpr/db179119e6f5636a52b9ab8b6a8904fc to your computer and use it in GitHub Desktop.
Save evnpr/db179119e6f5636a52b9ab8b6a8904fc to your computer and use it in GitHub Desktop.
an implementation of strategy pattern on swift
//: Playground - noun: a place where people can play
import UIKit
//lets to trya strategy pattern!
protocol QCallKit{
func dial()
func endCall()
func accept()
func decline()
}
protocol QCallKitDelegate{
func conversationStarted(message: String)
func conversationClossed(message: String)
//other delegate here...
}
/*================== Agora and friends =================*/
class Agora : QCallKit {
private var delegate: QCallKitDelegate!
init(withDelegate: QCallKitDelegate) {
self.delegate = withDelegate
}
func dial(){
print("dialing using agora")
//other procedure here...
//assume the dialing is succeed and the conversation begin.
delegate.conversationStarted(message: "agora conversation is started")
}
func endCall(){
print("endCall using agora")
//long time conversation happen. now the user close the phone
//alot of procedure using agora to close the call.
//lets assume endCall is succed!
delegate.conversationClossed(message: "agora conversation is closed")
}
func accept(){
print("accept using agora")
}
func decline(){
print("decline using agora")
}
func iHateThisFunction(){
}
// other agora functions...
}
class AgoraHelper{
//....
}
/*================== ToxBox and friends =================*/
class TokBox : QCallKit {
private var delegate: QCallKitDelegate!
init(withDelegate: QCallKitDelegate) {
self.delegate = withDelegate
}
func dial(){
print("dialing using TokBox")
//other procedure here...
//assume the dialing is succeed and the conversation begin.
delegate.conversationStarted(message: "TokBox conversation is started")
}
func endCall(){
print("endCall using TokBox")
//long time conversation happen. now the user close the phone
//alot of procedure using TokBox to close the call.
//lets assume endCall is succed!
delegate.conversationClossed(message: "agora conversation is closed")
}
func accept(){
print("accept using TokBox")
}
func decline(){
print("decline using TokBox")
}
func ruwerFunction(){
}
// other TokBox functions...
}
class TokBoxHelper{
//....
}
/*================== OtherCallLibrary and friends =================*/
class OtherCallLibrary : QCallKit {
private var delegate: QCallKitDelegate!
init(withDelegate: QCallKitDelegate) {
self.delegate = withDelegate
}
func dial(){
print("dialing using OtherCallLibrary")
//other procedure here...
//assume the dialing is succeed and the conversation begin.
delegate.conversationStarted(message: "OtherCallLibrary conversation is started")
}
func endCall(){
print("endCall using OtherCallLibrary")
//long time conversation happen. now the user close the phone
//alot of procedure using OtherCallLibrary to close the call.
//lets assume endCall is succed!
delegate.conversationClossed(message: "OtherCallLibrary conversation is started")
}
func accept(){
print("accept using OtherCallLibrary")
}
func decline(){
print("decline using OtherCallLibrary")
}
func wtfFunc(){
}
// other OtherCallLibrary functions...
}
class OtherCallLibraryHelper{
//
}
/*============== packaging all into CallSDK ==================*/
class CallSDK{
private var callKitDelegate: QCallKitDelegate
private var callKit: QCallKit
init(withDelegate:QCallKitDelegate) {
self.callKitDelegate = withDelegate
self.callKit = OtherCallLibrary(withDelegate: callKitDelegate)
}
func dialNow(userId: Int){
self.callKit.dial()
}
func closeConversation(){
self.callKit.endCall()
}
func reject(){
self.callKit.decline()
}
func answer(){
self.callKit.accept()
}
}
/*============== lets see the implementation =================*/
class CallVC: UIViewController, QCallKitDelegate {
var callSDK: CallSDK!
override func viewDidLoad() {
callSDK = CallSDK(withDelegate: self)
}
func callButtonDidTap(){
callSDK.dialNow(userId: 123)
}
func rejectButtonDidTap(){
callSDK.reject()
}
func answerButtonDidTap(){
callSDK.answer()
}
func closeButtonDidTap(){
callSDK.closeConversation()
}
// MARK : thoose function come from QCallKitDelegate
func conversationStarted(message: String){
print("from call vc - \(message)")
}
func conversationClossed(message: String){
print("from call vc - \(message)")
}
}
let callVC = CallVC()
callVC.viewDidLoad()
// mulai menelpon
callVC.callButtonDidTap()
callVC.closeButtonDidTap()
// menjawab telpon
// menutup telpon
// mereject telpon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment