swift-Command设计模式

命令模式的应用

主要是涉及场景,交互时需要键入命令的方式去实现可撤销,可以回滚的操作如何实现使用命令模式可较好的解决这样的问题.例1 如玩家可以在进行操作左右上下操作后,并能够撤销操作,回到上一步的状态

实现以游戏操作玩家的方式举例

1.定义协议(接口)
enum  ActionType:String{
    case  left  = "left",
    right = "right",
    up = "up",
    down = "down",
    quite  = "quite"
}
 protocol   BaseCommand{
    func execute() -> ActionType
}
2.抽象操作
class   Action:BaseCommand{
    var  commandtype:ActionType = ActionType.quite
    var   sprite:HeroSprite!
    init(actiontype: ActionType,sprite:HeroSprite) {
        self.sprite = sprite
        self.commandtype = actiontype
    }
    func execute() -> ActionType {
        return   self.commandtype
    }
}
class LetfAction: Action {
    init(sprite:HeroSprite) {
        super.init(actiontype: ActionType.left,sprite: sprite)
    } 
}
class RightAction: Action {
    init(sprite:HeroSprite) {
        super.init(actiontype: ActionType.right,sprite: sprite)
    }
}
class UpAction: Action {
    init(sprite:HeroSprite) {
        super.init(actiontype: ActionType.up,sprite: sprite)
    }
}
class DownAction: Action {
    init(sprite:HeroSprite) {
        super.init(actiontype: ActionType.down,sprite: sprite)
    }
}
3.具有这些操作的具象类
class  HeroSprite {
    var   leftaction:LetfAction!
    var   rightaction:RightAction!
    var   upaction:UpAction!
    var   downaction:DownAction!
    
    var   curaction:ActionType = ActionType.quite {
        willSet{
            updatestate(type: newValue)
            
        }
        
    }
    //撤销 操作的队列 支持最大返回操作数
    let    maxoperatecount =  10
    
    var   quennsactions = [ActionType]()
    
    var  name:String!
    init(name:String) {
        self.name = name
        leftaction = LetfAction.init(sprite: self)
         rightaction = RightAction.init(sprite: self)
         upaction = UpAction.init(sprite: self)
         downaction = DownAction.init(sprite: self)
        
    }
    func   left() {
        
        self.curaction =   self.leftaction.execute()
        addAction(actiontype: self.curaction)
    }
    func   right() {
        
        self.curaction =   self.rightaction.execute()
         addAction(actiontype: self.curaction)
    }
    func   up() {
        self.curaction =   self.upaction.execute()
         addAction(actiontype: self.curaction)
    }
    func   down() {
         self.curaction =   self.downaction.execute()
         addAction(actiontype: self.curaction)
    }
    func   updatestate(type:ActionType){
        switch type {
        case .quite:
            print("\(self.name): 静止中")
                break
        case .left:
                   print("\(self.name): 左边移动")
            break
        case .right:
                   print("\(self.name): 右边移动")
            break
        case .up:
                   print("\(self.name): 上移动 ")
            break
        case .down:
                   print("\(self.name): 下移动")
            break
        }
        
    }
    //撤销
    func   cancleOperation(){
        if self.quennsactions.count == 0 {
            print("可返回操作置空 已置位静止状态")
            self.curaction = ActionType.quite
            return
        }
        self.quennsactions.remove(at: self.quennsactions.count - 1)
        self.curaction = self.quennsactions.last  ?? ActionType.quite
        print("恢复状态到:" + "\(self.curaction)")
        
    }
    private   func  addAction(actiontype:ActionType){
        
        if self.quennsactions.count == maxoperatecount {
            
            self.quennsactions.remove(at: 0)
            self.quennsactions.append(actiontype)
        }
        else{
            self.quennsactions.append(actiontype)
        }
       
    }
    
}
4.测试
let    sprite = HeroSprite.init(name: "战士")
sprite.left()
sprite.right()
sprite.up()
sprite.up()
sprite.up()
sprite.up()
sprite.up()
sprite.up()
sprite.down()
sprite.cancleOperation()
sprite.down()
sprite.cancleOperation()
sprite.cancleOperation()
sprite.cancleOperation()
sprite.cancleOperation()

swift-Command设计模式_第1张图片
image.png

你可能感兴趣的:(swift-Command设计模式)