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

Swift GCD part 1: Thread safe singletons

Preview Singletons are entities, referenced to the same instance of a class from everywhere in your code. It doesn't matter if you like them or not, you will definitely meet them, so it's better to understand how they work. Constructing and handling a set of data doesn't seem to be a big challenge at first glance. The problems appear when you try to optimise the user experience with background work and your app starts acting weird. ??‍♂️ After decades of watching your display mostly with a blank face, you finally realize that your data isn't handled consistently by the manager because you're accessing it (running tasks on it) from multiple threads at the same time. So you really do have to deal with making your singletons thread safe. This article series is dedicated to thread handling using Swift. In the first part below you will get a comprehensive insight into som...

Thread safe singleton’s in Swift

What are singletons? — Singleton is design patterns which says that there should be only one instance of the class for the lifetime of the application. One the best example of Singleton is AppDelegate . How to write a singleton class ? class DefaultDict{ private var dict:[String:Any] = [:] public static let sharedManager = DefaultDict() private init(){ } public func set(value:Any,key:String){ dict[key] = value } public func object(key:String) -> Any?{ dict[key] } public func reset(){ dict.removeAll() } }   Testing singleton class under concurrent circumstances. We are going to write an example where we will set values in dict from various threads and even try to access some with different threads. When we do this we will encounter a crash. If you look closely it will be because of race condition and the crash will be on line set(value:Any,key:String) . class ViewController: UIViewController { ...

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...