You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trpl-zh-cn/ch17-03-oo-design-patterns....

817 lines
71 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE HTML>
<html lang="zh-CN" class="light" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>面向对象设计模式的实现 - Rust 程序设计语言 简体中文版</title>
<!-- Custom HTML head -->
<meta name="description" content="Rust 程序设计语言 简体中文版">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="favicon.svg">
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="highlight.css">
<link rel="stylesheet" href="tomorrow-night.css">
<link rel="stylesheet" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="ferris.css">
<link rel="stylesheet" href="theme/2018-edition.css">
</head>
<body class="sidebar-visible no-js">
<div id="body-container">
<!-- Provide site root to javascript -->
<script>
var path_to_root = "";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('light')
html.classList.add(theme);
var body = document.querySelector('body');
body.classList.remove('no-js')
body.classList.add('js');
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var body = document.querySelector('body');
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
body.classList.remove('sidebar-visible');
body.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item expanded affix "><a href="title-page.html">Rust 程序设计语言</a></li><li class="chapter-item expanded affix "><a href="foreword.html">前言</a></li><li class="chapter-item expanded affix "><a href="ch00-00-introduction.html">简介</a></li><li class="chapter-item expanded "><a href="ch01-00-getting-started.html"><strong aria-hidden="true">1.</strong> 入门指南</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch01-01-installation.html"><strong aria-hidden="true">1.1.</strong> 安装</a></li><li class="chapter-item expanded "><a href="ch01-02-hello-world.html"><strong aria-hidden="true">1.2.</strong> Hello, World!</a></li><li class="chapter-item expanded "><a href="ch01-03-hello-cargo.html"><strong aria-hidden="true">1.3.</strong> Hello, Cargo!</a></li></ol></li><li class="chapter-item expanded "><a href="ch02-00-guessing-game-tutorial.html"><strong aria-hidden="true">2.</strong> 写个猜数字游戏</a></li><li class="chapter-item expanded "><a href="ch03-00-common-programming-concepts.html"><strong aria-hidden="true">3.</strong> 常见编程概念</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch03-01-variables-and-mutability.html"><strong aria-hidden="true">3.1.</strong> 变量与可变性</a></li><li class="chapter-item expanded "><a href="ch03-02-data-types.html"><strong aria-hidden="true">3.2.</strong> 数据类型</a></li><li class="chapter-item expanded "><a href="ch03-03-how-functions-work.html"><strong aria-hidden="true">3.3.</strong> 函数</a></li><li class="chapter-item expanded "><a href="ch03-04-comments.html"><strong aria-hidden="true">3.4.</strong> 注释</a></li><li class="chapter-item expanded "><a href="ch03-05-control-flow.html"><strong aria-hidden="true">3.5.</strong> 控制流</a></li></ol></li><li class="chapter-item expanded "><a href="ch04-00-understanding-ownership.html"><strong aria-hidden="true">4.</strong> 认识所有权</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch04-01-what-is-ownership.html"><strong aria-hidden="true">4.1.</strong> 什么是所有权?</a></li><li class="chapter-item expanded "><a href="ch04-02-references-and-borrowing.html"><strong aria-hidden="true">4.2.</strong> 引用与借用</a></li><li class="chapter-item expanded "><a href="ch04-03-slices.html"><strong aria-hidden="true">4.3.</strong> Slice 类型</a></li></ol></li><li class="chapter-item expanded "><a href="ch05-00-structs.html"><strong aria-hidden="true">5.</strong> 使用结构体组织相关联的数据</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch05-01-defining-structs.html"><strong aria-hidden="true">5.1.</strong> 结构体的定义和实例化</a></li><li class="chapter-item expanded "><a href="ch05-02-example-structs.html"><strong aria-hidden="true">5.2.</strong> 结构体示例程序</a></li><li class="chapter-item expanded "><a href="ch05-03-method-syntax.html"><strong aria-hidden="true">5.3.</strong> 方法语法</a></li></ol></li><li class="chapter-item expanded "><a href="ch06-00-enums.html"><strong aria-hidden="true">6.</strong> 枚举和模式匹配</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch06-01-defining-an-enum.html"><strong aria-hidden="true">6.1.</strong> 枚举的定义</a></li><li class="chapter-item expanded "><a href="ch06-02-match.html"><strong aria-hidden="true">6.2.</strong> match 控制流结构</a></li><li class="chapter-item expanded "><a href="ch06-03-if-let.html"><strong aria-hidden="true">6.3.</strong> if let 简洁控制流</a></li></ol></li><li class="chapter-item expanded "><a href="ch07-00-managing-growing-projects-with-packages-crates-and-modules.html"><strong aria-hidden="true">7.</strong> 使用包、Crate 和模块管理不断增长的项目</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch07-01-packages-and-crates.html"><strong aria-hidden="true">7.1.</strong> 包和 Crate</a></li><li class="chapter-item expanded "><a href="ch07-02-defining-modules-to-control-scope-and-privacy.html"><strong aria-hidden="true">7.2.</strong> 定义模块来控制作用域与私有性</a></li><li class="chapter-item expanded "><a href="ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html"><strong aria-hidden="true">7.3.</strong> 引用模块项目的路径</a></li><li class="chapter-item expanded "><a href="ch07-04-bringing-paths-into-scope-with-the-use-keyword.html"><strong aria-hidden="true">7.4.</strong> 使用 use 关键字将路径引入作用域</a></li><li class="chapter-item expanded "><a href="ch07-05-separating-modules-into-different-files.html"><strong aria-hidden="true">7.5.</strong> 将模块拆分成多个文件</a></li></ol></li><li class="chapter-item expanded "><a href="ch08-00-common-collections.html"><strong aria-hidden="true">8.</strong> 常见集合</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch08-01-vectors.html"><strong aria-hidden="true">8.1.</strong> 使用 Vector 储存列表</a></li><li class="chapter-item expanded "><a href="ch08-02-strings.html"><strong aria-hidden="true">8.2.</strong> 使用字符串储存 UTF-8 编码的文本</a></li><li class="chapter-item expanded "><a href="ch08-03-hash-maps.html"><strong aria-hidden="true">8.3.</strong> 使用 Hash Map 储存键值对</a></li></ol></li><li class="chapter-item expanded "><a href="ch09-00-error-handling.html"><strong aria-hidden="true">9.</strong> 错误处理</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch09-01-unrecoverable-errors-with-panic.html"><strong aria-hidden="true">9.1.</strong> 用 panic! 处理不可恢复的错误</a></li><li class="chapter-item expanded "><a href="ch09-02-recoverable-errors-with-result.html"><strong aria-hidden="true">9.2.</strong> 用 Result 处理可恢复的错误</a></li><li class="chapter-item expanded "><a href="ch09-03-to-panic-or-not-to-panic.html"><strong aria-hidden="true">9.3.</strong> 要不要 panic!</a></li></ol></li><li class="chapter-item expanded "><a href="ch10-00-generics.html"><strong aria-hidden="true">10.</strong> 泛型、Trait 和生命周期</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch10-01-syntax.html"><strong aria-hidden="true">10.1.</strong> 泛型数据类型</a></li><li class="chapter-item expanded "><a href="ch10-02-traits.html"><strong aria-hidden="true">10.2.</strong> Trait定义共同行为</a></li><li class="chapter-item expanded "><a href="ch10-03-lifetime-syntax.html"><strong aria-hidden="true">10.3.</strong> 生命周期确保引用有效</a></li></ol></li><li class="chapter-item expanded "><a href="ch11-00-testing.html"><strong aria-hidden="true">11.</strong> 编写自动化测试</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch11-01-writing-tests.html"><strong aria-hidden="true">11.1.</strong> 如何编写测试</a></li><li class="chapter-item expanded "><a href="ch11-02-running-tests.html"><strong aria-hidden="true">11.2.</strong> 控制测试如何运行</a></li><li class="chapter-item expanded "><a href="ch11-03-test-organization.html"><strong aria-hidden="true">11.3.</strong> 测试的组织结构</a></li></ol></li><li class="chapter-item expanded "><a href="ch12-00-an-io-project.html"><strong aria-hidden="true">12.</strong> 一个 I/O 项目:构建命令行程序</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch12-01-accepting-command-line-arguments.html"><strong aria-hidden="true">12.1.</strong> 接受命令行参数</a></li><li class="chapter-item expanded "><a href="ch12-02-reading-a-file.html"><strong aria-hidden="true">12.2.</strong> 读取文件</a></li><li class="chapter-item expanded "><a href="ch12-03-improving-error-handling-and-modularity.html"><strong aria-hidden="true">12.3.</strong> 重构以改进模块化与错误处理</a></li><li class="chapter-item expanded "><a href="ch12-04-testing-the-librarys-functionality.html"><strong aria-hidden="true">12.4.</strong> 采用测试驱动开发完善库的功能</a></li><li class="chapter-item expanded "><a href="ch12-05-working-with-environment-variables.html"><strong aria-hidden="true">12.5.</strong> 处理环境变量</a></li><li class="chapter-item expanded "><a href="ch12-06-writing-to-stderr-instead-of-stdout.html"><strong aria-hidden="true">12.6.</strong> 将错误信息输出到标准错误而不是标准输出</a></li></ol></li><li class="chapter-item expanded "><a href="ch13-00-functional-features.html"><strong aria-hidden="true">13.</strong> Rust 中的函数式语言功能:迭代器与闭包</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch13-01-closures.html"><strong aria-hidden="true">13.1.</strong> 闭包:可以捕获其环境的匿名函数</a></li><li class="chapter-item expanded "><a href="ch13-02-iterators.html"><strong aria-hidden="true">13.2.</strong> 使用迭代器处理元素序列</a></li><li class="chapter-item expanded "><a href="ch13-03-improving-our-io-project.html"><strong aria-hidden="true">13.3.</strong> 改进之前的 I/O 项目</a></li><li class="chapter-item expanded "><a href="ch13-04-performance.html"><strong aria-hidden="true">13.4.</strong> 性能比较:循环对迭代器</a></li></ol></li><li class="chapter-item expanded "><a href="ch14-00-more-about-cargo.html"><strong aria-hidden="true">14.</strong> 更多关于 Cargo 和 Crates.io 的内容</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch14-01-release-profiles.html"><strong aria-hidden="true">14.1.</strong> 采用发布配置自定义构建</a></li><li class="chapter-item expanded "><a href="ch14-02-publishing-to-crates-io.html"><strong aria-hidden="true">14.2.</strong> 将 crate 发布到 Crates.io</a></li><li class="chapter-item expanded "><a href="ch14-03-cargo-workspaces.html"><strong aria-hidden="true">14.3.</strong> Cargo 工作空间</a></li><li class="chapter-item expanded "><a href="ch14-04-installing-binaries.html"><strong aria-hidden="true">14.4.</strong> 使用 cargo install 安装二进制文件</a></li><li class="chapter-item expanded "><a href="ch14-05-extending-cargo.html"><strong aria-hidden="true">14.5.</strong> Cargo 自定义扩展命令</a></li></ol></li><li class="chapter-item expanded "><a href="ch15-00-smart-pointers.html"><strong aria-hidden="true">15.</strong> 智能指针</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch15-01-box.html"><strong aria-hidden="true">15.1.</strong> 使用 Box&lt;T&gt; 指向堆上数据</a></li><li class="chapter-item expanded "><a href="ch15-02-deref.html"><strong aria-hidden="true">15.2.</strong> 使用 Deref Trait 将智能指针当作常规引用处理</a></li><li class="chapter-item expanded "><a href="ch15-03-drop.html"><strong aria-hidden="true">15.3.</strong> 使用 Drop Trait 运行清理代码</a></li><li class="chapter-item expanded "><a href="ch15-04-rc.html"><strong aria-hidden="true">15.4.</strong> Rc&lt;T&gt; 引用计数智能指针</a></li><li class="chapter-item expanded "><a href="ch15-05-interior-mutability.html"><strong aria-hidden="true">15.5.</strong> RefCell&lt;T&gt; 与内部可变性模式</a></li><li class="chapter-item expanded "><a href="ch15-06-reference-cycles.html"><strong aria-hidden="true">15.6.</strong> 引用循环会导致内存泄漏</a></li></ol></li><li class="chapter-item expanded "><a href="ch16-00-concurrency.html"><strong aria-hidden="true">16.</strong> 无畏并发</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch16-01-threads.html"><strong aria-hidden="true">16.1.</strong> 使用线程同时地运行代码</a></li><li class="chapter-item expanded "><a href="ch16-02-message-passing.html"><strong aria-hidden="true">16.2.</strong> 使用消息传递在线程间通信</a></li><li class="chapter-item expanded "><a href="ch16-03-shared-state.html"><strong aria-hidden="true">16.3.</strong> 共享状态并发</a></li><li class="chapter-item expanded "><a href="ch16-04-extensible-concurrency-sync-and-send.html"><strong aria-hidden="true">16.4.</strong> 使用 Sync 与 Send Traits 的可扩展并发</a></li></ol></li><li class="chapter-item expanded "><a href="ch17-00-oop.html"><strong aria-hidden="true">17.</strong> Rust 的面向对象编程特性</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch17-01-what-is-oo.html"><strong aria-hidden="true">17.1.</strong> 面向对象语言的特点</a></li><li class="chapter-item expanded "><a href="ch17-02-trait-objects.html"><strong aria-hidden="true">17.2.</strong> 顾及不同类型值的 trait 对象</a></li><li class="chapter-item expanded "><a href="ch17-03-oo-design-patterns.html" class="active"><strong aria-hidden="true">17.3.</strong> 面向对象设计模式的实现</a></li></ol></li><li class="chapter-item expanded "><a href="ch18-00-patterns.html"><strong aria-hidden="true">18.</strong> 模式与模式匹配</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch18-01-all-the-places-for-patterns.html"><strong aria-hidden="true">18.1.</strong> 所有可能会用到模式的位置</a></li><li class="chapter-item expanded "><a href="ch18-02-refutability.html"><strong aria-hidden="true">18.2.</strong> Refutability可反驳性: 模式是否会匹配失效</a></li><li class="chapter-item expanded "><a href="ch18-03-pattern-syntax.html"><strong aria-hidden="true">18.3.</strong> 模式语法</a></li></ol></li><li class="chapter-item expanded "><a href="ch19-00-advanced-features.html"><strong aria-hidden="true">19.</strong> 高级特征</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch19-01-unsafe-rust.html"><strong aria-hidden="true">19.1.</strong> 不安全的 Rust</a></li><li class="chapter-item expanded "><a href="ch19-03-advanced-traits.html"><strong aria-hidden="true">19.2.</strong> 高级 trait</a></li><li class="chapter-item expanded "><a href="ch19-04-advanced-types.html"><strong aria-hidden="true">19.3.</strong> 高级类型</a></li><li class="chapter-item expanded "><a href="ch19-05-advanced-functions-and-closures.html"><strong aria-hidden="true">19.4.</strong> 高级函数与闭包</a></li><li class="chapter-item expanded "><a href="ch19-06-macros.html"><strong aria-hidden="true">19.5.</strong></a></li></ol></li><li class="chapter-item expanded "><a href="ch20-00-final-project-a-web-server.html"><strong aria-hidden="true">20.</strong> 最后的项目:构建多线程 web server</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch20-01-single-threaded.html"><strong aria-hidden="true">20.1.</strong> 建立单线程 web server</a></li><li class="chapter-item expanded "><a href="ch20-02-multithreaded.html"><strong aria-hidden="true">20.2.</strong> 将单线程 server 变为多线程 server</a></li><li class="chapter-item expanded "><a href="ch20-03-graceful-shutdown-and-cleanup.html"><strong aria-hidden="true">20.3.</strong> 优雅停机与清理</a></li></ol></li><li class="chapter-item expanded "><a href="appendix-00.html"><strong aria-hidden="true">21.</strong> 附录</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="appendix-01-keywords.html"><strong aria-hidden="true">21.1.</strong> A - 关键字</a></li><li class="chapter-item expanded "><a href="appendix-02-operators.html"><strong aria-hidden="true">21.2.</strong> B - 运算符与符号</a></li><li class="chapter-item expanded "><a href="appendix-03-derivable-traits.html"><strong aria-hidden="true">21.3.</strong> C - 可派生的 trait</a></li><li class="chapter-item expanded "><a href="appendix-04-useful-development-tools.html"><strong aria-hidden="true">21.4.</strong> D - 实用开发工具</a></li><li class="chapter-item expanded "><a href="appendix-05-editions.html"><strong aria-hidden="true">21.5.</strong> E - 版本</a></li><li class="chapter-item expanded "><a href="appendix-06-translation.html"><strong aria-hidden="true">21.6.</strong> F - 本书译本</a></li><li class="chapter-item expanded "><a href="appendix-07-nightly-rust.html"><strong aria-hidden="true">21.7.</strong> G - Rust 是如何开发的与 “Nightly Rust”</a></li></ol></li></ol>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<!-- Track and set sidebar scroll position -->
<script>
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
sidebarScrollbox.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
}
}, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar
sidebarScrollbox.scrollTop = sidebarScrollTop;
} else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
var activeSection = document.querySelector('#sidebar .active');
if (activeSection) {
activeSection.scrollIntoView({ block: 'center' });
}
}
</script>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Rust 程序设计语言 简体中文版</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/KaiserY/trpl-zh-cn/tree/main" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/KaiserY/trpl-zh-cn/edit/main/src/ch17-03-oo-design-patterns.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h2 id="面向对象设计模式的实现"><a class="header" href="#面向对象设计模式的实现">面向对象设计模式的实现</a></h2>
<blockquote>
<p><a href="https://github.com/rust-lang/book/blob/main/src/ch17-03-oo-design-patterns.md">ch17-03-oo-design-patterns.md</a>
<br>
commit 937784b8708c24314707378ad42faeb12a334bbd</p>
</blockquote>
<p><strong>状态模式</strong><em>state pattern</em>)是一个面向对象设计模式。该模式的关键在于定义一系列值的内含状态。这些状态体现为一系列的 <strong>状态对象</strong>,同时值的行为随着其内部状态而改变。我们将编写一个博客发布结构体的例子,它拥有一个包含其状态的字段,这是一个有着 &quot;draft&quot;&quot;review&quot;&quot;published&quot; 的状态对象</p>
<p>状态对象共享功能:当然,在 Rust 中使用结构体和 trait 而不是对象和继承。每一个状态对象负责其自身的行为,以及该状态何时应当转移至另一个状态。持有一个状态对象的值对于不同状态的行为以及何时状态转移毫不知情。</p>
<p>使用状态模式的优点在于,程序的业务需求改变时,无需改变值持有状态或者使用值的代码。我们只需更新某个状态对象中的代码来改变其规则,或者是增加更多的状态对象。</p>
<p>首先我们将以一种更加传统的面向对象的方式实现状态模式,接着使用一种更 Rust 一点的方式。让我们使用状态模式增量式地实现一个发布博文的工作流以探索这个概念。</p>
<p>这个博客的最终功能看起来像这样:</p>
<ol>
<li>博文从空白的草案开始。</li>
<li>一旦草案完成,请求审核博文。</li>
<li>一旦博文过审,它将被发表。</li>
<li>只有被发表的博文的内容会被打印,这样就不会意外打印出没有被审核的博文的文本。</li>
</ol>
<p>任何其他对博文的修改尝试都是没有作用的。例如,如果尝试在请求审核之前通过一个草案博文,博文应该保持未发布的状态。</p>
<p>示例 17-11 展示这个工作流的代码形式:这是一个我们将要在一个叫做 <code>blog</code> 的库 crate 中实现的 API 的示例。这段代码还不能编译,因为还未实现 <code>blog</code></p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><code class="language-rust ignore does_not_compile">use blog::Post;
fn main() {
let mut post = Post::new();
post.add_text(&quot;I ate a salad for lunch today&quot;);
assert_eq!(&quot;&quot;, post.content());
post.request_review();
assert_eq!(&quot;&quot;, post.content());
post.approve();
assert_eq!(&quot;I ate a salad for lunch today&quot;, post.content());
}</code></pre>
<p><span class="caption">示例 17-11: 展示了 <code>blog</code> crate 期望行为的代码</span></p>
<p>我们希望允许用户使用 <code>Post::new</code> 创建一个新的博文草案。也希望能在草案阶段为博文编写一些文本。如果在审批之前尝试立刻获取博文的内容,不应该获取到任何文本因为博文仍然是草案。一个好的单元测试将是断言草案博文的 <code>content</code> 方法返回空字符串,不过我们并不准备为这个例子编写单元测试。</p>
<p>接下来,我们希望能够请求审核博文,而在等待审核的阶段 <code>content</code> 应该仍然返回空字符串。最后当博文审核通过,它应该被发表,这意味着当调用 <code>content</code> 时博文的文本将被返回。</p>
<p>注意我们与 crate 交互的唯一的类型是 <code>Post</code>。这个类型会使用状态模式并会存放处于三种博文所可能的状态之一的值 —— 草案,等待审核和发布。状态上的改变由 <code>Post</code> 类型内部进行管理。状态依库用户对 <code>Post</code> 实例调用的方法而改变,但是不能直接管理状态变化。这也意味着用户不会在状态上犯错,比如在过审前发布博文。</p>
<h3 id="定义-post-并新建一个草案状态的实例"><a class="header" href="#定义-post-并新建一个草案状态的实例">定义 <code>Post</code> 并新建一个草案状态的实例</a></h3>
<p>让我们开始实现这个库吧!我们知道需要一个公有 <code>Post</code> 结构体来存放一些文本,所以让我们从结构体的定义和一个创建 <code>Post</code> 实例的公有关联函数 <code>new</code> 开始,如示例 17-12 所示。还需定义一个私有 trait <code>State</code></p>
<p><code>Post</code> 将在私有字段 <code>state</code> 中存放一个 <code>Option&lt;T&gt;</code> 类型的 trait 对象 <code>Box&lt;dyn State&gt;</code>。稍后将会看到为何 <code>Option&lt;T&gt;</code> 是必须的。</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground">pub struct Post {
state: Option&lt;Box&lt;dyn State&gt;&gt;,
content: String,
}
impl Post {
pub fn new() -&gt; Post {
Post {
state: Some(Box::new(Draft {})),
content: String::new(),
}
}
}
trait State {}
struct Draft {}
impl State for Draft {}</code></pre>
<p><span class="caption">示例 17-12: <code>Post</code> 结构体的定义和新建 <code>Post</code> 实例的 <code>new</code> 函数,<code>State</code> trait 和结构体 <code>Draft</code></span></p>
<p><code>State</code> trait 定义了所有不同状态的博文所共享的行为,这个状态对象是 <code>Draft</code><code>PendingReview</code><code>Published</code>,它们都会实现 <code>State</code> 状态。现在这个 trait 并没有任何方法,同时开始将只定义 <code>Draft</code> 状态因为这是我们希望博文的初始状态。</p>
<p>当创建新的 <code>Post</code> 时,我们将其 <code>state</code> 字段设置为一个存放了 <code>Box</code><code>Some</code> 值。这个 <code>Box</code> 指向一个 <code>Draft</code> 结构体新实例。这确保了无论何时新建一个 <code>Post</code> 实例,它都会从草案开始。因为 <code>Post</code><code>state</code> 字段是私有的,也就无法创建任何其他状态的 <code>Post</code> 了!。<code>Post::new</code> 函数中将 <code>content</code> 设置为新建的空 <code>String</code></p>
<h3 id="存放博文内容的文本"><a class="header" href="#存放博文内容的文本">存放博文内容的文本</a></h3>
<p>在示例 17-11 中,展示了我们希望能够调用一个叫做 <code>add_text</code> 的方法并向其传递一个 <code>&amp;str</code> 来将文本增加到博文的内容中。选择实现为一个方法而不是将 <code>content</code> 字段暴露为 <code>pub</code> 。这意味着之后可以实现一个方法来控制 <code>content</code> 字段如何被读取。<code>add_text</code> 方法是非常直观的,让我们在示例 17-13 的 <code>impl Post</code> 块中增加一个实现:</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground"><span class="boring">pub struct Post {
</span><span class="boring"> state: Option&lt;Box&lt;dyn State&gt;&gt;,
</span><span class="boring"> content: String,
</span><span class="boring">}
</span><span class="boring">
</span>impl Post {
// --snip--
<span class="boring"> pub fn new() -&gt; Post {
</span><span class="boring"> Post {
</span><span class="boring"> state: Some(Box::new(Draft {})),
</span><span class="boring"> content: String::new(),
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span> pub fn add_text(&amp;mut self, text: &amp;str) {
self.content.push_str(text);
}
}
<span class="boring">
</span><span class="boring">trait State {}
</span><span class="boring">
</span><span class="boring">struct Draft {}
</span><span class="boring">
</span><span class="boring">impl State for Draft {}</span></code></pre>
<p><span class="caption">示例 17-13: 实现方法 <code>add_text</code> 来向博文的 <code>content</code> 增加文本</span></p>
<p><code>add_text</code> 获取一个 <code>self</code> 的可变引用,因为需要改变调用 <code>add_text</code><code>Post</code> 实例。接着调用 <code>content</code> 中的 <code>String</code><code>push_str</code> 并传递 <code>text</code> 参数来保存到 <code>content</code> 中。这不是状态模式的一部分,因为它的行为并不依赖博文所处的状态。<code>add_text</code> 方法完全不与 <code>state</code> 状态交互,不过这是我们希望支持的行为的一部分。</p>
<h3 id="确保博文草案的内容是空的"><a class="header" href="#确保博文草案的内容是空的">确保博文草案的内容是空的</a></h3>
<p>即使调用 <code>add_text</code> 并向博文增加一些内容之后,我们仍然希望 <code>content</code> 方法返回一个空字符串 slice因为博文仍然处于草案状态如示例 17-11 的第 8 行所示。现在让我们使用能满足要求的最简单的方式来实现 <code>content</code> 方法:总是返回一个空字符串 slice。当实现了将博文状态改为发布的能力之后将改变这一做法。但是目前博文只能是草案状态这意味着其内容应该总是空的。示例 17-14 展示了这个占位符实现:</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground"><span class="boring">pub struct Post {
</span><span class="boring"> state: Option&lt;Box&lt;dyn State&gt;&gt;,
</span><span class="boring"> content: String,
</span><span class="boring">}
</span><span class="boring">
</span>impl Post {
// --snip--
<span class="boring"> pub fn new() -&gt; Post {
</span><span class="boring"> Post {
</span><span class="boring"> state: Some(Box::new(Draft {})),
</span><span class="boring"> content: String::new(),
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn add_text(&amp;mut self, text: &amp;str) {
</span><span class="boring"> self.content.push_str(text);
</span><span class="boring"> }
</span><span class="boring">
</span> pub fn content(&amp;self) -&gt; &amp;str {
&quot;&quot;
}
}
<span class="boring">
</span><span class="boring">trait State {}
</span><span class="boring">
</span><span class="boring">struct Draft {}
</span><span class="boring">
</span><span class="boring">impl State for Draft {}</span></code></pre>
<p><span class="caption">列表 17-14: 增加一个 <code>Post</code><code>content</code> 方法的占位实现,它总是返回一个空字符串 slice</span></p>
<p>通过增加这个 <code>content</code> 方法,示例 17-11 中直到第 8 行的代码能如期运行。</p>
<h3 id="请求审核博文来改变其状态"><a class="header" href="#请求审核博文来改变其状态">请求审核博文来改变其状态</a></h3>
<p>接下来需要增加请求审核博文的功能,这应当将其状态由 <code>Draft</code> 改为 <code>PendingReview</code>。示例 17-15 展示了这个代码:</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground"><span class="boring">pub struct Post {
</span><span class="boring"> state: Option&lt;Box&lt;dyn State&gt;&gt;,
</span><span class="boring"> content: String,
</span><span class="boring">}
</span><span class="boring">
</span>impl Post {
// --snip--
<span class="boring"> pub fn new() -&gt; Post {
</span><span class="boring"> Post {
</span><span class="boring"> state: Some(Box::new(Draft {})),
</span><span class="boring"> content: String::new(),
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn add_text(&amp;mut self, text: &amp;str) {
</span><span class="boring"> self.content.push_str(text);
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn content(&amp;self) -&gt; &amp;str {
</span><span class="boring"> &quot;&quot;
</span><span class="boring"> }
</span><span class="boring">
</span> pub fn request_review(&amp;mut self) {
if let Some(s) = self.state.take() {
self.state = Some(s.request_review())
}
}
}
trait State {
fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt;;
}
struct Draft {}
impl State for Draft {
fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
Box::new(PendingReview {})
}
}
struct PendingReview {}
impl State for PendingReview {
fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
self
}
}</code></pre>
<p><span class="caption">示例 17-15: 实现 <code>Post</code><code>State</code> trait 的 <code>request_review</code> 方法</span></p>
<p>这里为 <code>Post</code> 增加一个获取 <code>self</code> 可变引用的公有方法 <code>request_review</code>。接着在 <code>Post</code> 的当前状态下调用内部的 <code>request_review</code> 方法,并且第二个 <code>request_review</code> 方法会消费当前的状态并返回一个新状态。</p>
<p>这里给 <code>State</code> trait 增加了 <code>request_review</code> 方法;所有实现了这个 trait 的类型现在都需要实现 <code>request_review</code> 方法。注意不同于使用 <code>self</code><code>&amp;self</code> 或者 <code>&amp;mut self</code> 作为方法的第一个参数,这里使用了 <code>self: Box&lt;Self&gt;</code>。这个语法意味着该方法只可在持有这个类型的 <code>Box</code> 上被调用。这个语法获取了 <code>Box&lt;Self&gt;</code> 的所有权使老状态无效化,以便 <code>Post</code> 的状态值可转换为一个新状态。</p>
<p>为了消费老状态,<code>request_review</code> 方法需要获取状态值的所有权。这就是 <code>Post</code><code>state</code> 字段中 <code>Option</code> 的来历:调用 <code>take</code> 方法将 <code>state</code> 字段中的 <code>Some</code> 值取出并留下一个 <code>None</code>,因为 Rust 不允许结构体实例中存在值为空的字段。这使得我们将 <code>state</code> 的值移出 <code>Post</code> 而不是借用它。接着我们将博文的 <code>state</code> 值设置为这个操作的结果。</p>
<p>我们需要将 <code>state</code> 临时设置为 <code>None</code> 来获取 <code>state</code> 值,即老状态的所有权,而不是使用 <code>self.state = self.state.request_review();</code> 这样的代码直接更新状态值。这确保了当 <code>Post</code> 被转换为新状态后不能再使用老 <code>state</code> 值。</p>
<p><code>Draft</code><code>request_review</code> 方法需要返回一个新的,装箱的 <code>PendingReview</code> 结构体的实例,其用来代表博文处于等待审核状态。结构体 <code>PendingReview</code> 同样也实现了 <code>request_review</code> 方法,不过它不进行任何状态转换。相反它返回自身,因为当我们请求审核一个已经处于 <code>PendingReview</code> 状态的博文,它应该继续保持 <code>PendingReview</code> 状态。</p>
<p>现在我们能看出状态模式的优势了:无论 <code>state</code> 是何值,<code>Post</code><code>request_review</code> 方法都是一样的。每个状态只负责它自己的规则。</p>
<p>我们将继续保持 <code>Post</code><code>content</code> 方法实现不变,返回一个空字符串 slice。现在我们可以拥有 <code>PendingReview</code> 状态和 <code>Draft</code> 状态的 <code>Post</code> 了,不过我们希望在 <code>PendingReview</code> 状态下 <code>Post</code> 也有相同的行为。现在示例 17-11 中直到 10 行的代码是可以执行的!</p>
<h3 id="增加改变-content-行为的-approve-方法"><a class="header" href="#增加改变-content-行为的-approve-方法">增加改变 <code>content</code> 行为的 <code>approve</code> 方法</a></h3>
<p><code>approve</code> 方法将与 <code>request_review</code> 方法类似:它会将 <code>state</code> 设置为审核通过时应处于的状态,如示例 17-16 所示。</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground"><span class="boring">pub struct Post {
</span><span class="boring"> state: Option&lt;Box&lt;dyn State&gt;&gt;,
</span><span class="boring"> content: String,
</span><span class="boring">}
</span><span class="boring">
</span>impl Post {
// --snip--
<span class="boring"> pub fn new() -&gt; Post {
</span><span class="boring"> Post {
</span><span class="boring"> state: Some(Box::new(Draft {})),
</span><span class="boring"> content: String::new(),
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn add_text(&amp;mut self, text: &amp;str) {
</span><span class="boring"> self.content.push_str(text);
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn content(&amp;self) -&gt; &amp;str {
</span><span class="boring"> &quot;&quot;
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn request_review(&amp;mut self) {
</span><span class="boring"> if let Some(s) = self.state.take() {
</span><span class="boring"> self.state = Some(s.request_review())
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span> pub fn approve(&amp;mut self) {
if let Some(s) = self.state.take() {
self.state = Some(s.approve())
}
}
}
trait State {
fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt;;
fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt;;
}
struct Draft {}
impl State for Draft {
// --snip--
<span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> Box::new(PendingReview {})
</span><span class="boring"> }
</span><span class="boring">
</span> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
self
}
}
struct PendingReview {}
impl State for PendingReview {
// --snip--
<span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> self
</span><span class="boring"> }
</span><span class="boring">
</span> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
Box::new(Published {})
}
}
struct Published {}
impl State for Published {
fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
self
}
fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
self
}
}</code></pre>
<p><span class="caption">示例 17-16: 为 <code>Post</code><code>State</code> trait 实现 <code>approve</code> 方法</span></p>
<p>这里为 <code>State</code> trait 增加了 <code>approve</code> 方法,并新增了一个实现了 <code>State</code> 的结构体,<code>Published</code> 状态。</p>
<p>类似于 <code>PendingReview</code><code>request_review</code> 的工作方式,如果对 <code>Draft</code> 调用 <code>approve</code> 方法,并没有任何效果,因为它会返回 <code>self</code>。当对 <code>PendingReview</code> 调用 <code>approve</code> 时,它返回一个新的、装箱的 <code>Published</code> 结构体的实例。<code>Published</code> 结构体实现了 <code>State</code> trait同时对于 <code>request_review</code><code>approve</code> 两方法来说,它返回自身,因为在这两种情况博文应该保持 <code>Published</code> 状态。</p>
<p>现在需要更新 <code>Post</code><code>content</code> 方法。我们希望 <code>content</code> 根据 <code>Post</code> 的当前状态返回值,所以需要 <code>Post</code> 代理一个定义于 <code>state</code> 上的 <code>content</code> 方法,如实例 17-17 所示:</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust ignore does_not_compile"><span class="boring">pub struct Post {
</span><span class="boring"> state: Option&lt;Box&lt;dyn State&gt;&gt;,
</span><span class="boring"> content: String,
</span><span class="boring">}
</span><span class="boring">
</span>impl Post {
// --snip--
<span class="boring"> pub fn new() -&gt; Post {
</span><span class="boring"> Post {
</span><span class="boring"> state: Some(Box::new(Draft {})),
</span><span class="boring"> content: String::new(),
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn add_text(&amp;mut self, text: &amp;str) {
</span><span class="boring"> self.content.push_str(text);
</span><span class="boring"> }
</span><span class="boring">
</span> pub fn content(&amp;self) -&gt; &amp;str {
self.state.as_ref().unwrap().content(self)
}
// --snip--
<span class="boring">
</span><span class="boring"> pub fn request_review(&amp;mut self) {
</span><span class="boring"> if let Some(s) = self.state.take() {
</span><span class="boring"> self.state = Some(s.request_review())
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn approve(&amp;mut self) {
</span><span class="boring"> if let Some(s) = self.state.take() {
</span><span class="boring"> self.state = Some(s.approve())
</span><span class="boring"> }
</span><span class="boring"> }
</span>}
<span class="boring">
</span><span class="boring">trait State {
</span><span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt;;
</span><span class="boring"> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt;;
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">struct Draft {}
</span><span class="boring">
</span><span class="boring">impl State for Draft {
</span><span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> Box::new(PendingReview {})
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> self
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">struct PendingReview {}
</span><span class="boring">
</span><span class="boring">impl State for PendingReview {
</span><span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> self
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> Box::new(Published {})
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">struct Published {}
</span><span class="boring">
</span><span class="boring">impl State for Published {
</span><span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> self
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> self
</span><span class="boring"> }
</span><span class="boring">}</span></code></pre>
<p><span class="caption">示例 17-17: 更新 <code>Post</code><code>content</code> 方法来委托调用 <code>State</code><code>content</code> 方法</span></p>
<p>因为目标是将所有像这样的规则保持在实现了 <code>State</code> 的结构体中,我们将调用 <code>state</code> 中的值的 <code>content</code> 方法并传递博文实例(也就是 <code>self</code>)作为参数。接着返回 <code>state</code> 值的 <code>content</code> 方法的返回值。</p>
<p>这里调用 <code>Option</code><code>as_ref</code> 方法是因为需要 <code>Option</code> 中值的引用而不是获取其所有权。因为 <code>state</code> 是一个 <code>Option&lt;Box&lt;dyn State&gt;&gt;</code>,调用 <code>as_ref</code> 会返回一个 <code>Option&lt;&amp;Box&lt;dyn State&gt;&gt;</code>。如果不调用 <code>as_ref</code>,将会得到一个错误,因为不能将 <code>state</code> 移动出借用的 <code>&amp;self</code> 函数参数。</p>
<p>接着调用 <code>unwrap</code> 方法,这里我们知道它永远也不会 panic因为 <code>Post</code> 的所有方法都确保在它们返回时 <code>state</code> 会有一个 <code>Some</code> 值。这就是一个第十二章 <a href="ch09-03-to-panic-or-not-to-panic.html#%E5%BD%93%E6%88%91%E4%BB%AC%E6%AF%94%E7%BC%96%E8%AF%91%E5%99%A8%E7%9F%A5%E9%81%93%E6%9B%B4%E5%A4%9A%E7%9A%84%E6%83%85%E5%86%B5">“当我们比编译器知道更多的情况”</a> 部分讨论过的我们知道 <code>None</code> 是不可能的而编译器却不能理解的情况。</p>
<p>接着我们就有了一个 <code>&amp;Box&lt;dyn State&gt;</code>,当调用其 <code>content</code>Deref 强制转换会作用于 <code>&amp;</code><code>Box</code> ,这样最终会调用实现了 <code>State</code> trait 的类型的 <code>content</code> 方法。这意味着需要为 <code>State</code> trait 定义增加 <code>content</code>,这也是放置根据所处状态返回什么内容的逻辑的地方,如示例 17-18 所示:</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground"><span class="boring">pub struct Post {
</span><span class="boring"> state: Option&lt;Box&lt;dyn State&gt;&gt;,
</span><span class="boring"> content: String,
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Post {
</span><span class="boring"> pub fn new() -&gt; Post {
</span><span class="boring"> Post {
</span><span class="boring"> state: Some(Box::new(Draft {})),
</span><span class="boring"> content: String::new(),
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn add_text(&amp;mut self, text: &amp;str) {
</span><span class="boring"> self.content.push_str(text);
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn content(&amp;self) -&gt; &amp;str {
</span><span class="boring"> self.state.as_ref().unwrap().content(self)
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn request_review(&amp;mut self) {
</span><span class="boring"> if let Some(s) = self.state.take() {
</span><span class="boring"> self.state = Some(s.request_review())
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn approve(&amp;mut self) {
</span><span class="boring"> if let Some(s) = self.state.take() {
</span><span class="boring"> self.state = Some(s.approve())
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">
</span>trait State {
// --snip--
<span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt;;
</span><span class="boring"> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt;;
</span><span class="boring">
</span> fn content&lt;'a&gt;(&amp;self, post: &amp;'a Post) -&gt; &amp;'a str {
&quot;&quot;
}
}
// --snip--
<span class="boring">
</span><span class="boring">struct Draft {}
</span><span class="boring">
</span><span class="boring">impl State for Draft {
</span><span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> Box::new(PendingReview {})
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> self
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">struct PendingReview {}
</span><span class="boring">
</span><span class="boring">impl State for PendingReview {
</span><span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> self
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> Box::new(Published {})
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">
</span>struct Published {}
impl State for Published {
// --snip--
<span class="boring"> fn request_review(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> self
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> fn approve(self: Box&lt;Self&gt;) -&gt; Box&lt;dyn State&gt; {
</span><span class="boring"> self
</span><span class="boring"> }
</span><span class="boring">
</span> fn content&lt;'a&gt;(&amp;self, post: &amp;'a Post) -&gt; &amp;'a str {
&amp;post.content
}
}</code></pre>
<p><span class="caption">示例 17-18: 为 <code>State</code> trait 增加 <code>content</code> 方法</span></p>
<p>这里增加了一个 <code>content</code> 方法的默认实现来返回一个空字符串 slice。这意味着无需为 <code>Draft</code><code>PendingReview</code> 结构体实现 <code>content</code> 了。<code>Published</code> 结构体会覆盖 <code>content</code> 方法并会返回 <code>post.content</code> 的值。</p>
<p>注意这个方法需要生命周期注解,如第十章所讨论的。这里获取 <code>post</code> 的引用作为参数,并返回 <code>post</code> 一部分的引用,所以返回的引用的生命周期与 <code>post</code> 参数相关。</p>
<p>现在示例完成了 —— 现在示例 17-11 中所有的代码都能工作!我们通过发布博文工作流的规则实现了状态模式。围绕这些规则的逻辑都存在于状态对象中而不是分散在 <code>Post</code> 之中。</p>
<blockquote>
<h4 id="为什么不用枚举"><a class="header" href="#为什么不用枚举">为什么不用枚举?</a></h4>
<p>你可能会好奇为什么不用包含不同可能的博文状态的 <code>enum</code> 作为变量。这确实是一个可能的方案,尝试实现并对比最终结果来看看哪一种更适合你!使用枚举的一个缺点是每一个检查枚举值的地方都需要一个 <code>match</code> 表达式或类似的代码来处理所有可能的成员。这相比 trait 对象模式可能显得更重复。</p>
</blockquote>
<h3 id="状态模式的权衡取舍"><a class="header" href="#状态模式的权衡取舍">状态模式的权衡取舍</a></h3>
<p>我们展示了 Rust 是能够实现面向对象的状态模式的,以便能根据博文所处的状态来封装不同类型的行为。<code>Post</code> 的方法并不知道这些不同类型的行为。通过这种组织代码的方式,要找到所有已发布博文的不同行为只需查看一处代码:<code>Published</code><code>State</code> trait 的实现。</p>
<p>如果要创建一个不使用状态模式的替代实现,则可能会在 <code>Post</code> 的方法中,或者甚至于在 <code>main</code> 代码中用到 <code>match</code> 语句,来检查博文状态并在这里改变其行为。这意味着需要查看很多位置来理解处于发布状态的博文的所有逻辑!这在增加更多状态时会变得更糟:每一个 <code>match</code> 语句都会需要另一个分支。</p>
<p>对于状态模式来说,<code>Post</code> 的方法和使用 <code>Post</code> 的位置无需 <code>match</code> 语句,同时增加新状态只涉及到增加一个新 <code>struct</code> 和为其实现 trait 的方法。</p>
<p>这个实现易于扩展增加更多功能。为了体会使用此模式维护代码的简洁性,请尝试如下一些建议:</p>
<ul>
<li>增加 <code>reject</code> 方法将博文的状态从 <code>PendingReview</code> 变回 <code>Draft</code></li>
<li>在将状态变为 <code>Published</code> 之前需要两次 <code>approve</code> 调用</li>
<li>只允许博文处于 <code>Draft</code> 状态时增加文本内容。提示:让状态对象负责内容可能发生什么改变,但不负责修改 <code>Post</code></li>
</ul>
<p>状态模式的一个缺点是因为状态实现了状态之间的转换,一些状态会相互联系。如果在 <code>PendingReview</code><code>Published</code> 之间增加另一个状态,比如 <code>Scheduled</code>,则不得不修改 <code>PendingReview</code> 中的代码来转移到 <code>Scheduled</code>。如果 <code>PendingReview</code> 无需因为新增的状态而改变就更好了,不过这意味着切换到另一种设计模式。</p>
<p>另一个缺点是我们会发现一些重复的逻辑。为了消除它们,可以尝试为 <code>State</code> trait 中返回 <code>self</code><code>request_review</code><code>approve</code> 方法增加默认实现,不过这会违反对象安全性,因为 trait 不知道 <code>self</code> 具体是什么。我们希望能够将 <code>State</code> 作为一个 trait 对象,所以需要其方法是对象安全的。</p>
<p>另一个重复是 <code>Post</code><code>request_review</code><code>approve</code> 这两个类似的实现。它们都委托调用了 <code>state</code> 字段中 <code>Option</code> 值的同一方法,并在结果中为 <code>state</code> 字段设置了新值。如果 <code>Post</code> 中的很多方法都遵循这个模式,我们可能会考虑定义一个宏来消除重复(查看第十九章的 <a href="ch19-06-macros.html#%E5%AE%8F">“宏”</a> 部分)。</p>
<p>完全按照面向对象语言的定义实现这个模式并没有尽可能地利用 Rust 的优势。让我们看看一些代码中可以做出的修改,来将无效的状态和状态转移变为编译时错误。</p>
<h4 id="将状态和行为编码为类型"><a class="header" href="#将状态和行为编码为类型">将状态和行为编码为类型</a></h4>
<p>我们将展示如何稍微反思状态模式来进行一系列不同的权衡取舍。不同于完全封装状态和状态转移使得外部代码对其毫不知情我们将状态编码进不同的类型。如此Rust 的类型检查就会将任何在只能使用发布博文的地方使用草案博文的尝试变为编译时错误。</p>
<p>让我们考虑一下示例 17-11 中 <code>main</code> 的第一部分:</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><code class="language-rust ignore"><span class="boring">use blog::Post;
</span><span class="boring">
</span>fn main() {
let mut post = Post::new();
post.add_text(&quot;I ate a salad for lunch today&quot;);
assert_eq!(&quot;&quot;, post.content());
<span class="boring">
</span><span class="boring"> post.request_review();
</span><span class="boring"> assert_eq!(&quot;&quot;, post.content());
</span><span class="boring">
</span><span class="boring"> post.approve();
</span><span class="boring"> assert_eq!(&quot;I ate a salad for lunch today&quot;, post.content());
</span>}</code></pre>
<p>我们仍然希望能够使用 <code>Post::new</code> 创建一个新的草案博文,并能够增加博文的内容。不过不同于存在一个草案博文时返回空字符串的 <code>content</code> 方法,我们将使草案博文完全没有 <code>content</code> 方法。这样如果尝试获取草案博文的内容,将会得到一个方法不存在的编译错误。这使得我们不可能在生产环境意外显示出草案博文的内容,因为这样的代码甚至就不能编译。示例 17-19 展示了 <code>Post</code> 结构体、<code>DraftPost</code> 结构体以及各自的方法的定义:</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground">pub struct Post {
content: String,
}
pub struct DraftPost {
content: String,
}
impl Post {
pub fn new() -&gt; DraftPost {
DraftPost {
content: String::new(),
}
}
pub fn content(&amp;self) -&gt; &amp;str {
&amp;self.content
}
}
impl DraftPost {
pub fn add_text(&amp;mut self, text: &amp;str) {
self.content.push_str(text);
}
}</code></pre>
<p><span class="caption">示例 17-19: 带有 <code>content</code> 方法的 <code>Post</code> 和没有 <code>content</code> 方法的 <code>DraftPost</code></span></p>
<p><code>Post</code><code>DraftPost</code> 结构体都有一个私有的 <code>content</code> 字段来储存博文的文本。这些结构体不再有 <code>state</code> 字段因为我们将状态编码改为结构体类型。<code>Post</code> 将代表发布的博文,它有一个返回 <code>content</code><code>content</code> 方法。</p>
<p>仍然有一个 <code>Post::new</code> 函数,不过不同于返回 <code>Post</code> 实例,它返回 <code>DraftPost</code> 的实例。现在不可能创建一个 <code>Post</code> 实例,因为 <code>content</code> 是私有的同时没有任何函数返回 <code>Post</code></p>
<p><code>DraftPost</code> 上定义了一个 <code>add_text</code> 方法,这样就可以像之前那样向 <code>content</code> 增加文本,不过注意 <code>DraftPost</code> 并没有定义 <code>content</code> 方法!如此现在程序确保了所有博文都从草案开始,同时草案博文没有任何可供展示的内容。任何绕过这些限制的尝试都会产生编译错误。</p>
<h4 id="实现状态转移为不同类型的转换"><a class="header" href="#实现状态转移为不同类型的转换">实现状态转移为不同类型的转换</a></h4>
<p>那么如何得到发布的博文呢?我们希望强制执行的规则是草案博文在可以发布之前必须被审核通过。等待审核状态的博文应该仍然不会显示任何内容。让我们通过增加另一个结构体 <code>PendingReviewPost</code> 来实现这个限制,在 <code>DraftPost</code> 上定义 <code>request_review</code> 方法来返回 <code>PendingReviewPost</code>,并在 <code>PendingReviewPost</code> 上定义 <code>approve</code> 方法来返回 <code>Post</code>,如示例 17-20 所示:</p>
<p><span class="filename">文件名src/lib.rs</span></p>
<pre><code class="language-rust noplayground"><span class="boring">pub struct Post {
</span><span class="boring"> content: String,
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">pub struct DraftPost {
</span><span class="boring"> content: String,
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">impl Post {
</span><span class="boring"> pub fn new() -&gt; DraftPost {
</span><span class="boring"> DraftPost {
</span><span class="boring"> content: String::new(),
</span><span class="boring"> }
</span><span class="boring"> }
</span><span class="boring">
</span><span class="boring"> pub fn content(&amp;self) -&gt; &amp;str {
</span><span class="boring"> &amp;self.content
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">
</span>impl DraftPost {
// --snip--
<span class="boring"> pub fn add_text(&amp;mut self, text: &amp;str) {
</span><span class="boring"> self.content.push_str(text);
</span><span class="boring"> }
</span><span class="boring">
</span> pub fn request_review(self) -&gt; PendingReviewPost {
PendingReviewPost {
content: self.content,
}
}
}
pub struct PendingReviewPost {
content: String,
}
impl PendingReviewPost {
pub fn approve(self) -&gt; Post {
Post {
content: self.content,
}
}
}</code></pre>
<p><span class="caption">列表 17-20: <code>PendingReviewPost</code> 通过调用 <code>DraftPost</code><code>request_review</code> 创建,<code>approve</code> 方法将 <code>PendingReviewPost</code> 变为发布的 <code>Post</code></span></p>
<p><code>request_review</code><code>approve</code> 方法获取 <code>self</code> 的所有权,因此会消费 <code>DraftPost</code><code>PendingReviewPost</code> 实例,并分别转换为 <code>PendingReviewPost</code> 和发布的 <code>Post</code>。这样在调用 <code>request_review</code> 之后就不会遗留任何 <code>DraftPost</code> 实例,后者同理。<code>PendingReviewPost</code> 并没有定义 <code>content</code> 方法,所以尝试读取其内容会导致编译错误,<code>DraftPost</code> 同理。因为唯一得到定义了 <code>content</code> 方法的 <code>Post</code> 实例的途径是调用 <code>PendingReviewPost</code><code>approve</code> 方法,而得到 <code>PendingReviewPost</code> 的唯一办法是调用 <code>DraftPost</code><code>request_review</code> 方法,现在我们就将发博文的工作流编码进了类型系统。</p>
<p>这也意味着不得不对 <code>main</code> 做出一些小的修改。因为 <code>request_review</code><code>approve</code> 返回新实例而不是修改被调用的结构体,所以我们需要增加更多的 <code>let post = </code> 覆盖赋值来保存返回的实例。也不再能断言草案和等待审核的博文的内容为空字符串了,我们也不再需要它们:不能编译尝试使用这些状态下博文内容的代码。更新后的 <code>main</code> 的代码如示例 17-21 所示:</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><code class="language-rust ignore">use blog::Post;
fn main() {
let mut post = Post::new();
post.add_text(&quot;I ate a salad for lunch today&quot;);
let post = post.request_review();
let post = post.approve();
assert_eq!(&quot;I ate a salad for lunch today&quot;, post.content());
}</code></pre>
<p><span class="caption">示例 17-21: <code>main</code> 中使用新的博文工作流实现的修改</span></p>
<p>不得不修改 <code>main</code> 来重新赋值 <code>post</code> 使得这个实现不再完全遵守面向对象的状态模式:状态间的转换不再完全封装在 <code>Post</code> 实现中。然而,得益于类型系统和编译时类型检查,我们得到了的是无效状态是不可能的!这确保了某些特定的 bug比如显示未发布博文的内容将在部署到生产环境之前被发现。</p>
<p>尝试为示例 17-21 之后的 <code>blog</code> crate 实现这一部分开始所建议的任务来体会使用这个版本的代码是何感觉。注意在这个设计中一些需求可能已经完成了。</p>
<p>即便 Rust 能够实现面向对象设计模式,也有其他像将状态编码进类型这样的模式存在。这些模式有着不同的权衡取舍。虽然你可能非常熟悉面向对象模式,重新思考这些问题来利用 Rust 提供的像在编译时避免一些 bug 这样有益功能。在 Rust 中面向对象模式并不总是最好的解决方案,因为 Rust 拥有像所有权这样的面向对象语言所没有的功能。</p>
<h2 id="总结"><a class="header" href="#总结">总结</a></h2>
<p>阅读本章后,不管你是否认为 Rust 是一个面向对象语言,现在你都见识了 trait 对象是一个 Rust 中获取部分面向对象功能的方法。动态分发可以通过牺牲少量运行时性能来为你的代码提供一些灵活性。这些灵活性可以用来实现有助于代码可维护性的面向对象模式。Rust 也有像所有权这样不同于面向对象语言的功能。面向对象模式并不总是利用 Rust 优势的最好方式,但也是可用的选项。</p>
<p>接下来,让我们看看另一个提供了多样灵活性的 Rust 功能:模式。贯穿全书的模式,我们已经和它们打过照面了,但并没有见识过它们的全部本领。让我们开始探索吧!</p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="ch17-02-trait-objects.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="ch18-00-patterns.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="ch17-02-trait-objects.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="ch18-00-patterns.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js"></script>
<script src="mark.min.js"></script>
<script src="searcher.js"></script>
<script src="clipboard.min.js"></script>
<script src="highlight.js"></script>
<script src="book.js"></script>
<!-- Custom JS scripts -->
<script src="ferris.js"></script>
</div>
</body>
</html>