Design Pattern in iOS

Tram Ho

)

self.request.request(method: method, data: data, header: mutableHeader, completion: completion)
}
}

let v1Request: Request = MyHeader(request: MyRequest(), apiVersion: .v1)
let standardRequest: Request = MyHeader(request: MyRequest())

Chúng ta cũng cần để filter kết quả của request. Cái mà có thể được thực hiện khi tạo một method mới, hoặc thay đổi request của bạn. Chúng tôi quyết định sử dụng Decorator để thêm vào hành vi này bởi vì service của chúng ta đã được sử udngj trong một class khác và chúng ta muốn thay đối ít nhất một số dòng ít nhất có thể.

Adapter

Adapter pattern là một software design pattern cho phép giao diện của class đã có được sử dụng như giao diện khác. Nó thường được sử dụng để tạo class có sẵn làm việc với class khác mà không phải sửa source code

Chúng ta hãy tưởng tượn adapter giống như một bộ chuyển đổi trên thực tế. Giả dụ bạ cần Nintendo 64 có video tổng hợp để làm đầu ra cho TV 4k của bạn. Do đó bạn cần một bộ chuyển đổi Composite-HDMI.

Vấn đề được giải quyết khi sử dụng adapter

Bạn cần 4 số cuối của một Card và cũng tương thích với PKPaymentPass. Nói cách khác, tạo một adapter để trả về một PKPaymentPass làm một thể hiện của Card.

Nhưng chúng ta cũng cần mix các pattern để tạo ra code có tính dùng lại và tính duy trì. Ví dụ, mix adapter với strategy pattern. Chúng ta không thực sự cần Card object mà chúng ta chỉ cần lastNumbers vậy tại sao chúng ta không tạo protocol để thực hiện chngs và tích hợp PKPaymentPass, Card và bất kì Object khác mà bạn có thể cần.

Tổng kết

Design pattern giúp chúng ta rất nhiều trong việc sử dụng lại code, tiết kiệm thời gian khi cần thay đổi một số tính năng trong code và một số tác vụ cũng dễ dàng sử dụng.

Link tham khảo: https://medium.com/cocoaacademymag/real-world-ios-design-patterns-3e5aad172094

This article we will learn about a number of design patterns useful in iOS.

Strategy

Strategy pattern allows to select an algorithm at runtime. Instead of executing single algorithms directly, the code will identify each type via the initialization function and then perform the corresponding action.

We often use different tonas that only care about runtime but don't care about how to implement them.

Simple example, the algorithm needs to know how to fly . Class Duck knows how to "fly" and so does Rocket class. Our algorithm does not care if done, it will need a pair of wings or gas, just need to know if they can fly or not.

The problem is solved when using the strategy

In this project we need to get the viewController but we also need them to have some predefined behavior. This, Strategy pattern can do very well. A generic ViewController in mobile app is for Login. So just create a protocol to determine what actions LoginViewController has. This means that the LoginViewController is in the project but we don't need to be mindful about the UI if it has a tableView or animation or some validation method. But we need it to know how to do login.

Factory

Factory Method Pattern solves the problem of creating objects without knowing how the object will be created.

Factory Pattern is useful when you create objects at runtime. So if the user wants a cheese pizza you will create CheesePizza (), if you want peperoni, it will create PeperoniPizza ().

The problem is solved when using the factory

In a similar project, you use a factory pattern to create objects at runtime by passing them what they need.

We need to pass a LoginViewController which can be created using different approaches like Storyboards, xib or coded. We have a factory instance, with an input parameter that can change the way we create that object.

With Factory instance, we only need to call the build method. This factory can be instance of ViewCodedLoginViewControllerFactory or StoryboardLoginViewControllerFactory we don't really care about, just need to implement the build method and return a LoginViewController object.

Decorator

The decorator pattern allows behavior to be added to a single, static, or dynamic object, without affecting the behavior of other objects in the same class.

A simple example, Coffee shop wants to add whip cream to coffee and calculate new prices and describe based on drinks.

The problem is solved when using decorator

We need different API version for each service call, this may have different ways to handle but we use this pattern to hteme a custom for Request Header. With this approach, we can just finish the current job, but in the future, it is ok to add another API call to the header.

We also need to filter the result of the request. What can be done when creating a new method, or changing your request. We decided to use Decorator to add this behavior because our service has been used in another class and we want to change at least some of the least possible lines.

Adapter

Adapter pattern is a software design pattern that allows the interface of the existing class to be used as another interface. It is often used to create an existing class that works with another class without modifying the source code

Let's think of an adapter as a practical converter. Suppose you need Nintendo 64 to have a composite video to be the output for your 4k TV. Therefore you need a Composite-HDMI converter.

The problem is solved when using an adapter

You need the last 4 digits of a Card and are also compatible with PKPaymentPass. In other words, create an adapter to return a PKPaymentPass as an instance of the Card.

But we also need to mix patterns to create reusable code and maintainability. For example, mix adapter with strategy pattern. We don't really need a Card object, we just need to lastNumbers so why don't we create a protocol to implement and integrate PKPaymentPass, Card and any other Objects you may need.

summary

Design patterns help us a lot in reusing code, saving time when changing some features in code and some tasks are also easy to use.

Link to reference: https://medium.com/cocoaacademymag/real-world-ios-design-patterns-3e5aad172094

Share the news now

Source : Viblo