Android中的构建器设计模式

Nowadays, Design Patterns play essential roles in designing and developing Android apps because they can provide some best practices for having high quality apps and following clean code principles. One of the main design patterns that are used in android development is Builder Design Pattern. In fact, this design pattern could be effective in improving flexibility and readability of codes. This article aims to discuss Builder Design Pattern in Android applications with using Kotlin.

如今,“设计模式”在设计和开发Android应用程序中起着至关重要的作用,因为它们可以为拥有高质量应用程序和遵循简洁代码原则提供一些最佳实践。 android开发中使用的主要设计模式之一是Builder Design Pattern。 实际上,这种设计模式可以有效地提高代码的灵活性和可读性。 本文旨在讨论使用Kotlin在Android应用程序中构建器设计模式。

Design Pattern的解释是什么? (What is the definition of Design Pattern?)

Design patterns are used to represent some of the best practices adapted by experienced object-oriented software developers. In software development, a design pattern is a general reusable solution to a commonly occurring problem or task in software design. In other words, it describes the problem, the solution, when to apply the solution, and its consequences. Besides, It gives implementation hints and examples.

设计模式用于表示由经验丰富的面向对象软件开发人员改编的一些最佳实践。 在软件开发中,设计模式是解决软件设计中常见问题或任务的通用可重用解决方案。 换句话说,它描述了问题,解决方案,何时应用解决方案及其后果。 此外,它提供了实现提示和示例。

在Android中使用设计模式的好处 (Benefits of using Design Patterns in Android)

  1. Design Patterns use object-oriented concepts such as decomposition, inheritance, and polymorphism.

    设计模式使用面向对象的概念,例如分解,继承和多态性。
  2. They can improve the re-usability and extensibility of Android apps. It means you do not need to write the same code again at various places, and also you can be able to add other features to your app much more easier than general coding. Therefore, Design Patterns can increase speed in application development in Android.

    它们可以提高Android应用程序的可重用性和可扩展性。 这意味着您无需在各个地方再次编写相同的代码,而且与常规编码相比,您可以更轻松地向应用程序添加其他功能。 因此,设计模式可以提高Android应用程序开发的速度。
  3. They can make your codes cleaner by decoupling the code. In addition, Design Patterns can enhance the understanding of codes in Android apps.

    他们可以通过解耦代码来使您的代码更整洁。 此外,设计模式还可以增强对Android应用程序中代码的理解。
  4. Design Patterns are proved and testified solutions because they have been built upon the knowledge and experience of expert application developers.

    设计模式是经过验证的解决方案,因为它们是建立在专家应用程序开发人员的知识和经验之上的。
  5. They make communication between developers more efficiently.

    它们使开发人员之间的通信更加有效。

设计模式的类型 (Types of Design Patterns)

Fundamentally, there are a number of Design Patterns, which can be used in software development, and all these could be categorized into the following three categories:

从根本上讲,有许多设计模式可用于软件开发,所有这些都可以分为以下三类:

  1. Creational Design Pattern

    创新设计模式

They are concerned with the way of creating objects such as Builder, Singleton, and Dependency Injection.

他们关心创建对象的方式,例如Builder,Singleton和Dependency Injection。

2. Structural Design Pattern

2. 结构设计模式

They are discussed how classes and objects can be composed, to form larger structures such as Adapter, Facade, and Proxy.

他们讨论了如何构成类和对象,以形成更大的结构,例如Adapter,Facade和Proxy。

3. Behavioral Design Pattern

3. 行为设计模式

They are considered the interaction and responsibility of objects such as Observer, Mediator, and Interpreter.

它们被视为对象(例如观察者,中介者和解释器)的交互作用和责任。

生成器设计模式 (Builder Design Pattern)

Basically, The Creational Design Pattern is used to create some objects without representing the logic or the steps, which are involved in creating the objects. In fact, Hard-Coded code is not an appropriate programming approach. In this category, we are creating the instance by using the new keyword. Occasionally, the nature of the object must be changed according to the nature of the program. In these situations, we must use the Creational Design Patterns to provide more flexibility. Thus, this makes the creation of objects easier.

基本上,创建设计模式用于创建某些对象,但不表示创建对象所涉及的逻辑或步骤。 实际上,硬编码代码不是适当的编程方法。 在此类别中,我们正在使用new关键字创建实例。 有时,必须根据程序的性质更改对象的性质。 在这种情况下,我们必须使用创新设计模式来提供更大的灵活性。 因此,这使对象的创建更加容易。

