您的位置:首页 > 运维架构

oop概念_彻底摆脱OOP(第1部分)

2020-08-25 14:58 1341 查看

oop概念

In this article, we will discuss one of the main principles of OOP (Object Oriented Programming), versus one of the main principles of POP (Protocol-Oriented Programming), and why you should consider adopting POP over OOP.

在本文中,我们将讨论OOP(面向对象编程)的主要原理之一,以及POP(面向协议编程)的主要原理之一,以及为什么要考虑在OOP上采用POP。

If you are familiar with programming, you are probably familiar also with the OOP concept, and if not, here is a small brief to catch up.

如果您熟悉编程,那么您可能也熟悉OOP概念,如果不熟悉,那么这里有个简短的摘要供您参考。

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which can contain data (known as properties) and procedures (known as functions or methods).

面向对象编程(OOP) 是基于以下概念的编程范例 对象 ,其中可以包含数据(称为属性)和过程(称为函数或方法)。

OOP is a way of writing computer programs that are using the idea of objects to represent data (variables) and functions (also called methods), instead of just a list of commands. The way objects are separated depends on one subject or another, thus the functions and data it contains must be related to the same subject or flow.

面向对象 是一种编写计算机程序的方法,该程序使用对象的概念来表示数据(变量)和函数(也称为方法),而不仅仅是命令列表。 对象的分离方式取决于一个或多个主题,因此其中包含的功能和数据必须与同一主题或流程相关。

Enough theory. Ready to begin? Let’s break it down and see how it works.

足够的理论。 准备开始了吗? 让我们分解一下,看看它是如何工作的。

Think of a Volkswagen Golf car, how would you describe it?

想想一辆大众高尔夫车,您如何形容?

The term can be divided into 3 three parts: Car, Volkswagen, and Golf.

该术语可分为3个三个部分: 汽车大众汽车高尔夫。

The following diagram will simplify things:

下图将简化操作:

As you can see, Golf is a kind of Volkswagen, which is a kind of a car, which is a kind of a vehicle. Each one is of the objects related to the object it’s made of.

如您所见, 高尔夫大众汽车的一种, 汽车汽车的一种 每个对象都是与其构成的对象相关的对象。

You can think of it as a derivation, or how we call it in programming languages — inheritance.

您可以将其视为派生,或我们在编程语言中如何称其为继承。

In simpler terms, it means that car contains all vehicle’s properties and functionality, Volkswagen contains all car’s and vehicle’s properties and functionality, and Golf contains all Volkswagen’s, car’s and vehicle’s properties and functionality.

简而言之,它意味着汽车包含所有车辆的属性和功能, 大众汽车包含所有汽车和车辆的属性和功能,而高尔夫则包含所有大众汽车,汽车和车辆的属性和功能。

Let’s complicate it a bit. What will happen if we have more than one car manufacturer?

让我们复杂一点。 如果我们拥有多家汽车制造商,将会发生什么?

Looks simple right? Now instead of another car manufacturer, we will add another vehicle type. How would it look like?

看起来很简单吧? 现在,我们将代替其他汽车制造商来添加其他车辆类型。 看起来如何?

The diagram can keep dividing until it gets to the pyramid’s top. And as you can see all these diagrams represent the same idea — an object that is a part of a group, which is also a part of a group, etc.

该图可以一直分割,直到到达金字塔的顶部。 正如您所看到的,所有这些图都代表着相同的想法-一个对象是一个组的一部分,这也是一个组的一部分,等等。

Here is a code example of OOP implementation:

这是OOP实现的代码示例:

As shown, the Vehicle class contains two methods: ignite() and accelerate(), and the Car class inherits the Vehicle class and contains only one method — startDriving().

如图所示, Vehicle类包含两种方法: ignite()accelerator() ,而Car类继承了Vehicle类,并且仅包含一个方法— startDriving()

Although the car class does not declare the ignite() nor the accelerate() method, according to its inheritance to the Vehicle class — it can use its “father” methods.

