抽象工厂模式与工厂方法模式代码结构的区别

两种模式有点分不清,在相同的场景下分别使用两种模式编写一下代码来让大家伙具体看看,到底是差在哪里。
首先 先说结论,举个例子:
工厂方法模式相当于是生产线的小作坊,比如有四个小作坊,一个产A手机、一个产A电脑,一个产B手机、一个产B电脑;四个小作坊各干各的,你需要电脑就去找A电脑生产线或者B电脑生产线。
抽象工厂模式相当于来了2个厂长,把个小作坊给收购了,A厂子里面有手机和电脑两条生产线,B厂子里也有两条对应的生产线,那我需要电脑就只需要找这个厂长给我生产就可以了。

下面这个场景两种模式分别写一下:
首先写一下实体接口类:
两个厂长(工厂方法模式不具备)以及四个产品线

#region 产品接口
    public interface IComputer
    {
        void Open();
        void Close();
    }

    public interface IPhone
    {
        void Call();
    }
    
    //工厂方法模式不具备这个抽象类
    public abstract class Product
    {
        protected IComputer Computer { get; set; }
        protected IPhone Phone { get; set; }

        public abstract IComputer CreateComputer();

        public abstract IPhone CreatePhone();
    }
    #endregion


    #region 具体实现类
	//工厂方法模式不具备这个A厂子类
    public class AProduct : Product
    {
       public override IComputer CreateComputer()
        {
            if (Computer==null)
            {
                Computer = new AComputer();
            }
            return Computer;
        }
		
        public override IPhone CreatePhone()
        {
            if (Phone==null)
            {
                Phone = new APhone();
            }
            return Phone;
        }
    }

	//工厂方法模式不具备这个B厂子类
    public class BProduct : Product
    {
        public override IComputer CreateComputer()
        {
            throw new NotImplementedException();
        }

        public override IPhone CreatePhone()
        {
            throw new NotImplementedException();
        }
    }


	//A电脑生产线
    public class AComputer : IComputer
    {
        public void Open()
        {
            Console.WriteLine("");

        }

        public void Close()
        {
            throw new NotImplementedException();
        }
    }
	//A手机生产线
    public class APhone : IPhone
    {
        public void Call()
        {
            throw new NotImplementedException();
        }
    }

	//B电脑生产线
    public class BComputer : IComputer
    {
        public void Open()
        {
            throw new NotImplementedException();
        }

        public void Close()
        {
            throw new NotImplementedException();
        }
    }
	//B手机生产线
    public class BPhone : IPhone
    {
        public void Call()
        {
            throw new NotImplementedException();
        }
    }

在这个基础上在写一下抽象工厂模式的factory:
两个厂子,各自有两条生产线,需要手机的话就直接去找厂长要就可以了

public interface IFactory
    {
        //create两条产品线
        IComputer CreateComputerProduct();
        IPhone CreatePhoneProduct();
    }

	//A厂子,里面有两条生产线
    public class AMiFactory : IFactory
    {
        public IComputer CreateComputerProduct()
        {
            return new AProduct().CreateComputer();
        }

        public IPhone CreatePhoneProduct()
        {
            return new AProduct().CreatePhone();
        }
    }

	//B厂子,里面有两条生产线
    public class BFactory : IFactory
    {
        public IComputer CreateComputerProduct()
        {
            return new BProduct().CreateComputer();
        }

        public IPhone CreatePhoneProduct()
        {
            return new BProduct().CreatePhone();
        }
    }

然后再写一下工厂方法模式的factory:
手机电脑factory要分别写,下面对应了4个生产线factory,想要手机就需要去找具体的生产线

public interface IComputerFactory
    {
        IComputer CreateComputer();
    }
    public interface IPhoneFactory
    {
        IPhone CreatePhone();
    }


    public class AComputerFactory : IComputerFactory
    {
        public IComputer CreateComputer()
        {
            return new AComputer();
        }
    }

    public class APhoneFactory : IPhoneFactory
    {
        public IPhone CreatePhone()
        {
            return new APhone();
        }
    }

    public class BComputerFacotry : IComputerFactory
    {
        public IComputer CreateComputer()
        {
            return new BComputer();
        }
    }
    public class BPhoneFactory : IPhoneFactory
    {
        public IPhone CreatePhone()
        {
            return new BPhone();
        }
    }

以上。
另外csdn总是报推荐受影响,本来两个厂商用的是xiaomi和huawei,后来感觉是不是这个有广告嫌疑啊,又替换成了A和B厂商,最后还是不行,猜测难道是文字太少了?篇幅过短? 那我只好多复制几次了,希望csdn能够优化下,大家发文还是为了分享给大家一些知识,建议加一个人工提报审核。
防止篇幅过短,再复制一遍:另外csdn总是报推荐受影响,本来两个厂商用的是xiaomi和huawei,后来感觉是不是这个有广告嫌疑啊,又替换成了A和B厂商,最后还是不行,猜测难道是文字太少了?篇幅过短? 那我只好多复制几次了,希望csdn能够优化下,大家发文还是为了分享给大家一些知识,建议加一个人工提报审核。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值