加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python设计模式之门面模式

发布时间:2020-12-17 00:17:34 所属栏目:Python 来源:网络整理
导读:div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=10035644"gt; h1 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaa

<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=10035644"&gt;
<h1 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">一.结构型设计模式
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">门面模式与单例模式,工厂模式不同,它是一种结构型模式。


<blockquote style="margin: 1.2em 0px; border-left: 4px solid #dddddd; padding: 0px 1em; color: #777777; quotes: none;">
<ul style="margin: 1.2em 0px; padding-left: 2em; list-style-type: square; font-size: 16px;">
<li style="margin: 0.5em 0px; font-size: 16px;">结构型模式描述如何将对象和类组合成更大的结构
<li style="margin: 0.5em 0px; font-size: 16px;">结构型模式是一种能够简化设计工作的模式,它能找出更简单的方法来认识或表示实体之间的关系。
<li style="margin: 0.5em 0px; font-size: 16px;">结构型模式是类和对象模式的综合体。类模式通过继承来描述抽象,从而提供更有用的程序接口,而对象模式描述了如何将对象联系起来从而组合成更大的对象。

 :
     :
        Waiter().make_set_meal1()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Waiter<span class="hljs-params" style="color: #ffc58f;">():
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_set_meal_1<span class="hljs-params" style="color: #ffc58f;">(self):
Coke().make()
Hamburger().make()
French_fries().make()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coke<span class="hljs-params" style="color: #ffc58f;">():
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'making coke')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Hamburger<span class="hljs-params" style="color: #ffc58f;">():
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'making hamburger')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">French_fries<span class="hljs-params" style="color: #ffc58f;">():
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'making french fries')

you=Client()
you.order()
<span class="hljs-string" style="color: #d1f1a9;">'''

making coke
making hamburger
making french fries

'''


<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">这里的make_set_meal_1,做套餐1,也就是让子系统的组合完成任务,将这个复杂的组合封装了起来。当然可能还有其他类型的套餐,那么Waiter里还可以添加其他的方法,但是这与子系统没有太大的联系,只是增加了又一种组合,例如只点可乐和汉堡的套餐,只需要增加Waiter的方法,不需要修改子系统。


<h1 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">四.最少知识原则


<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">门面模式背后的设计原理就是最少知识原则,它的理念为减少对象之间的交互,这意味着:


<ul style="margin: 1.2em 0px; padding-left: 2em; list-style-type: square; font-size: 16px;">
<li style="margin: 0.5em 0px; font-size: 16px;">对于设计系统时创建的每个对象,都应该考察与之交互的类的数量以及交互的方式
<li style="margin: 0.5em 0px; font-size: 16px;">这样能够避免创建许多彼此紧密耦合的类,如果类与类之间存在大量的依赖关系,那么对于系统的任何一部分的修改都有可能导致系统的其他部分无意的被改变。


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读