设计模式之抽象工厂模式(C#实现)

示例

抽象工厂模式是对工厂进一步抽象,定义PCFactory抽象类,DELLFactory和HPFactory作为其子类,子类由具体的厂商实现,每个厂商不仅可以生产鼠标,还可以生产键盘,相比于工厂方法模式无需增加过多的factory

实现


    /// <summary>
    /// 对工厂进一步抽象,成为PCFactory,子类由具体的厂商实现,每个厂商不仅可以生产鼠标,还可以生产键盘,相比于工厂方法模式无需增加过多的factory
    /// </summary>
    abstract class PCFactory
    {
        internal abstract Mouse CreateMouse();
        internal abstract Mouse CreateKeyBoard();
    }

    class HPFactory : PCFactory
    {
        internal override Mouse CreateKeyBoard()
        {
            throw new NotImplementedException();
        }

        internal override Mouse CreateMouse()
        {
            return new HPMouse();
        }
    }

    class DELLFactory : PCFactory
    {
        internal override Mouse CreateKeyBoard()
        {
            throw new NotImplementedException();
        }

        internal override Mouse CreateMouse()
        {
            return new DellMouse();
        }
    }
//抽象工厂模式
new HPFactory().CreateMouse().DoubleClick();
new DELLFactory().CreateMouse().DoubleClick();

输出

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值