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


    两种模式有点分不清,在相同的场景下分别使用两种模式编写一下代码来让大家伙具体看看,到底是差在哪里。
    首先 先说结论,举个例子:
    工厂方法模式相当于是生产线的小作坊,比如有四个小作坊,一个产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();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107

    在这个基础上在写一下抽象工厂模式的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();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    然后再写一下工厂方法模式的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();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

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

  • 相关阅读:
    数字媒体技术基础之:常见图片文件格式
    Java 并发编程解析 | 关于Java领域中的线程机制,我们应该知道的那些事?
    LeetCode0912.排序数组 Go语言AC笔记
    【Rust日报】2023-10-14 Rust101: 使用光线跟踪渲染Cornell box
    ERP系统选型需谨慎
    Nuxt3框架全局引用外部JS/CSS文件的相关配置方法
    操作系统之《PV操作》【知识点+详细解题过程】
    【吴恩达机器学习-笔记整理】异常检测与高斯分布
    【代码随想录】LC 27. 移除元素
    前端框架的发展史可以追溯到早期的静态网页时代
  • 原文地址:https://blog.csdn.net/u012549951/article/details/126178774