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 | |
public class Shape { | |
let graphicsApi: GraphicsAPI | |
init(_ graphicsApi: GraphicsAPI) { | |
self.graphicsApi = graphicsApi | |
} | |
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 GraphicsAPI { | |
func drawRectangle(_ x: Int, _ y: Int, _ width: Int, _ height: Int) | |
func drawCircle(_ x: Int, _ y: Int, _ radius: Int) | |
} | |
class DirectXAPI : GraphicsAPI { | |
func drawRectangle(_ x: Int, _ y: Int, _ width: Int, _ height: Int) { |
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 | |
public class Circle : Shape { | |
var x: Int = 0 | |
var y: Int = 0 | |
var radius: Int = 0 | |
convenience init(_ x: Int, _ y: Int, _ radius: Int, _ graphicsApi: GraphicsAPI) { | |
self.init(graphicsApi) | |
self.x = x |
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 | |
let DEMO_TEXT: String = "My Name is Mahbub. I like to use design pattern as much as possible because design patterns helps to write scalable code." | |
let ethernetConn: EthernetConnection = EthernetConnection() | |
let mobileDataConn: MobileDataConnection = MobileDataConnection() | |
var adapter: ThunderboltAdapter = ThunderboltAdapter(ethernetConn) | |
adapter.sendData(DEMO_TEXT) |
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
class ThunderboltAdapter { | |
var connection: Connection | |
init(_ connection: Connection) { | |
self.connection = connection | |
} | |
func sendData(_ data: String) { | |
if connection is EthernetConnection { |
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
class EthernetConnection : Connection { | |
func makeReady() { | |
print("Making Ethernet Ready...") | |
} | |
func isReady() -> Bool { | |
return arc4random_uniform(2) == 0 | |
} | |
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 Connection { | |
func makeReady() -> Void | |
func isReady() -> Bool | |
func sendPacket(_ data: String) -> Void | |
} |
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 guiBuilder: GUIBuilder = GUIBuilder(platform: "Windows") | |
let window: Window = guiBuilder.buildWindow() | |
window.setTitle("Mahbub") | |
window.show() | |
let button: Button = guiBuilder.buildButton() | |
button.setTitle("Connect") | |
button.show() |
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
class GUIBuilder { | |
private var platform: String | |
private var guiFactory: AbstractGUIFactory? | |
init(platform: String) { | |
self.platform = platform | |
} | |
func initGuiFactory() -> Void { | |
if nil != guiFactory { return } |
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
class WinFactory : AbstractGUIFactory { | |
func createButton() -> Button { | |
return WinButton() | |
} | |
func createWindow() -> Window { | |
return WinWindow() | |
} | |
} |