This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | |
| // Bridge pattern is good for run time binding of implementation | |
| var openGLApi: OpenGLAPI = OpenGLAPI() | |
| var directXApi: DirectXAPI = DirectXAPI() | |
| var circle: Circle = Circle(10, 10, 10, openGLApi) | |
| circle.draw() | |
| circle = Circle(10, 5, 4, directXApi) |
This file contains hidden or 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 User { | |
| var posts: [Post] = [Post]() | |
| func addPost(_ post: Post) -> Void { | |
| posts.append(post) | |
| } | |
| func analyzePosts(_ strategy: PostAnalyzeStrategy) -> Void { |
This file contains hidden or 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 PostAnalyzeStrategy { | |
| func analyze(_ posts: [Post]) -> Void | |
| } | |
| class SadnessAnalysisStrategy : PostAnalyzeStrategy { | |
| func analyze(_ posts: [Post]) -> Void { | |
| let postCount: Int = posts.count | |
| var totalSadCount: Int = 0 |
This file contains hidden or 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 user: User = User() | |
| user.addPost(Post("Post 1", 10, 5, 5)) | |
| user.addPost(Post("Post 2", 10, 5, 5)) | |
| user.addPost(Post("Post 3", 10, 0, 0)) | |
| user.addPost(Post("Post 4", 0, 0, 10)) | |
| user.analyzePosts(LaughAnalyzeStrategy()) |
This file contains hidden or 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 Book { | |
| var currentOwner: String? | |
| func buy() -> Void { | |
| print("Buying the book...") | |
| currentOwner = "Me" | |
| } | |
This file contains hidden or 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 Command { | |
| func execute() -> Void | |
| } | |
| public class BuyCommand : Command { | |
| let book: Book | |
| init(_ book: Book) { |