当前位置 : 主页 > 编程语言 > c语言 >

C#工厂模式案例

来源:互联网 收集:自由互联 发布时间:2021-06-25
class JianDanGongChang { static void Main( string [] args) { Factory factory = new LianXiangFactory(); DianNao dianNao = factory.Create(); dianNao.Print(); } } public abstract class DianNao { public abstract void Print(); } public class Lia
class JianDanGongChang
    {
        static void Main(string[] args)
        {
            Factory factory=new LianXiangFactory();
            DianNao dianNao=factory.Create();
            dianNao.Print();
        }
    }
    
    public abstract class DianNao
    {
        public abstract void Print();
    }
    
    public class LianXiang:DianNao
    {
        public override void Print()
        {
            Console.WriteLine("联想电脑");
        }
    }
    
    public class DaiEr:DianNao
    {
        public override void Print()
        {
            Console.WriteLine("戴尔电脑");
        }
    }
    
    public abstract class Factory
    {
        public abstract DianNao Create();
    }
    
    public class LianXiangFactory:Factory
    {
        public override DianNao Create()
        {
            return new LianXiang();
        }
    }
    
    public class DaiErFactory:Factory
    {
        public override DianNao Create()
        {
            return new DaiEr();
        }
    }
网友评论