Skip to main content

Decorator và ứng dụng thực tế trong swift

Chào mọi người, hôm nay mình sẽ giới thiệu về một pattern khá phổ biến - decorator . Nói một cách vắn tắt, đây là pattern thuộc nhóm cấu trúc và sử dụng composition để tạo ra đối tượng ta mong muốn một cách linh hoạt. Nghe có vẻ hơi...lý thuyết, nhưng mình sẽ đi ngay vào phần tình huống và cách thức giải quyết dựa vào decorator để các bạn dễ dàng hình dung hơn.
Vấn đề gặp phải:
Giả sử công ty giao cho bạn 1 task khá to, dựa vào kinh nghiệm bản thân, bạn chia nhỏ để trị. Khi ấy bạn vừa dễ dàng báo cáo sếp, vừa dễ dàng thực thi công việc. Bạn chia task to đó thành 3 task nhỏ A, B, C. Và giờ bạn bắt đầu triển khai từng task con đó.
Show me the code:
Bước đầu tiên, bạn định hình công việc cần làm bằng cách tạo interface.
protocol Process {
    typealias Handler = () -> Void
    func run(completion: @escaping Handler)
}

class ProcessCommand: Process {
    func run(completion: @escaping Handler) {
        completion()
    }
}

 Sau đấy bạn bắt đầu định nghĩa 1 lớp abstract class, có tên là ProcessDecorator. Nó khác ProcessCommand mình tạo ở trên ở chỗ: mình cần truyền vào 1 Process.
class ProcessDecorator: Process {
    var process: Process
    init(process: Process) {
        self.process = process
    }

    func run(completion: @escaping Handler) {
        print("begin process...")
        self.process.run(completion: completion)
    }
}
Nếu không có biến process thì bạn không thể tạo ra 1 chuỗi các sự kiện để hình thành decorator được.
Trong pattern decorator, những đối tượng giống như ProcessCommand bá đạo nhất, nó chẳng cần nương tựa ai cả và luôn luôn đứng đầu.
Hình dung một cách đơn giản, nếu bạn hay đi tập thể dục buổi sáng ở bờ Hồ, bạn sẽ thấy các cụ đứng đấm lưng cho nhau, người sau đấm cho người trước. Người đầu tiên chẳng phải làm gì cả (ProcessCommand), các người phía sau đấm lưng là decorator :v. Nói đến đây lại thèm 1 bữa đi bộ trên bờ Hồ sáng sớm.
Tiếp đến thì đơn giản thôi, tạo decorator từ lớp abstract class ProcessDecorator
class ProcessA: ProcessDecorator {
    override func run(completion: @escaping Handler) {
        super.process.run{
            print("A")
            completion()
        }
    }
}

class ProcessB: ProcessDecorator {
    var conditionDoB: (() -> Bool)?

    func isNeedToDo() -> Bool {
        return self.conditionDoB?() == true
    }

    override func run(completion: @escaping Handler) {
        super.process.run{
            if self.isNeedToDo() {
                 print("B")
            } else {
                print("ko lam nua")
            }

            completion()
        }
    }
}

class ProcessC: ProcessDecorator {
    override func run(completion: @escaping Handler) {
        super.process.run{
            print("C")
            completion()
        }
    }
}
 
Thằng ProcessB mình đặt thêm điều kiện để tăng độ khó cho game :D. Tức là nó có thể thực thi hoặc không, tùy điều kiện truyền vào.
Bây giờ ta cần nối các task con A, B, C thành một task to.
let decorator = ProcessDecorator(process: ProcessCommand()) let doA = ProcessA(process: decorator) let doAB = ProcessB(process: doA) doAB.conditionDoB = { false } let doABC = ProcessC(process: doAB) Khi sử dụng hàm run thì kết quả như sau
doABC.run { print("end process...") } // begin process... // A // ko lam nua // C // end process...
 
 Như vậy là mọi thứ đã xong. Tuy nhiên có một thứ chưa được ổn, khi áp dụng cách này, ta cần biết detail của A, B, C mà sếp thì có bao giờ để ý chi tiết từng bước đâu mà chỉ quan tâm đến kết quả cuối cùng. Để làm mịn công việc được giao, chúng ta có thể áp dụng thêm chút Builder

