Skip to Content
编写整洁的Python代码(第2版)
book

编写整洁的Python代码(第2版)

by Posts & Telecom Press, Mariano Anaya
February 2024
Intermediate to advanced
326 pages
4h 39m
Chinese
Packt Publishing
Content preview from 编写整洁的Python代码(第2版)

第9章 常见设计模式

设计模式最初由著名的四人组(Gang of Four,GoF)在其著作Design Patterns: Elements of Reusable Object-Oriented Software中提出,自此以后这个主题在软件工程领域得以广泛传播。设计模式可以帮助解决常见的问题,这是使用适合特定场景的抽象实现的。在实现正确的情况下,设计模式可以改善解决方案的总体设计。

本章介绍一些常见的设计模式,但不涉及如何在特定情况下应用现成的设计模式,而分析设计模式对编写整洁代码有何帮助。给出实现设计模式的解决方案后,我们将进行分析,指出如果最初选择了不同的路径,最终的实现更好。

通过这样的分析,你将明白如何在Python中具体地实现设计模式。这将让你意识到,Python 的动态特征使得设计模式的 Python 实现与静态类型语言实现之间存在一些差异(很多设计模式都源自静态类型语言)。这意味着Python中的设计模式有一些独特之处。在有些情况下,如果试图应用并不适用的设计模式,编写出的代码将不符合Python的语言习惯。

本章涵盖如下主题。

常见设计模式。

在Python中不适用的设计模式及其符合Python语言习惯的替代解决方案。

以符合Python语言习惯的方式实现常见的设计模式。

优良的抽象是如何自然而然地演变成模式的。

通过前面的学习,你现在能够从较高的层级对设计进行分析,同时从详细实现的角度思考(如何使用Python特性以效率最高的方式编写代码)。

本章讨论如何使用设计模式让代码更整洁,我们先来说说在Python中使用设计模式时需要注意的事项。

面向对象的设计模式属于软件构建理念,在处理问题模型时经常能派上用场。设计模式是高层次的理念,不与特定的编程语言挂钩,而是有关应用程序中对象如何交互的通用概念。当然,设计模式也包含实现细节,这些细节随使用的语言而异,并非设计模式的精髓所在。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Excel金融建模

Excel金融建模

Posts & Telecom Press, Shmuel Oluwa
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
软件开发实践:项目驱动式的Java开发指南

软件开发实践:项目驱动式的Java开发指南

Raoul-Gabriel Urma, Richard Warburton

Publisher Resources

ISBN: 9781835888988