首页 > 其他 > 详细

设计模式 工厂

时间:2021-06-02 21:32:38      阅读:22      评论:0      收藏:0      [点我收藏+]

三种工厂模式:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             food a = gongchang.selectfood("cai1");
14             a.print();
15         }
16        
17     }
18     public interface food
19     {
20           void print();
21     }
22     public class  cai1:food
23     {
24         public  void print()
25         {
26             Console.WriteLine("hongshaoyu");
27         }
28     }
29     public class cai2 : food
30     {
31         public  void print()
32         {
33             Console.WriteLine("qiezi");
34         }
35     }
36     public class gongchang
37     {
38         public static food selectfood(string type)
39         {
40             if (type.Equals("cai1"))
41             {
42                 return new cai1();
43             }
44             else
45             {
46                 return new cai2();
47             }
48         }
49     }
50 }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//简单工厂总结:
//               1.创建具体事物的抽象类,创建不同的具体事物继承
//               2.创建抽象工厂类,创建不同的具体工厂继承
//               3.选择工厂的新实例。工厂选择具体事物的新实例
//               4.执行具体事物的方法。
//如果要扩展产品,要添加一个具体事物类和具体工厂类,缺点:单个工厂只创建单个产品
namespace 工厂方法模式
{
    class Program
    {

        static void Main(string[] args)
        {
            Factory a = new CreFactoryAodi();//大众工厂赋值给抽象工厂
            Factory d = new CreFactoryDazhong();//奥迪工厂赋值给抽象工厂
            car q = a.CreFactory();//执行奥迪工厂的方法,这个方法会选择new奥迪的对象,方法返回是奥迪具体事物类
            q.outcar();//执行奥迪具体事物类
            car w = d.CreFactory();//执行大众工厂的方法,这个方法会选择new大众的对象,方法返回是大众具体事物类
            w.outcar();//执行大众具体事物类
        }
    }
    public abstract class car//抽象汽车类
    {
        public abstract void outcar();
    }
    public class dazhong : car//具体大众子类
    {
        public override void outcar()
        {
            Console.WriteLine("生产一辆大众汽车");
        }
    }
    public class aodi : car//具体奥迪子类
    {
        public override void outcar()
        {
            Console.WriteLine("生产一辆奥迪汽车");
        }
    }
    public abstract class Factory//抽象工厂类
    {
        public abstract car CreFactory();
    }
    public class CreFactoryAodi: Factory//具体工厂类,选择aodi工厂
    {
        public override  car CreFactory()
        {
            return new aodi();
        }
    }
    public class CreFactoryDazhong : Factory//具体工厂类,选择dazhong工厂
    {
        public override car CreFactory()
        {
            return new dazhong();
        }
    }
}
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 抽象工厂
 8 {
 9     //工厂总结:
10     //               1.这里有抽象工厂,抽象产品1,抽象产品2
11     //               2.选择产地工厂,选择品牌,生产
12     //如果要扩展产地,要添加一个具体产地和产品1,产品2
13     class Program
14     {
15         static void Main(string[] args)
16         {
17             FA S = new shangqiFA();//得到具体工厂,选择工厂
18             dazhong ss= S.dazhongCar();//选择品牌
19             ss.outCar();//生产
20             aodi ss1 = S.aodiCar();
21             ss1.outCar();
22             FA Q = new yiqiFA();
23             dazhong QQ = Q.dazhongCar();
24             QQ.outCar();
25             aodi QQ1 = Q.aodiCar();
26             QQ1.outCar();
27 
28         }
29     }
30     public abstract class FA//抽象工厂
31     {
32         public abstract dazhong dazhongCar();
33         public abstract aodi aodiCar();
34     }
35     public abstract class dazhong//抽象大众
36     {
37         public abstract void outCar();
38     }
39     public abstract class aodi//抽象奥迪
40     {
41         public abstract void outCar();
42     }
43     public class shangqiDazhong : dazhong
44     {
45         public override void outCar()
46         {
47             Console.WriteLine("生产上海大众汽车");
48         }
49     }
50     public class yiqidazhong : dazhong
51     {
52         public override void outCar()
53         {
54             Console.WriteLine("生产一汽大众汽车");
55         }
56     }
57     public class yiqiaodi : aodi
58     {
59         public override void outCar()
60         {
61             Console.WriteLine("生产一汽奥迪汽车");
62         }
63     }
64     public class shangqiaodi : aodi
65     {
66         public override void outCar()
67         {
68             Console.WriteLine("生产上海奥迪汽车");
69         }
70     }
71 
72     public class shangqiFA : FA
73     {
74         public override dazhong dazhongCar()
75         {
76             return new shangqiDazhong();
77         }
78         public override aodi aodiCar()
79         {
80             return new shangqiaodi();
81         }
82 
83     }
84     public class yiqiFA : FA
85     {
86         public override dazhong dazhongCar()
87         {
88             return new yiqidazhong();
89         }
90         public override aodi aodiCar()
91         {
92             return new yiqiaodi();
93         }
94 
95     }
96 }

 

设计模式 工厂

原文:https://www.cnblogs.com/haoweiwei/p/14842410.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!