尽管car类未声明ignite()Accelerator()方法,但根据其对Vehicle类的继承,它可以使用其“父亲”方法。

Now that we know OOP, we can move forward to POP. But before that, we will discuss the basics of it: the protocol itself. What is a protocol?

既然我们了解OOP,我们就可以迈向POP。 但是在此之前,我们将讨论它的基础知识: 协议本身。 什么是协议

A protocol is also called an interface, and the most simple way to describe it is: A protocol is a set of rules to follow, known as abstraction as well.

协议也被称为界面,和最简单的方式来描述,它是: 协议 是一组规则可循,称为抽象为好 。

So what does it mean in programming?

那么在编程中意味着什么呢?

Given the previous example, let’s say we have a car. What is the main purpose of a car? To drive. In that case, the car is driveable, therefore our protocol will be named driveable, and will contain at least one primary function called drive. The protocol can contain as many variables and functions as we need when the only rule is — it has to be related to the main subject of the protocol.

给定前面的示例,假设我们有一辆汽车。 汽车的主要目的是什么? 驾车。 在这种情况下,汽车是可驾驶的 ,因此我们的协议将被命名为可驾驶 ,并且将至少包含一个称为drive的主要功能。 当唯一的规则是时,该协议可以包含我们需要的尽可能多的变量和功能- 它必须与该协议的主要主题相关

An object can conform to more than one protocol. The reason for that is to give the ability to a certain object to do as many actions as needed.

一个对象可以符合多个协议。 这样做的原因是赋予某个对象执行所需数量的动作的能力。

Let’s take our car as an example:

让我们以汽车为例:

How does that come to action in real-life coding?

在现实生活中编码如何起作用?

Assuming we have popups in our app and we know that all of our popups need to have a close button, we will create a dedicated protocol and call it Closeable. The purpose of this protocol will be to grant a closing action(functionality) to every object who conforms to it. To be more precise, at this point the protocol enforces this ability, which means that every popup that will conform to it will have to implement the close action we previously declared.

假设我们的应用程序中有弹出窗口,并且我们知道所有弹出窗口都需要有一个关闭按钮,我们将创建一个专用协议并将其称为Closeable。协议的目的是向符合该协议的每个对象授予关闭动作(功能)。 更准确地说,在这一点上,协议强制执行此功能,这意味着符合该协议的每个弹出窗口都必须执行我们先前声明的关闭动作。

** Keep in mind that protocols can only declare variables and functions but not implement them. **

**请记住,协议只能声明变量和函数,而不能实现它们。 **

Here is a code example of POP implementation:

这是POP实现的代码示例:

Conclusions:

结论:

In OOP we concentrate on creating objects, our program will be based on objects and what needs to be inherited. All of our planning will be done according to that. When we create an object, we need to consider what will be the parent / child objects we will inherit from / to.

在OOP中,我们专注于创建对象,我们的程序将基于对象以及需要继承的内容。 我们所有的计划都将据此完成。 创建对象时,我们需要考虑将继承自/的父/子对象。

POP is a bit different. The main consideration is the sets of rules and using them to enrich or limit the object’s functionality. In POP we also create objects but the objects conform to one (or more) protocol instead of one to another.

POP有点不同。 主要考虑因素是规则集,并使用它们来丰富或限制对象的功能。 在POP中,我们还创建对象,但是对象符合一个(或多个)协议,而不是一个到另一个。

It is essential to remember that the main difference between those two approaches is that when using OOP we are limited to inherit only from one object, while in POP we can conform to as many protocols as we need. Therefore, in my opinion, POP is much more preferable.

重要的是要记住,这两种方法之间的主要区别在于,使用OOP时,我们只能从一个对象继承 ,而在POP中,我们可以根据需要遵循尽可能多的协议 。 因此,我认为POP更可取。

翻译自: https://medium.com/takeaway-tech/get-rid-of-object-oriented-programming-for-good-part-1-cfd0bc325513

oop概念

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: