Last active
May 13, 2017 20:01
-
-
Save aybarsyalcin/564a3606891d113981b5 to your computer and use it in GitHub Desktop.
Factory Pattern for Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Circle : Shape{ | |
func draw() { | |
print("Inside \(self.dynamicType)::draw() method.") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Rectangle: Shape { | |
func draw() { | |
print("Inside \(self.dynamicType)::draw() method.") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol Shape{ | |
func draw() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Square: Shape{ | |
func draw() { | |
print("Inside \(self.dynamicType)::draw() method.") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class ShapeFactory : NSObject{ | |
func getShape(shapeType: String) ->Shape? { | |
if (shapeType == ""){ | |
return nil | |
} | |
else if (shapeType == "Circle"){ | |
return Circle() | |
} | |
else if (shapeType == "Rectangle"){ | |
return Rectangle() | |
} | |
else if (shapeType == "Square"){ | |
return Square() | |
} | |
return nil | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let shapeFactory : ShapeFactory = ShapeFactory() | |
let shape1 = shapeFactory.getShape("Circle") | |
shape1?.draw() | |
let shape2 = shapeFactory.getShape("Rectangle") | |
shape2?.draw() | |
let shape3 = shapeFactory.getShape("Square") | |
shape3?.draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment