In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.
val inputString ="LOREM ipsum DOLOR sit amet"val lowerCasePrinter =Printer(lowerCaseFormatter)
lowerCasePrinter.printString(inputString)
val upperCasePrinter =Printer(upperCaseFormatter)
upperCasePrinter.printString(inputString)
val prefixPrinter =Printer { "Prefix: $it" }
prefixPrinter.printString(inputString)
Output
lorem ipsum dolor sit amet
LOREM IPSUM DOLOR SIT AMET
Prefix: LOREM ipsum DOLOR sit amet
The command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object. The command may then be executed immediately or held for later use.
Example:
interfaceOrderCommand {
funexecute()
}
classOrderAddCommand(valid:Long) : OrderCommand {
overridefunexecute() =println("Adding order with id: $id")
}
classOrderPayCommand(valid:Long) : OrderCommand {
overridefunexecute() =println("Paying for order with id: $id")
}
classCommandProcessor {
privateval queue =ArrayList<OrderCommand>()
funaddToQueue(orderCommand:OrderCommand): CommandProcessor=apply {
queue.add(orderCommand)
}
funprocessCommands(): CommandProcessor=apply {
queue.forEach { it.execute() }
queue.clear()
}
}
The state pattern is used to alter the behaviour of an object as its internal state changes.
The pattern allows the class for an object to apparently change at run-time.
Example
sealedclassAuthorizationStateobjectUnauthorized:AuthorizationState()
classAuthorized(valuserName:String) : AuthorizationState()
classAuthorizationPresenter {
privatevar state:AuthorizationState=Unauthorizedval isAuthorized:Boolean
get() =when (state) {
isAuthorized->trueisUnauthorized->false
}
val userName:String
get() {
val state =this.state //val enables smart casting of statereturnwhen (state) {
isAuthorized-> state.userName
isUnauthorized->"Unknown"
}
}
funloginUser(userName:String) {
state =Authorized(userName)
}
funlogoutUser() {
state =Unauthorized
}
overridefuntoString() ="User '$userName' is logged in: $isAuthorized"
}
Usage
val authorizationPresenter =AuthorizationPresenter()
authorizationPresenter.loginUser("admin")
println(authorizationPresenter)
authorizationPresenter.logoutUser()
println(authorizationPresenter)
Output
User 'admin' is logged in: true
User 'Unknown' is logged in: false
The visitor pattern is used to separate a relatively complex set of structured data classes from the functionality that may be performed upon the data that they hold.
Mediator design pattern is used to provide a centralized communication medium between different objects in a system. This pattern is very helpful in an enterprise application where multiple objects are interacting with each other.
val mediator =ChatMediator()
val john =ChatUser(mediator, "John")
mediator
.addUser(ChatUser(mediator, "Alice"))
.addUser(ChatUser(mediator, "Bob"))
.addUser(john)
john.send("Hi everyone!")
Output
John: Sending Message= Hi everyone!
Alice: Message received: Hi everyone!
Bob: Message received: Hi everyone!
请发表评论