zoukankan      html  css  js  c++  java
  • 抽象工厂模式

    抽象工厂模式:提供了创建一系列相关或互相依赖对象的接口,而无须指定它们具体的类。

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 抽象工厂模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                User user = new User();
                Department dept = new Department();
    
                //AbstractFactory factory = new SqlServerFactory();
                IFactory factory = new AccessFactory();
                IUser iu = factory.CreateUser();
    
                iu.Insert(user);
                iu.GetUser(1);
    
                IDepartment id = factory.CreateDepartment();
                id.Insert(dept);
                id.GetDepartment(1);
    
                Console.Read();
            }
        }
    
        class User
        {
            private int _id;
            public int ID
            {
                get { return _id; }
                set { _id = value; }
            }
    
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }
    
        class Department
        {
            private int _id;
            public int ID
            {
                get { return _id; }
                set { _id = value; }
            }
    
            private string _deptName;
            public string DeptName
            {
                get { return _deptName; }
                set { _deptName = value; }
            }
        }
    
        interface IUser
        {
            void Insert(User user);
    
            User GetUser(int id);
        }
    
        class SqlserverUser : IUser
        {
            public void Insert(User user)
            {
                Console.WriteLine("在Sqlserver中给User表增加一条记录");
            }
    
            public User GetUser(int id)
            {
                Console.WriteLine("在Sqlserver中根据ID得到User表一条记录");
                return null;
            }
        }
    
        class AccessUser : IUser
        {
            public void Insert(User user)
            {
                Console.WriteLine("在Access中给User表增加一条记录");
            }
    
            public User GetUser(int id)
            {
                Console.WriteLine("在Access中根据ID得到User表一条记录");
                return null;
            }
        }
    
        interface IDepartment
        {
            void Insert(Department department);
    
            Department GetDepartment(int id);
        }
    
        class SqlserverDepartment : IDepartment
        {
            public void Insert(Department department)
            {
                Console.WriteLine("在Sqlserver中给Department表增加一条记录");
            }
    
            public Department GetDepartment(int id)
            {
                Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录");
                return null;
            }
        }
    
        class AccessDepartment : IDepartment
        {
            public void Insert(Department department)
            {
                Console.WriteLine("在Access中给Department表增加一条记录");
            }
    
            public Department GetDepartment(int id)
            {
                Console.WriteLine("在Access中根据ID得到Department表一条记录");
                return null;
            }
        }
    
        interface IFactory
        {
            IUser CreateUser();
    
            IDepartment CreateDepartment();
        }
    
        class SqlServerFactory : IFactory
        {
            public IUser CreateUser()
            {
                return new SqlserverUser();
            }
    
            public IDepartment CreateDepartment()
            {
                return new SqlserverDepartment();
            }
        }
    
        class AccessFactory : IFactory
        {
            public IUser CreateUser()
            {
                return new AccessUser();
            }
    
            public IDepartment CreateDepartment()
            {
                return new AccessDepartment();
            }
        }
    }
  • 相关阅读:
    Yum安装MySQL
    Java最小化镜像制作
    Docker CE安装
    每月最后一周的周六晚上21:00执行任务-crontab
    每10秒执行定时任务-crontab
    可复制领导力-回顾收录
    逻辑数据库设计
    5e赋能核心文化
    python 学习自学
    德鲁克的“五项主要习惯”
  • 原文地址:https://www.cnblogs.com/hongdada/p/2992793.html
Copyright © 2011-2022 走看看