swift 听筒模式
If you’ve ever faced an init with too many parameters or objects whose creation is dictated by dozens of settings, it’s time for you to get to know the builder pattern.
如果您遇到了太多的参数或对象的初始化,而这些参数或对象的创建是由许多设置决定的,那么该是您了解构建器模式的时候了。
The builder pattern is a creational design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming.
构建器模式是一种创新的设计模式,旨在为面向对象的编程中的各种对象创建问题提供灵活的解决方案 。
The intent of this pattern is to separate the creation of an object from its representation.
该模式的目的是将对象的创建与其表示分开。
This pattern is very popular in Swift because it lets you construct complex objects step by step. In this way the construction process itself can create different representations of the object.
这种模式在Swift中非常流行,因为它可以让您逐步构造复杂的对象。 这样,构造过程本身可以创建对象的不同表示。
A common scenario during the development of an iOS app is to initialize a View and then go to set all the values of the newly created object. Just like this:
在开发iOS应用程序时,常见的情况是初始化View,然后设置新创建对象的所有值。 像这样:
let view = MovieView() view.titleLabel.text = movie.title view.descriptionLabel.text = movie.description view.coverImage.isHidden = false view.coverImage.image = movie.coverImage view.favouriteImage.isHidden = false if movie.isFavourite { view.favouriteImage.image = UIImage(named: "favourite_true") }else { view.favouriteImage.image = UIImage(named: "favourite_false") }In this case if I want to create the same view, but this time without a cover image I’ll have to do something like this:
在这种情况下,如果我想创建相同的视图,但是这次没有封面图像,则必须执行以下操作:
let view = MovieView() view.titleLabel.text = movie.title view.descriptionLabel.text = movie.description view.favouriteImage.isHidden = false if movie.isFavourite { view.favouriteImage.image = UIImage(named: "favourite_true") }else { view.favouriteImage.image = UIImage(named: "favourite_false") }As you can see facing this scenario is not so great, let’s see how we can improve our code.
如您所见,面对这种情况并不是很好,让我们看看如何改进代码。
The builder pattern allows us to make the code seen above way cleaner and readable. It uses a series of concatenated methods to set the object, then calls the build method to create the actual instance.
构建器模式使我们可以使上面看到的代码更清晰易读 。 它使用一系列串联的方法来设置对象,然后调用build 方法来创建实际实例。
let view = MovieViewBuilder() .withTitle(movie.title) .withDescription(movie.description) .withCoverImage(movie.coverImage) .withFavouriteIcon(movie.isFavourite) .build() let view = MovieViewBuilder() .withTitle(movie.title) .withDescription(movie.description) .withFavouriteIcon(movie.isFavourite) .build()By recreating the same views by adopting the pattern, our code changes radically.
通过采用该模式来重新创建相同的视图,我们的代码将发生根本性的变化。
A simple approach is to return the builder itself from each method call, to make it easy to concatenate the configuration methods.
一种简单的方法是从每个方法调用返回构建器本身 ,以使连接配置方法变得容易。
class MovieViewBuilder { private let view = MovieView() public func withTitle(_ title: String) -> MovieViewBuilder{ view.titleLabel.text = title return self } public func withDescription(_ description: String) -> MovieViewBuilder{ view.descriptionLabel.text = description return self } public func withCoverImage(_ image: UIImage) -> MovieViewBuilder{ //by default the coverImage is hidden view.coverImage.isHidden = false view.coverImage.image = image return self } public func withFavouriteIcon(_ isFavourite: Bool) -> MovieViewBuilder { //by default the favouriteImage is hidden view.favouriteImage.isHidden = false if isFavourite { view.favouriteImage.image = UIImage(named: "favourite_true") }else { view.favouriteImage.image = UIImage(named: "favourite_false") } return self } public func build () -> MovieView{ return self.view } }The only real disadvantage is that you have to define a builder class for each class that needs it, this involves an increase in development time.
唯一的实际缺点是,您必须为每个需要它的类定义一个构建器类,这会增加开发时间 。
In my opinion the time invested in preparing the builder is always paid back, speeding up the understanding of the code and making maintenance easier.
在我看来,花在准备构建器上的时间总是可以收回的,从而加快了对代码的理解 , 并使维护更加容易 。
翻译自: https://medium.com/@m.delgiudice/builder-pattern-in-swift-ce87b40de597
swift 听筒模式
相关资源:微信小程序源码-合集6.rar