溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java設計模式的抽象工廠模式怎么寫

發布時間:2022-01-12 22:26:19 來源:億速云 閱讀:172 作者:iii 欄目:編程語言

本文小編為大家詳細介紹“java設計模式的抽象工廠模式怎么寫”,內容詳細,步驟清晰,細節處理妥當,希望這篇“java設計模式的抽象工廠模式怎么寫”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

public class UserEntity
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

public interface IUser
    {
        void InsertUser(UserEntity user);
        UserEntity GetUser(int id);
    }
class SqlUser: IUser
    {
        public void InsertUser(UserEntity user)
        {
            Console.WriteLine("在SQL中插入一個用戶");
        }
        public UserEntity GetUser(int id)
        {
            Console.WriteLine("在SQL中獲取一個用戶");
            return null;
        }
    }

    class AccessUser : IUser
    {
        public void InsertUser(UserEntity user)
        {
            Console.WriteLine("在Access中插入一個用戶");
        }
        public UserEntity GetUser(int id)
        {
            Console.WriteLine("在Access中獲取一個用戶");
            return null;
        }
    }

    //抽象工廠改簡單工廠->反射
    public class SimpleFactory
    {
        private static string assemblyName = "抽象工廠";        
        private static string db = ConfigurationSettings.AppSettings["DB"];        
        public static IUser CreateUser()
        {
            //switch (db)
            //{
            //    case "sql":return new SqlUser();
            //    case "access": return new AccessUser();
            //}
            //return null;      
            string className = assemblyName + "." + db + "User";
            IUser iuser = (IUser)Assembly.Load(assemblyName).CreateInstance(className);
            return iuser;
        }

        public static IDepartment CreateIDepartment()
        {            
            string className = assemblyName + "." + db + "Department";
            IDepartment department = (IDepartment)Assembly.Load(assemblyName).CreateInstance(className);
            return department;
        }
    }
        <appSettings>
    <add key="DB" value="Product.Sql"/>
  </appSettings>

    static void Main(string[] args)
        {
            //UserEntity user = new UserEntity();
            //IFactory factory = new AccessFactory();
            //IUser su = factory.CreateUser();
            //su.InsertUser(user);
            //su.GetUser(1);

            //DepartmentEntity department = new DepartmentEntity();
            //IDepartment de = factory.CreateDepartment();
            //de.InsertDepartment(department);
            //de.GetDepartment(1);
            //Console.ReadLine();

            UserEntity user = new UserEntity();
            DepartmentEntity department = new DepartmentEntity();
            IUser su = SimpleFactory.CreateUser();
            su.InsertUser(user);
            su.GetUser(1);
            IDepartment dep = SimpleFactory.CreateIDepartment();
            dep.InsertDepartment(department);
            dep.GetDepartment(1);
            Console.ReadLine();

        }

讀到這里,這篇“java設計模式的抽象工廠模式怎么寫”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女