As a matter of fact, Builder Design Pattern indicates that you should construct a complex object from simple objects by using step-by-step approach. It is mainly used when object cannot be implemented in single step like in the deserialization of a complicated object.

实际上,“构建器设计模式”指示您应使用逐步方法从简单对象构造一个复杂对象。 它主要用于无法像复杂对象的反序列化那样仅一步实现对象的情况。

使用构建器设计模式的优点 (Advantages of using Builder Design Pattern)

  1. when you initialize your object, you do not need to have all the data to pass it to your object. As s result, it could be helpful to control over construction process.

    初始化对象时,不需要所有数据即可将其传递给对象。 结果,可能有助于控制施工过程。
  2. It implements a clear separation between the construction and representation of an object. Furthermore, it hides internal representation of the objects from the client.

    它在对象的构造和表示之间实现了清晰的分离。 此外,它向客户端隐藏对象的内部表示。
  3. It handles the changes in the internal representation of objects.

    它处理对象内部表示形式中的更改。
  4. It can improve the flexibility and readability of source codes.

    它可以提高源代码的灵活性和可读性。
  5. It avoids the Telescoping Constructor Pattern. For instance:

    它避免了伸缩构造函数模式。 例如:

Pizza(int size){
}
Pizza(int size, boolean cheese){
}
Pizza(int size, boolean cheese, boolean meat){
}
Pizza(int size, boolean cheese, boolean meat, boolean bacon){
}

The problem with this pattern is that when a constructor have 4 or 5 parameters long, it becomes difficult to remember those parameters.

这种模式的问题在于,当构造函数的长度为4或5个参数时,很难记住这些参数。

Kotlin中的一个简单例子 (A simple example in Kotlin)

In Android, the Builder pattern appears when using objects like AlertDialog.Builder. For example:

在Android中,使用AlertDialog.Builder之类的对象时会出现Builder模式。 例如:

val builder = AlertDialog.Builder(this)
builder.setTitle("Sample Alert")
builder.setMessage("Sample Message!")
builder.setPositiveButton(android.R.string.yes){ dialog, which ->
}
builder.setNegativeButton(android.R.string.no){ dialog, which ->
}
builder.setNeutralButton("Maybe"){ dialog, which ->
} builder.show()

This builder proceeds step-by-step, and helps you specify just only the parts of your AlertDialog that you want.

此构建器将逐步进行,并帮助您仅指定所需的AlertDialog部分。

Kotlin的另一个例子 (Another Example in Kotlin)

In below example, we have PersonalComputer class with some properties such as cpu, ram, batteryCapacity, and screenSize. In this class, the cpu is significant and compulsory. Therefore, you have to pass it every time, but the rest of the properties are optional. Additionally, if you do not set any value, the default value will apply.

在下面的示例中,我们具有一些属性的PersonalComputer类,例如cpu,ram,batteryCapacity和screenSize。 在这一堂课中,cpu是重要且必修的。 因此,您必须每次都传递它,但是其余属性是可选的。 此外,如果您未设置任何值,则将应用默认值。

class PersonalComputer(builder: Builder){    private val cpu: String = builder.cpu
private val ram: String = builder.ram
private val batteryCapacity: String = builder.batteryCapacity
private val screenSize: String = builder.screenSize
class Builder(cpu: String){ var cpu: String = cpu
var ram: String = "8G"
var batteryCapacity: String = "10000mAH"
var screenSize: String = "17inch"
fun setRam(ram: String): Builder{ this.ram = ram
return this }fun setBattery(batteryCapacity: String): Builder{ this.batteryCapacity = batteryCapacity
return this }fun setScreenSize(screenSize: String): Builder{ this.screenSize = screenSize
return this }fun build(): PersonalComputer{ return PersonalComputer(this) }
}
}

Now, we can use the Builder class as follows:

现在,我们可以如下使用Builder类:

PersonalComputer.Builder("i5") 
.setRam("16G")
.setScreenSize("14inch")
.build()

In conclusion, Design patterns play key roles in having a high quality Android apps and following clean code principles. In this essay, using Builder Design Pattern was considered in Android Development, and some advantages are explained such as providing flexibility and readability of codes. However, the number of lines of code increase at least to double in builder pattern.

总而言之,设计模式在拥有高质量的Android应用并遵循简洁的代码原则中扮演着关键角色。 本文在Android开发中考虑了使用Builder设计模式,并解释了一些优势,例如提供了代码的灵活性和可读性。 但是,在构建器模式中,代码行数至少增加了一倍。

翻译自: https://medium.com/kayvan-kaseb/builder-design-pattern-in-android-a38dccb75485

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值