Last active
August 25, 2019 13:33
-
-
Save emrdgrmnci/a87e4d755cbcd863b613b18214ec225d to your computer and use it in GitHub Desktop.
FactoryDesignPattern
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 UIKit | |
//String tipinde değer döndüren Draw() fonksiyonuna sahip bir protokol oluşturdum. | |
public protocol Shape { | |
func draw() -> String | |
} | |
//Enumerator tipinde 3 adet geometrik sekil tanımladım | |
enum ShapeType { | |
case square | |
case circle | |
case rectangle | |
} | |
//Shape protokolünden kalıttığım ShapeFactory classım var | |
//draw() fonksiyonunu burada eziyor. | |
public class ShapeFactory : Shape { | |
public func draw() -> String { | |
return "Ciz" | |
} | |
//ShapeFactory classımında ki draw() fonksiyonumun parametresi, | |
//ShapeType tipinde bir shapeType değişkeni olacak ve | |
//return tipi basitçe bir String değer olacak. | |
//switch case bloğumda enumerator dan 3 adet geometrik şeklim ve | |
//bu şekillere ait (alt satırlarda tanımlı) | |
//3 adet classım return ediliyor. | |
func draw(_ shapeType : ShapeType) -> Shape { | |
switch shapeType { | |
case ShapeType.square: | |
return Square() | |
case ShapeType.circle: | |
return Circle() | |
case ShapeType.rectangle: | |
return Rectangle() | |
} | |
} | |
} | |
//3 adet Shape protokolünden kalıtılan Square, Circle, Rectangle classım var | |
//Bu classlar Shape protokolünde ki draw() fonksiyonunu ezip, | |
//String tipte değerler döndürüyor | |
public class Square : Shape { | |
public func draw() -> String { | |
return "Square is drawn" | |
} | |
} | |
public class Circle : Shape { | |
public func draw() -> String { | |
return "Circle is drawn" | |
} | |
} | |
public class Rectangle : Shape { | |
public func draw() -> String { | |
return "Rectangle is drawn" | |
} | |
} | |
//sf isimli ilk ShapeFactory nesnemi oluşturuyorum. | |
let sf = ShapeFactory() | |
//tanımladığım sf sabitim üzerinden (ShapeFactory) | |
//parametre alan (ShapeType tipinde shapeType) draw fonksiyonumu | |
//çağırıp içindeki String değerleri bastırıyorum. | |
let shape = sf.draw(ShapeType.Circle) | |
print(shape.draw()) | |
let shape1 = sf.draw(ShapeType.Square) | |
print(shape1.draw()) | |
let shape2 = sf.draw(ShapeType.Rectangle) | |
print(shape2.draw()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment