<人人都懂设计模式>-中介模式

2022-10-24,,,,

真正的用房屋中介来作例子,

好的书籍总是让人记忆深刻。

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 

<人人都设计模式>-中介模式的相关教程结束。

《<人人都懂设计模式>-中介模式.doc》

下载本文的Word格式文档,以方便收藏与打印。