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
public class PredicateDemo { | |
static class Person { | |
String name; | |
int age; | |
String gender; | |
public Person(String name, int age, String gender) { | |
this.name = name; | |
this.age = age; |
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
public class SupplierDemo { | |
static class Car { | |
String brand; | |
int engineCC; | |
int wheelCount; | |
int price; | |
public Car(String brand, int engineCC, int wheelCount, int price) { | |
this.brand = brand; |
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 | |
var book1: Book = Book() | |
var executor: CommandExecutor = CommandExecutor() | |
executor.addCommand(BuyCommand(book1)) | |
executor.runCommand() | |
print("Current owner: \(book1.currentOwner!)") | |
executor.addCommand(GiftFriendCommand(book1, "Mahbub")) | |
executor.addCommand(SellCommand(book1)) |
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 CommandExecutor { | |
var nextCommands = [Command]() | |
var executedCommands = [Command]() | |
init() { | |
} | |
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 Command { | |
func execute() -> Void | |
} | |
public class BuyCommand : Command { | |
let book: Book | |
init(_ book: Book) { |
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 Book { | |
var currentOwner: String? | |
func buy() -> Void { | |
print("Buying the book...") | |
currentOwner = "Me" | |
} | |
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 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 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 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 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) |