<人人都懂设计模式>-中介模式
真正的用房屋中介来作例子,
好的书籍总是让人记忆深刻。
class HouseInfo:
def __init__(self, area, price, has_window, has_bathroom, has_kitchen, address, owner):
self.__area = area
self.__price = price
self.__has_window = has_window
self.__has_bathroom = has_bathroom
self.__has_kitchen = has_kitchen
self.__address = address
self.__owner = owner
def get_address(self):
return self.__address
def get_owner_name(self):
return self.__owner
def show_info(self, is_show_owner=True):
print("面积:" + str(self.__area) + "平方米",
"价格:" + str(self.__price) + "元",
"窗户:" + ("有" if self.__has_window else "没有"),
"卫生间:" + self.__has_bathroom,
"厨房:" + ("有" if self.__has_kitchen else "没有"),
"地址:" + self.__address,
"房东:" + self.get_owner_name() if is_show_owner else "",
)
class HouseAgency:
def __init__(self, name):
self.__house_infos = []
self.__name = name
def get_name(self):
return self.__name
def add_house_info(self, house_info):
self.__house_infos.append(house_info)
def remove_house_info(self, house_info):
for info in self.__house_infos:
if info == house_info:
self.__house_infos.remove(info)
def get_search_condition(self, description):
return description
def get_match_infos(self, search_condition):
print(self.get_name(), "为您找到以下最合适房源:")
for info in self.__house_infos:
info.show_info(False)
return self.__house_infos
def sign_contract(self, house_info, period):
print("{} 与房东 {} 签订 {} 的房子的租赁合同,租期{}年,合同期内, {}有权限对其进行使用和转租。".format(
self.get_name(), house_info.get_owner_name(), house_info.get_address(), period, self.get_name()
))
def sign_contracts(self, period):
for info in self.__house_infos:
self.sign_contract(info, period)
class HouseOwner:
def __init__(self, name, address):
self.__name = name
self.__address = address
self.__house_info = None
def get_name(self):
return self.__name
def set_house_info(self, area, price, has_window, has_bathroom, has_kitchen):
self.__house_info = HouseInfo(area, price, has_window, has_bathroom, has_kitchen, self.__address, self.get_name())
def publish_house_info(self, agency):
agency.add_house_info(self.__house_info)
print("{}在{}发布房源出租信息。".format(self.get_name(), agency.get_name()))
class Customer:
def __init__(self, name):
self.__name = name
def get_name(self):
return self.__name
def find_house(self, description, agency):
print("我是{}, 我想要找一个{}的房子".format(self.get_name(), description))
print()
return agency.get_match_infos(agency.get_search_condition(description))
def see_house(self, house_infos):
size = len(house_infos)
return house_infos[size-1]
def sign_contract(self, house_info, agency, period):
print("{}与中介{}签订{}的房子租赁合同,租期{}年,合同期内,{}有权对其进行使用!".format(
self.get_name(), agency.get_name(), house_info.get_address(),period, self.__name
))
def test_renting():
my_home = HouseAgency("我爱我家")
zhangsan = HouseOwner("张三", "上地西里")
zhangsan.set_house_info(20, 2500, 1, "独立卫生间", 0)
zhangsan.publish_house_info(my_home)
lisi = HouseOwner("李四", "当代城市家园")
lisi.set_house_info(16, 1800, 1, "公用卫生间", 0)
lisi.publish_house_info(my_home)
wangwu = HouseOwner("王五", "金阳美和园")
wangwu.set_house_info(18, 2600, 1, "独立卫生间", 1)
wangwu.publish_house_info(my_home)
print()
my_home.sign_contracts(3)
print()
tony = Customer("Tony")
house_infos = tony.find_house("18平米左右, 要有独立卫生间,要有窗户, 最好朝南,有厨房更好,价位在2000左右", my_home)
print()
print("正在看房,寻找最合适的房源。。。")
print()
appropriate_house = tony.see_house(house_infos)
tony.sign_contract(appropriate_house, my_home, 1)
test_renting()
C:\Python36\python.exe C:/Users/Sahara/PycharmProjects/test/python_search.py 张三在我爱我家发布房源出租信息。 李四在我爱我家发布房源出租信息。 王五在我爱我家发布房源出租信息。 我爱我家 与房东 张三 签订 上地西里 的房子的租赁合同,租期3年,合同期内, 我爱我家有权限对其进行使用和转租。 我爱我家 与房东 李四 签订 当代城市家园 的房子的租赁合同,租期3年,合同期内, 我爱我家有权限对其进行使用和转租。 我爱我家 与房东 王五 签订 金阳美和园 的房子的租赁合同,租期3年,合同期内, 我爱我家有权限对其进行使用和转租。 我是Tony, 我想要找一个18平米左右, 要有独立卫生间,要有窗户, 最好朝南,有厨房更好,价位在2000左右的房子 我爱我家 为您找到以下最合适房源: 面积:20平方米 价格:2500元 窗户:有 卫生间:独立卫生间 厨房:没有 地址:上地西里 面积:16平方米 价格:1800元 窗户:有 卫生间:公用卫生间 厨房:没有 地址:当代城市家园 面积:18平方米 价格:2600元 窗户:有 卫生间:独立卫生间 厨房:有 地址:金阳美和园 正在看房,寻找最合适的房源。。。 Tony与中介我爱我家签订金阳美和园的房子租赁合同,租期1年,合同期内,Tony有权对其进行使用! Process finished with exit code
<人人都懂设计模式>-中介模式的更多相关文章
- <人人都懂设计模式>-状态模式
同样是水,固态,气态,液态的变化,是由温度引起. 引此为思考状态模式. from abc import ABCMeta, abstractmethod # 引入ABCMeta和abstractmeth ...
- <人人都懂设计模式>-单例模式
这个模式,我还是了解的. 书上用了三种不同的方法. class Singleton1: # 单例实现方式1 __instance = None __is_first_init = False def ...
- <人人都懂设计模式>-装饰模式
书上,真的用一个人穿衣打拌来讲解装饰模式的呢. from abc import ABCMeta, abstractmethod class Person(metaclass=ABCMeta): def ...
- 人人都懂区块链--pdf电子版学习资料下载
人人都懂区块链 21天从区块链“小白”到资深玩家电子版pdf下载 链接:https://pan.baidu.com/s/1TWxYv4TLa2UtTgU-HqLECQ 提取码:6gy0 好的学习资料需 ...
- [工作中的设计模式]中介模式模式Mediator
一.模式解析 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 中介模式又叫调停者模式,他有如下特点: 1.有多个系统或者对 ...
- 【人人都懂密码学】一篇最易懂的Java密码学入门教程
密码与我们的生活息息相关,远到国家机密,近到个人账户,我们每天都在跟密码打交道: 那么,密码从何而来?生活中常见的加密是怎么实现的?怎么保证个人信息安全?本文将从这几方面进行浅谈,如有纰漏,敬请各位大 ...
- 人人都懂的HTML基础知识-HTML教程(1)
01.HTML基础简介 HTML (HyperText Markup Language,超文本标记语言) 不是一门编程语言,而是一种用于定义内容结构的标记语言,用来描述网页内容,文件格式为.html. ...
- 7. 星际争霸之php设计模式--中介者模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- Java设计模式(16)中介模式(Mediator模式)
Mediator定义:用一个中介对象来封装一系列关于对象交互行为. 为何使用Mediator模式/中介模式 各个对象之间的交互操作非常多,每个对象的行为操作都依赖彼此对方,修改一个对象的行为,同时会涉 ...
随机推荐
- [NOIP2015]运输计划 线段树or差分二分
目录 [NOIP2015]运输计划 链接 思路1 暴力数据结构 思路2 二分树上差分 总的 代码1 代码2 [NOIP2015]运输计划 链接 luogu 好久没写博客了,水一篇波. 思路1 暴力数据 ...
- Attention篇(二)
主要是对<Attention is all you need>的分析 结合:http://www.cnblogs.com/robert-dlut/p/8638283.html 以及自己的 ...
- 关于几类STL容器swap的复杂度问题
\(swap\)的方式有 \(S1.swap(S2)\) 或 \(swap(S1,S2)\) \(vector,map,set,deque \ \ \ \ swap\)复杂度:\(O(1)\) \(p ...
- [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- [LeetCode] 337. House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 苹果开发之App签名
如果你的Apple ID账号(可使用邮箱来注册)为Apple developer类型的话,登录之后是看不到Certificates, Indentifiers & Profiles信息的 Ap ...
- nth-of-type(n)和nth-child(n)的区别
nth-of-type 与nth-child都属于css选择器,都是在同级别节点中找到第n个元素,前者是先确定元素类型,再计算n的位置:后者是先确定n的位置,再确定元素类型 例: <div cl ...
- 微信小程序起步
微信小程序 文档 微信小程序开发文档 本质 so微信小程序到底是什么?是原生的app还是H5应用? 简单来说,小程序是一种应用,运行的环境是微信(App)进程中,使用了部分的H5技术 目录介绍 app ...
- python 做词云图
#导入需要模块 import jieba import numpy as np import matplotlib.pyplot as plt from PIL import Image from w ...