面向数据编程 Data-Oriented Programming [18]

具有不可变数据的状态管理

4.1 介绍

  到目前为止,我们已经看到DO是如何处理查询系统信息的请求的,通过通用函数访问系统数据,以哈希图的形式表示。
  在本章和下一章中,我们将说明如何处理突变,即更改系统状态的请求。我们不是就地更新状态,而是维护系统数据的多个版本。在特定时间点,系统状态指的是系统数据的特定版本。
  维护多个版本的系统数据要求数据是不可变的。这在计算和内存方面都是有效的,这是通过一种称为结构共享的技术来实现的,在这种技术中,两个版本之间共享的部分数据是共享的,而不是被复制的。

  在DO中,突变被分成两个不同的阶段:

  1. 在计算阶段,我们计算下一个版本的系统数据。
  2. 在提交阶段,我们向前移动系统状态,以便它引用由计算阶段计算的系统数据的版本。

  计算和提交阶段之间的这种区别允许我们将系统中有状态的部分减少到最低限度。只有提交阶段的代码是有状态的,而由泛型函数组成的突变无状态计算阶段的代码类似于查询的代码。
  提交阶段的实现对所有突变都是通用的。因此,在提交阶段中,我们能够确保状态始终引用系统数据的有效版本。
  这种状态管理方法的另一个好处是,我们可以跟踪系统数据以前版本的历史记录。如果需要,可以直接将系统恢复到以前的状态。

表4.1突变的两个阶段

阶段责任状态实现
计算计算系统数据的下一个版本无状态具体的
提交向前移动系统状态有状态普通的

  在本章中,我们假设在我们的系统中没有并发突变。在下一章中,我们将处理并发控制。

4.2 系统数据的多个版本

  在喝咖啡的时候,你和乔在街区里散步,这次讨论的话题是版本控制系统。您将讨论git如何跟踪整个提交历史,以及将代码恢复到上一次提交有多容易、多快。您还将讨论允许在提交代码之前验证代码的提交挂钩。

乔:到目前为止,我们已经了解了如何在DO中管理从系统检索信息的查询。现在我将向你们展示我们是如何处理突变的。所谓突变,我指的是改变系统状态的操作。

NOTE 突变是一种改变系统状态的操作。

你:DO中的查询和突变有根本区别吗?毕竟,系统的整个状态被表示为散列映射。我可以很容易地编写代码来修改部分hash map。它将类似于从hash map中检索信息的代码。

乔:你可以就地对数据进行变异,但要确保变异的代码不会使系统进入无效的日期,这将是一件具有挑战性的事情。此外,您还会丢失系统状态的以前版本的跟踪信息。

你:我明白了。那么,你是如何处理DO中的突变的呢?

乔:我们采用多版本状态方法,类似于Git这样的版本控制。我们管理不同版本的系统数据。在特定时间点,系统状态指的是系统数据的版本。在执行突变之后,我们向前移动引用。

你:我很困惑:系统状态是可变的还是不可变的?

乔:数据是不可变的,但是状态引用是可变的。

TIP 数据是不可变的,但状态引用是可变的。

图4.1 执行突变B后,系统状态为数据V12。在执行突变C之后,系统状态参照数据V13。

 

你:这是不是意味着在突变的代码运行之前,我们会复制一份系统数据?

乔:没有。这将是非常低效的,因为我们将不得不对数据进行深度复制。

你:那它是怎么工作的呢?

乔:它的工作原理是使用一种称为结构共享的技术,在这种技术中,状态的后续版本之间的大部分数据都是共享的,而不是被复制。该技术允许在内存和计算方面高效地创建系统数据的新版本。

你:我很感兴趣。

TIP 通过结构共享,它可以高效的(在内存和计算方面)创建新版本的数据。

乔:稍后我会详细地向你解释结构共享是如何工作的。

  再看一下图4.1中的图表,它说明了系统状态如何引用系统数据的一个版本,然后一个问题突然出现在您的脑海中。

您:是否保留了以前版本的系统数据?

乔:在一个简单的应用程序中,垃圾收集器会自动删除以前的版本。但在某些情况下,我们会保留对以前版本数据的历史引用。

你:什么样的情况?

乔:例如,我们可以在我们的系统中允许时间旅行。就像在git中一样,我们可以非常容易地将系统移回以前的状态版本。

你:现在,我明白你的意思了:数据是不变的,但是状态引用是可变的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swift 3 Object Oriented Programming - Second Edition by Gastón C. Hillar English | 6 Mar. 2017 | ISBN: 1787120392 | 254 Pages | EPUB/PDF (conv) | 32.1 MB Key Features Leverage the most efficient object-oriented design patterns in your Swift applications Write robust, safer, and better code using the blueprints that generate objects Build a platform with object-oriented code using real-world elements and represent them in your apps Book Description Swift has quickly become one of the most-liked languages and developers' de-facto choice when building applications that target iOS and macOS. In the new version, the Swift team want to take its adoption to the next level by making it available for new platforms and audiences. This book introduces the object-oriented paradigm and its implementation in the Swift 3 programming language to help you understand how real-world objects can become part of fundamental elements in the code. This book is developed with XCode 8.x and covers all the enhancements included in Swift 3.0. In addition, we teach you to run most of the examples with the Swift REPL available on macOS and Linux, and with a Web-based Swift sandbox developed by IBM capable of running on any web browser, including Windows and mobile devices. You will organise data in blueprints that generate instances. You'll work with examples so you understand how to encapsulate and hide data by working with properties and access control. Then, you'll get to grips with complex scenarios where you use instances that belong to more than one blueprint. You'll discover the power of contract programming and parametric polymorphism. You'll combine generic code with inheritance and multiple inheritance. Later, you'll see how to combine functional programming with object-oriented programming and find out how to refactor your existing code for easy maintenance. What you will learn Write high-quality and easy-to-maintain reusable object-oriented code to build applications for iOS, macOS, and Linux Work with encapsulation, abstraction, and polymorphism using Swift 3.0 Work with classes, instances, properties, and methods in Swift 3.0 Take advantage of inheritance, specialization, and the possibility to overload or override members Implement encapsulation, abstraction, and polymorphism Explore functional programming techniques mixed with object-oriented code in Swift 3.0 Understand the differences between Swift 3.0, previous Swift versions, and Objective-C code
面向对象编程(Object-oriented programming)是一种编程范式,其中的程序结构被组织为对象的集合,这些对象可以通过相互传递消息来进行数据传递和操作。在Python中,我们可以使用class关键字来定义一个类,类是一种自定义数据类型,用于创建对象。 在Python中,一切皆为对象,包括数字、字符串、函数等。通过使用内置函数`dir`,我们可以查看对象所拥有的方法和属性。例如,对于一个列表对象a,我们可以使用`dir(a)`来查看其方法和属性的列表。 在面向对象编程的Python中,我们可以通过定义类来创建自定义对象。一个类通常包含属性和方法。例如,我们可以定义一个名为Animal的类,该类具有属性name、age和sex,以及一个描述对象的方法__str__。通过调用该方法,我们可以使用print函数输出对象的描述信息。 总结来说,面向对象编程是一种编程范式,通过定义类和创建对象来组织和处理数据。在Python中,我们可以使用class关键字定义类,一切皆为对象,可以通过调用对象的方法和访问对象的属性来操作数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python 中的面向对象](https://blog.csdn.net/liuxin_hello/article/details/127560700)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python面向对象(全套)](https://blog.csdn.net/Thewei666/article/details/126652501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值