Skip to content

Instantly share code, notes, and snippets.

View iamcrypticcoder's full-sized avatar

Mahbubur Rahman iamcrypticcoder

View GitHub Profile
class GUIBuilder {
private var platform: String
private var guiFactory: AbstractGUIFactory?
init(platform: String) {
self.platform = platform
}
func initGuiFactory() -> Void {
if nil != guiFactory { return }
protocol Button {
func setTitle(_ title: String) -> Void
func show() -> Void
}
protocol Window {
func setTitle(_ title: String) -> Void
func show() -> Void
}
class WinButton: Button {
var title: String?
func setTitle(_ title: String) -> Void {
self.title = title
}
func show() -> Void {
print("Showing Windows style button [Title: \(self.title!)]")
}
protocol AbstractGUIFactory {
func createButton() -> Button
func createWindow() -> Window
}
class WinFactory : AbstractGUIFactory {
func createButton() -> Button {
return WinButton()
}
func createWindow() -> Window {
return WinWindow()
}
}
class GUIBuilder {
private var platform: String
private var guiFactory: AbstractGUIFactory?
init(platform: String) {
self.platform = platform
}
func initGuiFactory() -> Void {
if nil != guiFactory { return }
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()
import Foundation
protocol Connection {
func makeReady() -> Void
func isReady() -> Bool
func sendPacket(_ data: String) -> Void
}
class EthernetConnection : Connection {
func makeReady() {
print("Making Ethernet Ready...")
}
func isReady() -> Bool {
return arc4random_uniform(2) == 0
}
class ThunderboltAdapter {
var connection: Connection
init(_ connection: Connection) {
self.connection = connection
}
func sendData(_ data: String) {
if connection is EthernetConnection {