Today I thought about Factory Design Pattern in Swift, and I do an implementation with Extension.
My post is about if anyone have any observation or issue about this implementation violate any convention about Factories? Can I do this way?
I did it because I thought the statics function in my object not smell good, and for who will do maintenance it will confuse, and because this I split to Extension.
let kToastMessageTurnType:String = "kToastMessageTurnType";
let kWhoStartTheGameMessageType:String = "kWhoStartTheGameMessageType";
class ToastMessage {
private var gameState:GameState;
private(set) var message:String = "";
private(set) var isFoul:Bool = false;
init(gameState:GameState, type:String, isFoul:Bool)
{
self.gameState = gameState;
self.isFoul = isFoul;
self.message = processMessage(type)
}
private func processMessage(type:String)->String
{
let textLocalized:TextLocalized;
switch type
{
case GSFoulTypeBallEightPocketedInBreakShot:
textLocalized = TextStartedIncorrectly(playersInfo: self.gameState);
case GSFoulTypeBreakAtLeastFourBumpers:
textLocalized = TextStartedIncorrectly(playersInfo: self.gameState);
case kToastMessageTurnType:
textLocalized = TextTurnMessage(playersInfo: self.gameState);
case kWhoStartTheGameMessageType:
textLocalized = TextWhoStartMessage(playersInfo: self.gameState);
default:
return "";
}
return textLocalized.text
}
}
//Factory from ToastMessage
extension ToastMessage
{
class func getFoulToastMessage(type:String)->ToastMessage
{
return ToastMessage(gameState: GameState.shared, type: type, isFoul: true);
}
class func getNotifyTurnMessagetMessage()->ToastMessage
{
return ToastMessage(gameState: GameState.shared, type: kToastMessageTurnType, isFoul: false);
}
class func getWhoStartTheGameMessage()->ToastMessage
{
return ToastMessage(gameState: GameState.shared, type: kWhoStartTheGameMessageType, isFoul: false);
}
}