抽象工厂模式-C#实现

该实例基于WPF实现,直接上代码,下面为三层架构的代码。

一 Model

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    public abstract class AbstructFactory
    {
        public abstract Comptuter GetComptuter(string name);
        public abstract Phone GetPhone(string name);
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    internal class AppleComptuter : Comptuter
    {
        public override string Name()
        {
            return "苹果电脑";
        }
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    internal class ApplePhone : Phone
    {
        public override string Name()
        {
            return "苹果手机";
        }
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    public abstract class Comptuter
    {
        public abstract string Name();
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    internal class ComptuterFactory : AbstructFactory
    {
        public override Comptuter GetComptuter(string name)
        {
            Comptuter comptuter = null;
            switch (name)
            {
                case "Apple":
                    comptuter = new AppleComptuter();
                    break;
                case "Dell":
                    comptuter = new DellComptuter();
                    break;
                case "Lesson":
                    comptuter = new LessonComptuter();
                    break;
            }

            return comptuter;
        }

        public override Phone GetPhone(string name)
        {
            Phone phone = null;
            switch (name)
            {
                case "Apple":
                    phone = new ApplePhone();
                    break;
                case "ReMi":
                    phone = new ReMiPhone();
                    break;
                case "HauWei":
                    phone = new HauWeiPhone();
                    break;
            }

            return phone;
        }
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    internal class DellComptuter : Comptuter
    {
        public override string Name()
        {
            return "戴尔电脑";
        }
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    internal class HauWeiPhone : Phone
    {
        public override string Name()
        {
            return "华为手机";
        }
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    internal class LessonComptuter : Comptuter
    {
        public override string Name()
        {
            return "联想电脑";
        }
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    public abstract class Phone
    {
        public abstract string Name();
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    internal class PhoneFactory : AbstructFactory
    {
        public override Comptuter GetComptuter(string name)
        {
            Comptuter comptuter = null;
            switch (name)
            {
                case "Apple":
                    comptuter = new AppleComptuter();
                    break;
                case "Dell":
                    comptuter = new DellComptuter();
                    break;
                case "Lesson":
                    comptuter = new LessonComptuter();
                    break;
            }

            return comptuter;
        }

        public override Phone GetPhone(string name)
        {
            Phone phone = null;
            switch (name)
            {
                case "Apple":
                    phone = new ApplePhone();
                    break;
                case "ReMi":
                    phone = new ReMiPhone();
                    break;
                case "HauWei":
                    phone = new HauWeiPhone();
                    break;
            }

            return phone;
        }
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.抽象工厂模式
{
    internal class ReMiPhone : Phone
    {
        public override string Name()
        {
            return "小米手机";
        }
    }
}

二 View

XML 复制代码
<Window x:Class="设计模式练习.View.抽象工厂模式.AbstructFactory"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:设计模式练习.View.抽象工厂模式"
        mc:Ignorable="d"
        Title="AbstructFactory" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="8*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0">
            <TextBlock Text="{Binding Ph1}"/>
            <TextBlock Text="{Binding Ph2}"/>
            <TextBlock Text="{Binding Ph3}"/>
            <TextBlock Text="{Binding Cp1}"/>
            <TextBlock Text="{Binding Cp2}"/>
            <TextBlock Text="{Binding Cp3}"/>
        </StackPanel>

        <Button Content="生产随机对象" Grid.Column="1" Command="{Binding fctCommand}"/>
    </Grid>
</Window>

三 ViewModel

cs 复制代码
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 设计模式练习.Model.抽象工厂模式;

namespace 设计模式练习.ViewModel.抽象工厂模式
{
    partial class AbstructFactory_ViewModel : ObservableObject
    {
        [ObservableProperty]
        private string ph1;

        [ObservableProperty]
        private string ph2;

        [ObservableProperty]
        private string ph3;

        [ObservableProperty]
        private string cp1;

        [ObservableProperty]
        private string cp2;

        [ObservableProperty]
        private string cp3;

        [RelayCommand]
        private void fct()
        {
            AbstructFactory a = new ComptuterFactory();
            AbstructFactory b = new PhoneFactory();

            Cp1 = a.GetComptuter("Dell").Name();
            Cp2 = a.GetComptuter("Apple").Name();
            Cp3 = a.GetComptuter("Lesson").Name();

            Ph1 = b.GetPhone("Apple").Name();
            Ph2 = b.GetPhone("ReMi").Name();
            Ph3 = b.GetPhone("HauWei").Name();
        }
    }
}
相关推荐
摘星编程2 天前
抽象工厂模式深度解析:从原理到与应用实战
设计模式·抽象工厂模式·源码分析·软件架构·实战案例
QQ_hoverer9 天前
抽象工厂模式与策略模式结合使用小案例
抽象工厂模式·策略模式
码农秋9 天前
设计模式系列(06):抽象工厂模式(Abstract Factory)
设计模式·抽象工厂模式
{⌐■_■}11 天前
【设计模式】简单工厂模式,工厂模式,抽象工厂模式,单例,代理,go案例区分总结
开发语言·redis·后端·设计模式·golang·简单工厂模式·抽象工厂模式
二次程序员16 天前
ECharts图表工厂,完整代码+思路逻辑
前端·javascript·css·echarts·抽象工厂模式·大屏端
wu~97021 天前
手撕四种常用设计模式(工厂,策略,代理,单例)
java·单例模式·设计模式·代理模式·抽象工厂模式·策略模式
不当菜虚困1 个月前
JAVA设计模式——(十)抽象工厂模式(Abstract Factory Pattern)
java·设计模式·抽象工厂模式
Cuit小唐1 个月前
C++ 抽象工厂模式详解
抽象工厂模式
帝锦_li1 个月前
Java进阶--设计模式
观察者模式·单例模式·代理模式·抽象工厂模式·适配器模式·原型模式
Java致死1 个月前
工厂设计模式
java·设计模式·简单工厂模式·工厂方法模式·抽象工厂模式