"""
中介模式:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显示地相互引用,
从而使其耦合松散,而且可以独立地改变他们之间的交互
"""
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.get_name()
def show_info(self, is_show_owner=True):
template = "面积: {}平方米, 价格: {}元, 窗户: {}, 卫生间: {}, 厨房: {}, 地址: {}, 房东: {}."
print(template.format(self.__area,
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 HousingAgency:
"""房屋中介"""
def __init__(self, name):
self.__houses_info = []
self.__name = name
def get_name(self):
return self.__name
def add_house_info(self, house_info):
self.__houses_info.append(house_info)
def remove_house_info(self, house_info):
for info in self.__houses_info:
if info == house_info:
self.__houses_info.remove(info)
# 边遍历边删除
def get_search_condition(self, description):
"""省略将用户描述信息转换成搜索条件的过程"""
return description
def get_match_info(self, search_condition):
"""根据房源信息的各项属性查找最匹配的信息,此处省略匹配过程,全部输出"""
print(self.get_name(), "为您找到以下最合适的房源: ")
for info in self.__houses_info:
info.show_info()
return self.__houses_info
def sign_contract(self, house_info, period):
"""与房东签订协议"""
template = "{}与房东{}签订{}的房子的租赁合同,租期{}年,合同期内{}有权对其进行使用和转租。"
print(template.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.__houses_info:
self.sign_contract(info, period)
class HouseOwner:
"""房东"""
def __init__(self, name):
self.__name = name
self.__house_info = None
def get_name(self):
return self.__name
def set_house_info(self, address, area, price, has_window, bathroom, kitchen):
self.__house_info = HouseInfo(area, price, has_window, bathroom, kitchen, address, self)
def publish_house_info(self, agency):
# 发布房源
agency.add_house_info(self.__house_info)
template = "{}在{}发布房源出租信息。"
print(template.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):
template = "我是{},我想找一个{}的房子"
print(template.format(self.get_name(), description))
return agency.get_match_info(agency.get_search_condition(description))
def see_house(self, houses_info):
# 看房,选择最合适的房子,省略看房选房的过程,直接选择最后一个间
return houses_info[-1]
def sign_contract(self, house_info, agency, period):
# 与中介签订协议
template = "{}与中介签订{}房子的租赁合同,租期{}年。合同期内{}有权对其进行使用!"
print(template.format(self.get_name(),
agency.get_name(),
period,
self.get_name()))
def test_renting():
my_home = HousingAgency("我爱我家")
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)
tony = Customer('Tony')
houses_info = tony.find_house('20平米左右,要有独立卫生间、厨房,房间有床,朝南,价格不高于2000!', my_home)
appropriate_house = tony.see_house(houses_info)
tony.sign_contract(appropriate_house, my_home, 1)
if __name__ == '__main__':
test_renting()
中介模式(python)
最新推荐文章于 2022-05-19 23:54:23 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)