Skip to Content
Python设计模式(第2版)
book

Python设计模式(第2版)

by Posts & Telecom Press, Chetan Giridhar
February 2024
Intermediate to advanced
139 pages
1h 42m
Chinese
Packt Publishing
Content preview from Python设计模式(第2版)

第4章 门面模式——与门面相适

在上一章中,你已经学习了工厂设计模式。并讨论了该模式的3种变体——简单工厂、工厂方法和抽象工厂模式。此外,你还学习了如何将它们应用于现实世界,并给出了相应的Python实现。我们还将工厂方法与抽象工厂模式进行了一番比较,并列出了其优缺点。我们知道,无论工厂设计模式还是单例设计模式(参见第2章),都属于创建型设计模式。

在这一章中,我们继续学习另外一种类型的设计模式,即结构型设计模式。这里,我们要介绍的是门面设计模式,以及如何将其应用于软件应用程序开发。我们将提供一个示例,并通过Python v3.5实现了该示例。

简而言之,我们将在本章中讨论下列主题:

  • 结构型设计模式概要;
  • 利用UML图理解门面设计模式;
  • 提供了Python v3.5实现代码的真实用例;
  • 门面模式与最少知识原则。

以下几点将有助于我们更好地了解结构型设计模式。

  • 结构型模式描述如何将对象和类组合成更大的结构。
  • 结构型模式是一种能够简化设计工作的模式,因为它能够找出更简单的方法来认识或表示实体之间的关系。在面向对象世界中,实体指的是对象或类。
  • 类模式可以通过继承来描述抽象,从而提供更有用的程序接口,而对象模式则描述了如何将对象联系起来从而组合成更大的对象。结构型模式是类和对象模式的综合体。

下面给出结构型设计模式的几个例子。你会注意到,它们都是通过对象或类之间的交互来实现更高级的设计或架构目标的。

下面是一些结构型设计模式的例子。

  • 适配器模式:将一个接口转换成客户希望的另外一个接口。它试图根据客户端的需求来匹配不同类的接口。
  • 桥接模式:该模式将对象的接口与其实现进行解耦,使得两者可以独立工作。
  • 装饰器模式:该模式允许在运行时或以动态方式为对象添加职责。我们可以通过接口给对象添加某些属性。 ...
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

Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott
算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)
人工智能技术与大数据

人工智能技术与大数据

Posts & Telecom Press, Anand Deshpande, Manish Kumar

Publisher Resources

ISBN: 9781835880043