类模式实现耳机接口转换:

代码如下:

class Headphone:
    pass

class ThreeFiveMMHeadphone(Headphone):
    def __init__(self):
        self.type = "3.5mm"

class TwoFiveMMHeadphone(Headphone):
    def __init__(self):
        self.type = "2.5mm"

class USBTypeCHeadphone(Headphone):
    def __init__(self):
        self.type = "USB Type-C"

class LightingHeadphone(Headphone):
    def __init__(self):
        self.type = "Lighting"

class Adapter:
    def __init__(self, headphone):
        self.headphone = headphone

class ThreeFiveMMToTwoFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "3.5mm":
            self.headphone = TwoFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert 3.5mm headphone to 2.5mm headphone.")

class ThreeFiveMMToUSBTypeCAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "3.5mm":
            self.headphone = USBTypeCHeadphone()
        else:
            raise ValueError("This adapter can only convert 3.5mm headphone to USB Type-C headphone.")

class ThreeFiveMMToLightingAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "3.5mm":
            self.headphone = LightingHeadphone()
        else:
            raise ValueError("This adapter can only convert 3.5mm headphone to Lighting headphone.")

class TwoFiveMMToThreeFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "2.5mm":
            self.headphone = ThreeFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert 2.5mm headphone to 3.5mm headphone.")

class TwoFiveMMToUSBTypeCAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "2.5mm":
            self.headphone = USBTypeCHeadphone()
        else:
            raise ValueError("This adapter can only convert 2.5mm headphone to USB Type-C headphone.")

class TwoFiveMMToLightingAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "2.5mm":
            self.headphone = LightingHeadphone()
        else:
            raise ValueError("This adapter can only convert 2.5mm headphone to Lighting headphone.")

class USBTypeCToThreeFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "USB Type-C":
            self.headphone = ThreeFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert USB Type-C headphone to 3.5mm headphone.")

class USBTypeCToTwoFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "USB Type-C":
            self.headphone = TwoFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert USB Type-C headphone to 2.5mm headphone.")

class USBTypeCToLightingAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "USB Type-C":
            self.headphone = LightingHeadphone()
        else:
            raise ValueError("This adapter can only convert USB Type-C headphone to Lighting headphone.")

class LightingToThreeFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "Lighting":
            self.headphone = ThreeFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert Lighting headphone to 3.5mm headphone.")

class LightingToTwoFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "Lighting":
            self.headphone = TwoFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert Lighting headphone to 2.5mm headphone.")

class LightingToUSBTypeCAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "Lighting":
            self.headphone = USBTypeCHeadphone()
        else:
            raise ValueError("This adapter can only convert Lighting headphone to USB Type-C headphone.")

类图如下:

class_diagram

对象模式实现耳机接口转换:

代码如下:

class Headphone:
    pass

class ThreeFiveMMHeadphone(Headphone):
    def __init__(self):
        self.type = "3.5mm"

class TwoFiveMMHeadphone(Headphone):
    def __init__(self):
        self.type = "2.5mm"

class USBTypeCHeadphone(Headphone):
    def __init__(self):
        self.type = "USB Type-C"

class LightingHeadphone(Headphone):
    def __init__(self):
        self.type = "Lighting"

class Adapter:
    def __init__(self, headphone):
        self.headphone = headphone

class ThreeFiveMMToTwoFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "3.5mm":
            self.headphone = TwoFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert 3.5mm headphone to 2.5mm headphone.")

class ThreeFiveMMToUSBTypeCAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "3.5mm":
            self.headphone = USBTypeCHeadphone()
        else:
            raise ValueError("This adapter can only convert 3.5mm headphone to USB Type-C headphone.")

class ThreeFiveMMToLightingAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "3.5mm":
            self.headphone = LightingHeadphone()
        else:
            raise ValueError("This adapter can only convert 3.5mm headphone to Lighting headphone.")

class TwoFiveMMToThreeFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "2.5mm":
            self.headphone = ThreeFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert 2.5mm headphone to 3.5mm headphone.")

class TwoFiveMMToUSBTypeCAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "2.5mm":
            self.headphone = USBTypeCHeadphone()
        else:
            raise ValueError("This adapter can only convert 2.5mm headphone to USB Type-C headphone.")

class TwoFiveMMToLightingAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "2.5mm":
            self.headphone = LightingHeadphone()
        else:
            raise ValueError("This adapter can only convert 2.5mm headphone to Lighting headphone.")

class USBTypeCToThreeFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "USB Type-C":
            self.headphone = ThreeFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert USB Type-C headphone to 3.5mm headphone.")

class USBTypeCToTwoFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "USB Type-C":
            self.headphone = TwoFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert USB Type-C headphone to 2.5mm headphone.")

class USBTypeCToLightingAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "USB Type-C":
            self.headphone = LightingHeadphone()
        else:
            raise ValueError("This adapter can only convert USB Type-C headphone to Lighting headphone.")

class LightingToThreeFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "Lighting":
            self.headphone = ThreeFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert Lighting headphone to 3.5mm headphone.")

class LightingToTwoFiveMMAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "Lighting":
            self.headphone = TwoFiveMMHeadphone()
        else:
            raise ValueError("This adapter can only convert Lighting headphone to 2.5mm headphone.")

class LightingToUSBTypeCAdapter(Adapter):
    def __init__(self, headphone):
        if headphone.type == "Lighting":
            self.headphone = USBTypeCHeadphone()
        else:
            raise ValueError("This adapter can only convert Lighting headphone to USB Type-C headphone.")

class HeadphoneAdapterFactory:
    def create_adapter(self, input_headphone, output_headphone):
        if input_headphone.type == "3.5mm":
            if output_headphone.type == "2.5mm":
                return ThreeFiveMMToTwoFiveMMAdapter(input_headphone)
            elif output_headphone.type == "USB Type-C":
                return ThreeFiveMMToUSBTypeCAdapter(input_headphone)
            elif output_headphone.type == "Lighting":
                return ThreeFiveMMToLightingAdapter(input_headphone)
        elif input_headphone.type == "2.5mm":
            if output_headphone.type == "3.5mm":
                return TwoFiveMMToThreeFiveMMAdapter(input_headphone)
            elif output_headphone.type == "USB Type-C":
                return TwoFiveMMToUSBTypeCAdapter(input_headphone)
            elif output_headphone.type == "Lighting":
                return TwoFiveMMToLightingAdapter(input_headphone)
        elif input_headphone.type == "USB Type-C":
            if output_headphone.type == "3.5mm":
                return USBTypeCToThreeFiveMMAdapter(input_headphone)
            elif output_headphone.type == "2.5mm":
                return USBTypeCToTwoFiveMMAdapter(input_headphone)
            elif output_headphone.type == "Lighting":
                return USBTypeCToLightingAdapter(input_headphone)
        elif input_headphone.type == "Lighting":
            if output_headphone.type == "3.5mm":
                return LightingToThreeFiveMMAdapter(input_headphone)
            elif output_headphone.type == "2.5mm":
                return LightingToTwoFiveMMAdapter(input_headphone)
            elif output_headphone.type == "USB Type-C":
                return LightingToUSBTypeCAdapter(input_headphone)

类图如下:

![object_diagram](https://i.imgur.com/r6CCNlf.png


原文地址: https://www.cveoy.top/t/topic/hji7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录