工厂模式与多态结合

工厂模式与多态的结合是平台化项目中实现灵活架构的核心技术之一。这种组合能够创建可扩展、易维护的系统架构。

多态(Polymorphism)指同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果。

例子1:

java 复制代码
public abstract class PaymentProcessor {
    public abstract void process(Payment payment);
}

public class CreditCardProcessor extends PaymentProcessor {
    public void process(Payment payment) { /* 信用卡处理逻辑 */ }
}

public class PayPalProcessor extends PaymentProcessor {
    public void process(Payment payment) { /* PayPal处理逻辑 */ }
}

public class PaymentProcessorFactory {
    public static PaymentProcessor create(String type) {
        switch(type) {
            case "creditcard": return new CreditCardProcessor();
            case "paypal": return new PayPalProcessor();
            default: throw new IllegalArgumentException();
        }
    }
}

// 使用
PaymentProcessor processor = PaymentProcessorFactory.create("creditcard");
processor.process(payment);

例子2:

Kotlin 复制代码
abstract class IPhoneLinkAdapter(val context: Context) {
    abstract fun getPhoneLinkType(): Int
    abstract fun getPhoneLinkName(): String
}



abstract class ICarPlayLinkAdapter(context: Context) : IPhoneLinkAdapter(context) {
    override fun getPhoneLinkType() = PhoneLinkType.CARPLAY
}

abstract class IAndroidAutoLinkAdapter(context: Context) : IPhoneLinkAdapter(context) {
    override fun getPhoneLinkType() = PhoneLinkType.ANDROID_AUTO
    override fun getPhoneLinkName() = context.getString(R.string.androidauto)
}

class ACarPlayLinkAdapter(context: Context) : ICarPlayLinkAdapter(context) {
    override fun getPhoneLinkName() = context.getString(R.string.carplay_a)
}

class BCarPlayLinkAdapter(context: Context) : ICarPlayLinkAdapter(context) {
    override fun getPhoneLinkName() = context.getString(R.string.carplay_b)
}

class AAndroidAutoLinkAdapter(context: Context) : IAndroidAutoLinkAdapter(context) {
}

class BAndroidAutoLinkAdapter(context: Context) : IAndroidAutoLinkAdapter(context) {
}



object PhoneLinkAdapterFactory {
    fun getPhoneLinkAdapters(context: Context): HashSet<IPhoneLinkAdapter> {
        val linkAdapters = HashSet<IPhoneLinkAdapter>()

        if (条件1) {
            linkAdapters.add(ACarPlayLinkAdapter(context))
        } else if (条件2) {
            linkAdapters.add(BCarPlayLinkAdapter(context))
        } 

        if (条件3) {
            linkAdapters.add(AAndroidAutoLinkAdapter(context))
        } else if (条件4) {
            linkAdapters.add(BAndroidAutoLinkAdapter(context))
        } 

        return linkAdapters
    }
}




//使用
internal val linkAdapters by lazy { PhoneLinkAdapterFactory.getPhoneLinkAdapters(mContext) }

fun getPhoneLinkName(phoneLinkType: Int): String {
    return getPhoneLinkAdapter(phoneLinkType)?.getPhoneLinkName() ?: ""
}

internal fun getPhoneLinkAdapter(phoneLinkType: Int): IPhoneLinkAdapter? {
    return linkAdapters.find { it.getPhoneLinkType() == phoneLinkType }
}

fun isWirelessConnected(): Boolean {
    linkAdapters.forEach {
        if (it.isWirelessConnected()) {
            return true
        }
    }
    return false
}
相关推荐
Wang's Blog43 分钟前
Java 项目实战: 外卖平台优化-YApi接口管理平台与文档导入导出
java·开发语言·yapi
专业程序开发源1 小时前
django新闻推荐系统70655-计算机课程设计、毕业设计
java·javascript·spring boot·后端·python·django·课程设计
vx_Biye_Design1 小时前
springboot一站式旅游管理平台81037-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·课程设计·express·旅游
写后端的胖头鱼1 小时前
时间复杂度 & 空间复杂度
java·数据结构·算法·时间复杂度·空间复杂度
嵌入式学习菌1 小时前
ESP32 ModbusTCP 分片缓存
java·后端·spring
vx_Biye_Design1 小时前
springboot宠物寄养服务预约与监管系统82684-计算机课程设计、毕业设计
java·vue.js·spring boot·elasticsearch·课程设计·express·宠物
程序员小杰@1 小时前
Spring Boot 常用注解分类速记
java·spring boot·后端
vx_Biye_Design1 小时前
springboot小学生英语学习APP62773-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·学习·课程设计
吴声子夜歌1 小时前
Nginx应用与运维——Nginx日志管理
java·运维·nginx