惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
O
OpenAI News
美团技术团队
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
Cyberwarzone
Cyberwarzone
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
T
Tenable Blog
S
Security Affairs
博客园_首页
S
Schneier on Security
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
Spread Privacy
Spread Privacy
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
Vercel News
Vercel News
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
B
Blog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 最新话题
D
DataBreaches.Net
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - Franky
W
WeLiveSecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Check Point Blog
H
Hacker News: Front Page

博客园 - 青玄鸟

.NET 中优雅处理 Server-Sent Events 请求取消 vue3.0 + ts 实现上传工厂(oss与cos) Dapr 订阅者参数无法正确反序列化问题 .NET 代码整洁手册 Blazor项目通过docker和nginx部署为静态站点的步骤 Moq mock 方法返回null空指针异常 基于接口隔离原则的依赖注入实现 HttpClient with Stream HttpClient partial update HttpClient 基本使用 值对象的封装 只读集合类型属性实现 适配器模式 模板模式 最少知识原则 单例模式 简单工厂、工厂方法、抽象工厂 工厂方法模式 使用 Visual Studio Code创建和执行T-SQL
抽象工厂
青玄鸟 · 2020-03-30 · via 博客园 - 青玄鸟

定义

提供一个接口,用于创建相关或者依赖对象的家族,而不需要明确指定具体类。

UML类图

实现

案例:提供一个创建手机的应用,根据不同的配件类型,生产不同类型的手机(安卓/iPhone)

定义接口

抽象工厂

    public interface MobileFactory
    {
        // 创建CPU
        ACPU CreateCPU();
        // 创建主板
        AMainboard CreateMainboard();
        // 创建外壳
        AShell CreateShell();
    }

CPU

    public abstract class ACPU
    {
        protected string version;

        protected string name;

        public string Version { get => version; }

        public string Name { get => name; }
    }

主板

    public abstract class AMainboard
    {
        protected string name;

        public string Name { get => name;}

    }

外壳

    public abstract class AShell
    {
        protected double screenSize;
        protected string color;
        public double ScreenSize { get=> screenSize; }
        public string Color { get => color; }
    }

具体工厂类

安卓工厂

    public class AndroidFactory : MobileFactory
    {
        public ACPU CreateCPU()
        {
            return new AndroidCPU();
        }

        public AMainboard CreateMainboard()
        {
            return new AndroidMainBoard();
        }

        public AShell CreateShell()
        {
            return new AndroidShell();
        }
    }

IOS工厂

   public class IOSFactory : MobileFactory
    {
        public ACPU CreateCPU()
        {
            return new IOSCPU();
        }

        public AMainboard CreateMainboard()
        {
            return new IOSMainboard();
        }

        public AShell CreateShell()
        {
            return new IOSShell();
        }
    } 

具体零件类(Member)

安卓

    public class AndroidCPU:ACPU
    {
        public AndroidCPU()
        {
            this.name = "Qualcomm";
            this.version = "860";
        }
    }
    public class AndroidMainBoard:AMainboard
    {
        public AndroidMainBoard()
        {
            name = "mainboard for android";
        }
    }
    public class AndroidShell:AShell
    {
        public AndroidShell()
        {
            screenSize = 6.0;
            color = "black";
        }
    }

IOS

    public class IOSCPU:ACPU
    {
        public IOSCPU()
        {
            name = "A";
            version = "13";
        }
    }
    public class IOSMainboard:AMainboard
    {
        public IOSMainboard()
        {
            name = "mainboard for ios";
        }
    }
    public class IOSShell:AShell
    {
        public IOSShell()
        {
            screenSize = 4.7;
            color = "red";
        }
    }

Client

    public class Mobile
    {
        ACPU cpu;
        AMainboard mainboard;
        AShell shell;
        MobileFactory mobileFactory;
        public Mobile(MobileFactory mobileFactory)
        {
            this.mobileFactory = mobileFactory;
        }

        public void Create()
        {
            cpu = mobileFactory.CreateCPU();
            mainboard = mobileFactory.CreateMainboard();
            shell = mobileFactory.CreateShell();

            Console.WriteLine($"The mobile is created,with cpu:{cpu.Name+cpu.Version},mainboard:{mainboard.Name},outshell:{shell.ScreenSize},{shell.Color}");
        }
    }

测试

    class Program
    {
        static void Main(string[] args)
        {
            MobileFactory mobileFactory = new AndroidFactory();
            Mobile android = new Mobile(mobileFactory);
            android.Create();

            mobileFactory = new IOSFactory();
            Mobile iphone = new Mobile(mobileFactory);
            iphone.Create();

            Console.ReadLine();
        }
    }

运行结果

The mobile is created,with cpu:Qualcomm860,mainboard:mainboard for android,outshell:6,black
The mobile is created,with cpu:A13,mainboard:mainboard for ios,outshell:4.7,red