• 抽象工厂模式


    故事:

      以前有个定制鞋子的工厂,客户满意度很高,后来有的客户来的时候想定制套装(包括鞋子和衬衫)。

    建模:

      工厂前台接待处。

      工厂套装协调部门。

      生产线。

      鞋。

      很多喜欢个性和变化人的都成了这个定制工厂的客户。

    类图:

    实现:

    HelpDesk

    namespace AbstractFactory
    {
        class HelpDesk
        {
            SuitDepartment sd;
            
            public HelpDesk()
            {
                sd = new SuitDepartment();
                
            }
    
            public void produceSuit(string shoesType, string shirtType)
            {
                sd.produceShoes(shoesType);
                sd.produceShirt(shirtType);
            }
        }
    }

    AbstractFactory

    namespace AbstractFactory
    {
        public abstract class AbstractFactory
        {
            public abstract Shoes produceShoes(string shoesType);
            public abstract Shirt produceShirt(string shirtType);        
        }
    }

    SuitDepartment

    namespace AbstractFactory
    {
        class SuitDepartment:AbstractFactory
        {
            public override Shoes produceShoes(string shoesType)
            {
                switch(shoesType)
                {
                    case "Sport":
                        return new SportShoes();
                    case "Leisure":
                        return new LeisureShoes();
                    default:
                        return null;
                }
            }
            public override Shirt produceShirt(string shirtType)
            {
                switch(shirtType)
                {
                    case "Sport":
                        return new SportShirt();
                    case "Leisure":
                        return new LeisureShirt();
                    default:
                        return null;
                }
    
            }
        }
    }

    Program

    namespace AbstractFactory
    {
        class Program
        {
            static void Main(string[] args)
            {
                HelpDesk helpDesk = new HelpDesk();
    
                Console.WriteLine("请输入你想要的鞋子:Sport/Leisure");
                string shoesType = Console.ReadLine();
                Console.WriteLine("请输入你想要的衬衫:Sport/Leisure");
                string shirtType = Console.ReadLine();
    
                Console.WriteLine("\n------进厂定制套装---------\n");
                helpDesk.produceSuit(shoesType, shirtType);
                Console.WriteLine("\n-------套装出厂--------\n");
                
            }
        }
    }

    效果:

  • 相关阅读:
    Oracle建立表空间和用户
    fscanf()函数具体解释
    三层架构(我的理解及具体分析)
    ListView嵌套ListView优化
    Android xml 解析
    玩转Web之servlet(三)---一张图看懂B/S架构
    jquery.scrollTo-min.js
    C#中MessageBox使用方法大全(附效果图)
    hdu 1882 Strange Billboard(位运算+枚举)
    MySQL 通配符学习小结
  • 原文地址:https://www.cnblogs.com/jiejue/p/2711739.html
Copyright © 2020-2023  润新知