Kết hợp với Builder:

class ProcessBuilder {
    private(set) var process: Process

    init() {
        self.process = ProcessDecorator(process: ProcessCommand())
    }

    func createProcessA() -> ProcessBuilder {
        let doA = ProcessA(process: self.process)
        self.process = doA
        return self
    }

    func createProcessB() -> ProcessBuilder {
        let doB = ProcessB(process: self.process)
        doB.conditionDoB = { false }
        self.process = doB
        return self
    }

    func createProcessC() -> ProcessBuilder {
        let doC = ProcessC(process: self.process)
        self.process = doC
        return self
    }

    func build() -> Process {
        self.createProcessA().createProcessB().createProcessC().process
    }
}
Như vậy khi đó ta không cần phải quan tâm từng task con A, B, C. Ta chỉ chú ý đến tổng thế công việc
ProcessBuilder().build().run {
    print("end process using builder")
} 

Tổng kết:

Decorator có nhiều điểm lợi nhưng cũng có mặt hạn chế:
  • Vì đây là một chuỗi các sự kiên liên tiếp nhau nên một mắt xích cần thay đổi ta cần tạo mới một chuỗi khác.
  • Để hiểu hết một chuỗi các sự kiện hoạt động ra sao cũng mất thời gian đi qua từng mắt xích.
Để hạn chế điểm yếu, tốt nhất là mọi người tạo ra 1 chuỗi sự kiện gồm 4, 5 sự kiện con đi kèm thôi :).
Như vậy việc áp dụng decorator sẽ rất tuyệt vời.
Trong dự án thực tế, có thể áp dụng decorator cho network layer, chia nhỏ thành các đối tượng xử lý error, show/hide indicator,...
 

Comments

Popular posts from this blog

Alamofire vs URLSession

Alamofire vs URLSession: a comparison for networking in Swift Alamofire and URLSession both help you to make network requests in Swift. The URLSession API is part of the foundation framework, whereas Alamofire needs to be added as an external dependency. Many  developers  doubt  whether it’s needed to include an extra dependency on something basic like networking in Swift. In the end, it’s perfectly doable to implement a networking layer with the great URLSession API’s which are available nowadays. This blog post is here to compare both frameworks and to find out when to add Alamofire as an external dependency. Build better iOS apps faster Looking for a great mobile CI/CD solution that has tons of iOS-specific tools, smooth code signing, and even real device testing? Learn more about Bitrise’s iOS specific solutions! This shows the real power of Alamofire as the framework makes a lot of things easier. What is Alamofire? Where URLSession...

Swift Tool Belt, Part 1: Adding a Border, Corner Radius, and Shadow to a UIView with Interface Builder

During my iOS work, I’ve assembled a set of code that I bring with me on every iOS project. I’m not talking about large frameworks or CocoaPods here. These are smaller Swift extensions or control overrides that are applicable to many projects. I think of them as my tool belt. In this post, I’ll show you an extension that will add a border, a corner radius, and a shadow to any UIView, UIButton, or UILabel and allow you to preview what it will look like in Interface Builder. Back in 2014, I wrote a blog post on Expanding User-Defined Runtime Attributes in Xcode where I added a border, corner radius, and shadow to a UIView using Interface Builder’s user-defined runtime attributes. This solution had no type checking—you had to type the property you wanted to modify by hand and often had to look up what it was called. You also had to run your project in order to see the effect of the runtime attribute. Starting with Xcode 6 , there is a new mech...

Frame vs Bounds in iOS

This article is a repost of an answer I wrote on Stack Overflow . Short description frame = a view’s location and size using the parent view’s coordinate system ( important for placing the view in the parent) bounds = a view’s location and size using its own coordinate system (important for placing the view’s content or subviews within itself) Details To help me remember frame , I think of a picture frame on a wall . The picture frame is like the border of a view. I can hang the picture anywhere I want on the wall. In the same way, I can put a view anywhere I want inside a parent view (also called a superview). The parent view is like the wall. The origin of the coordinate system in iOS is the top left. We can put our view at the origin of the superview by setting the view frame’s x-y coordinates to (0, 0), which is like hanging our picture in the very top left corner of the wall. To move it right, increase x, to move it down increase y. To help me remember bound...