首页 > 程序设计模式(C#代码)----工厂模式
头像
牛客836252573号
发布于 2021-01-03 01:10
+ 关注

程序设计模式(C#代码)----工厂模式

工厂模式是设计模式之一,属于创建型模式,通过手机工厂的对象可以创建华为手机和Vivo手机的对象

 

直接上代码

 

先写一个手机的接口,里面有一个生产手机的方法

1 interface Phone 2  { 3 public void createPhone(); 4 }

Huawei手机的类

复制代码
1 class HuaWei : Phone 2  { 3 public void createPhone() 4  { 5 Console.WriteLine("制造华为手机"); 6  } 7 }
复制代码

VIVO手机的类

复制代码
class Vivo : Phone
    { public void createPhone()
        {
            Console.WriteLine("制造Vivo手机");
        }
    }
复制代码

手机工厂

复制代码
class PhoneFactory
    { public const int HUAWEI= 0, VIVO = 1; public Phone getPhone(int phoneType)
        { switch (phoneType)
            { case HUAWEI: return new HuaWei(); break; case VIVO: return new Vivo(); break;
            } return null;
        }
    }
复制代码

实现

复制代码
 1 class Program  2  {  3 static void Main(string[] args)  4  {  5 PhoneFactory phoneFactory = new PhoneFactory();  6 Phone huawei=phoneFactory.getPhone(PhoneFactory.HUAWEI);  7  huawei.createPhone();  8  9 Phone vivo = phoneFactory.getPhone(PhoneFactory.VIVO); 10  vivo.createPhone(); 11  } 12 }
复制代码

 输出结果如下

 
https://github.com/threebb1/utvwcwdvks/discussions/1
https://www.github.com/threebb1/utvwcwdvks/discussions/1
http://github.com/threebb1/utvwcwdvks/discussions/1
https://github.com/threebb1/utvwcwdvks/discussions/2
https://www.github.com/threebb1/utvwcwdvks/discussions/2
http://github.com/threebb1/utvwcwdvks/discussions/2
https://github.com/threebb1/utvwcwdvks/discussions/3
https://www.github.com/threebb1/utvwcwdvks/discussions/3
http://github.com/threebb1/utvwcwdvks/discussions/3
https://github.com/threebb1/utvwcwdvks/discussions/4
https://www.github.com/threebb1/utvwcwdvks/discussions/4
http://github.com/threebb1/utvwcwdvks/discussions/4
https://github.com/threebb1/utvwcwdvks/discussions/5
https://www.github.com/threebb1/utvwcwdvks/discussions/5
http://github.com/threebb1/utvwcwdvks/discussions/5
https://github.com/threebb1/utvwcwdvks/discussions/6
https://www.github.com/threebb1/utvwcwdvks/discussions/6
http://github.com/threebb1/utvwcwdvks/discussions/6
https://github.com/threebb1/utvwcwdvks/discussions/7
https://www.github.com/threebb1/utvwcwdvks/discussions/7
http://github.com/threebb1/utvwcwdvks/discussions/7
https://github.com/threebb1/utvwcwdvks/discussions/8
https://www.github.com/threebb1/utvwcwdvks/discussions/8
http://github.com/threebb1/utvwcwdvks/discussions/8
https://github.com/threebb1/utvwcwdvks/discussions/9
https://www.github.com/threebb1/utvwcwdvks/discussions/9
http://github.com/threebb1/utvwcwdvks/discussions/9
https://github.com/threebb1/utvwcwdvks/discussions/10
https://www.github.com/threebb1/utvwcwdvks/discussions/10
http://github.com/threebb1/utvwcwdvks/discussions/10
https://github.com/threebb1/utvwcwdvks/discussions/11
https://www.github.com/threebb1/utvwcwdvks/discussions/11
http://github.com/threebb1/utvwcwdvks/discussions/11
https://github.com/threebb1/utvwcwdvks/discussions/12
https://www.github.com/threebb1/utvwcwdvks/discussions/12
http://github.com/threebb1/utvwcwdvks/discussions/12
https://github.com/threebb1/utvwcwdvks/discussions/13
https://www.github.com/threebb1/utvwcwdvks/discussions/13
http://github.com/threebb1/utvwcwdvks/discussions/13
https://github.com/threebb1/utvwcwdvks/discussions/14
https://www.github.com/threebb1/utvwcwdvks/discussions/14
http://github.com/threebb1/utvwcwdvks/discussions/14
https://github.com/threebb1/utvwcwdvks/discussions/15
https://www.github.com/threebb1/utvwcwdvks/discussions/15
http://github.com/threebb1/utvwcwdvks/discussions/15
https://github.com/threebb1/utvwcwdvks/discussions/16
https://www.github.com/threebb1/utvwcwdvks/discussions/16
http://github.com/threebb1/utvwcwdvks/discussions/16
https://github.com/threebb1/utvwcwdvks/discussions/17
https://www.github.com/threebb1/utvwcwdvks/discussions/17
http://github.com/threebb1/utvwcwdvks/discussions/17
https://github.com/threebb1/utvwcwdvks/discussions/18
https://www.github.com/threebb1/utvwcwdvks/discussions/18
http://github.com/threebb1/utvwcwdvks/discussions/18
https://github.com/threebb1/utvwcwdvks/discussions/19
https://www.github.com/threebb1/utvwcwdvks/discussions/19
http://github.com/threebb1/utvwcwdvks/discussions/19
https://github.com/threebb1/utvwcwdvks/discussions/20
https://www.github.com/threebb1/utvwcwdvks/discussions/20
http://github.com/threebb1/utvwcwdvks/discussions/20
https://github.com/threebb1/utvwcwdvks/discussions/21
https://www.github.com/threebb1/utvwcwdvks/discussions/21
http://github.com/threebb1/utvwcwdvks/discussions/21
https://github.com/threebb1/utvwcwdvks/discussions/22
https://www.github.com/threebb1/utvwcwdvks/discussions/22
http://github.com/threebb1/utvwcwdvks/discussions/22
https://github.com/threebb1/utvwcwdvks/discussions/23
https://www.github.com/threebb1/utvwcwdvks/discussions/23
http://github.com/threebb1/utvwcwdvks/discussions/23
https://github.com/threebb1/utvwcwdvks/discussions/24
https://www.github.com/threebb1/utvwcwdvks/discussions/24
http://github.com/threebb1/utvwcwdvks/discussions/24
https://github.com/threebb1/utvwcwdvks/discussions/25
https://www.github.com/threebb1/utvwcwdvks/discussions/25
http://github.com/threebb1/utvwcwdvks/discussions/25
https://github.com/threebb1/utvwcwdvks/discussions/26
https://www.github.com/threebb1/utvwcwdvks/discussions/26
http://github.com/threebb1/utvwcwdvks/discussions/26
https://github.com/threebb1/utvwcwdvks/discussions/27
https://www.github.com/threebb1/utvwcwdvks/discussions/27
http://github.com/threebb1/utvwcwdvks/discussions/27
https://github.com/threebb1/utvwcwdvks/discussions/28
https://www.github.com/threebb1/utvwcwdvks/discussions/28
http://github.com/threebb1/utvwcwdvks/discussions/28
https://github.com/threebb1/utvwcwdvks/discussions/29
https://www.github.com/threebb1/utvwcwdvks/discussions/29
http://github.com/threebb1/utvwcwdvks/discussions/29
https://github.com/threebb1/utvwcwdvks/discussions/30
https://www.github.com/threebb1/utvwcwdvks/discussions/30
http://github.com/threebb1/utvwcwdvks/discussions/30
https://github.com/threebb1/utvwcwdvks/discussions/31
https://www.github.com/threebb1/utvwcwdvks/discussions/31
http://github.com/threebb1/utvwcwdvks/discussions/31
https://github.com/threebb1/utvwcwdvks/discussions/32
https://www.github.com/threebb1/utvwcwdvks/discussions/32
http://github.com/threebb1/utvwcwdvks/discussions/32
https://github.com/threebb1/utvwcwdvks/discussions/33
https://www.github.com/threebb1/utvwcwdvks/discussions/33
http://github.com/threebb1/utvwcwdvks/discussions/33
https://github.com/threebb1/utvwcwdvks/discussions/34
https://www.github.com/threebb1/utvwcwdvks/discussions/34
http://github.com/threebb1/utvwcwdvks/discussions/34
https://github.com/threebb1/utvwcwdvks/discussions/35
https://www.github.com/threebb1/utvwcwdvks/discussions/35
http://github.com/threebb1/utvwcwdvks/discussions/35
https://github.com/threebb1/utvwcwdvks/discussions/36
https://www.github.com/threebb1/utvwcwdvks/discussions/36
http://github.com/threebb1/utvwcwdvks/discussions/36
https://github.com/threebb1/utvwcwdvks/discussions/37
https://www.github.com/threebb1/utvwcwdvks/discussions/37
http://github.com/threebb1/utvwcwdvks/discussions/37
https://github.com/threebb1/utvwcwdvks/discussions/38
https://www.github.com/threebb1/utvwcwdvks/discussions/38
http://github.com/threebb1/utvwcwdvks/discussions/38
https://github.com/threebb1/utvwcwdvks/discussions/39
https://www.github.com/threebb1/utvwcwdvks/discussions/39
http://github.com/threebb1/utvwcwdvks/discussions/39
https://github.com/threebb1/utvwcwdvks/discussions/40
https://www.github.com/threebb1/utvwcwdvks/discussions/40
http://github.com/threebb1/utvwcwdvks/discussions/40
https://github.com/threebb1/utvwcwdvks/discussions/41
https://www.github.com/threebb1/utvwcwdvks/discussions/41
http://github.com/threebb1/utvwcwdvks/discussions/41
https://github.com/threebb1/utvwcwdvks/discussions/42
https://www.github.com/threebb1/utvwcwdvks/discussions/42
http://github.com/threebb1/utvwcwdvks/discussions/42
https://github.com/threebb1/utvwcwdvks/discussions/43
https://www.github.com/threebb1/utvwcwdvks/discussions/43
http://github.com/threebb1/utvwcwdvks/discussions/43
https://github.com/threebb1/utvwcwdvks/discussions/44
https://www.github.com/threebb1/utvwcwdvks/discussions/44
http://github.com/threebb1/utvwcwdvks/discussions/44
https://github.com/threebb1/utvwcwdvks/discussions/45
https://www.github.com/threebb1/utvwcwdvks/discussions/45
http://github.com/threebb1/utvwcwdvks/discussions/45
https://github.com/threebb1/utvwcwdvks/discussions/46
https://www.github.com/threebb1/utvwcwdvks/discussions/46
http://github.com/threebb1/utvwcwdvks/discussions/46
https://github.com/threebb1/utvwcwdvks/discussions/47
https://www.github.com/threebb1/utvwcwdvks/discussions/47
http://github.com/threebb1/utvwcwdvks/discussions/47
https://github.com/threebb1/utvwcwdvks/discussions/48
https://www.github.com/threebb1/utvwcwdvks/discussions/48
http://github.com/threebb1/utvwcwdvks/discussions/48
https://github.com/threebb1/utvwcwdvks/discussions/49
https://www.github.com/threebb1/utvwcwdvks/discussions/49
http://github.com/threebb1/utvwcwdvks/discussions/49
https://github.com/threebb1/utvwcwdvks/discussions/50
https://www.github.com/threebb1/utvwcwdvks/discussions/50
http://github.com/threebb1/utvwcwdvks/discussions/50
https://github.com/threebb1/utvwcwdvks/discussions/51
https://www.github.com/threebb1/utvwcwdvks/discussions/51
http://github.com/threebb1/utvwcwdvks/discussions/51
https://github.com/threebb1/utvwcwdvks/discussions/52
https://www.github.com/threebb1/utvwcwdvks/discussions/52
http://github.com/threebb1/utvwcwdvks/discussions/52
https://github.com/threebb1/utvwcwdvks/discussions/53
https://www.github.com/threebb1/utvwcwdvks/discussions/53
http://github.com/threebb1/utvwcwdvks/discussions/53
https://github.com/threebb1/utvwcwdvks/discussions/54
https://www.github.com/threebb1/utvwcwdvks/discussions/54
http://github.com/threebb1/utvwcwdvks/discussions/54
https://github.com/threebb1/utvwcwdvks/discussions/55
https://www.github.com/threebb1/utvwcwdvks/discussions/55
http://github.com/threebb1/utvwcwdvks/discussions/55
https://github.com/threebb1/utvwcwdvks/discussions/56
https://www.github.com/threebb1/utvwcwdvks/discussions/56
http://github.com/threebb1/utvwcwdvks/discussions/56
https://github.com/threebb1/utvwcwdvks/discussions/57
https://www.github.com/threebb1/utvwcwdvks/discussions/57
http://github.com/threebb1/utvwcwdvks/discussions/57
https://github.com/threebb1/utvwcwdvks/discussions/58
https://www.github.com/threebb1/utvwcwdvks/discussions/58
http://github.com/threebb1/utvwcwdvks/discussions/58
https://github.com/threebb1/utvwcwdvks/discussions/59
https://www.github.com/threebb1/utvwcwdvks/discussions/59
http://github.com/threebb1/utvwcwdvks/discussions/59
https://github.com/threebb1/utvwcwdvks/discussions/60
https://www.github.com/threebb1/utvwcwdvks/discussions/60
http://github.com/threebb1/utvwcwdvks/discussions/60
https://github.com/threebb1/utvwcwdvks/discussions/61
https://www.github.com/threebb1/utvwcwdvks/discussions/61
http://github.com/threebb1/utvwcwdvks/discussions/61
https://github.com/threebb1/utvwcwdvks/discussions/62
https://www.github.com/threebb1/utvwcwdvks/discussions/62
http://github.com/threebb1/utvwcwdvks/discussions/62
https://github.com/threebb1/utvwcwdvks/discussions/63
https://www.github.com/threebb1/utvwcwdvks/discussions/63
http://github.com/threebb1/utvwcwdvks/discussions/63
https://github.com/threebb1/utvwcwdvks/discussions/64
https://www.github.com/threebb1/utvwcwdvks/discussions/64
http://github.com/threebb1/utvwcwdvks/discussions/64
https://github.com/threebb1/utvwcwdvks/discussions/65
https://www.github.com/threebb1/utvwcwdvks/discussions/65
http://github.com/threebb1/utvwcwdvks/discussions/65
https://github.com/threebb1/utvwcwdvks/discussions/66
https://www.github.com/threebb1/utvwcwdvks/discussions/66
http://github.com/threebb1/utvwcwdvks/discussions/66
https://github.com/threebb1/utvwcwdvks/discussions/67
https://www.github.com/threebb1/utvwcwdvks/discussions/67
http://github.com/threebb1/utvwcwdvks/discussions/67
https://github.com/threebb1/utvwcwdvks/discussions/68
https://www.github.com/threebb1/utvwcwdvks/discussions/68
http://github.com/threebb1/utvwcwdvks/discussions/68
https://github.com/threebb1/utvwcwdvks/discussions/69
https://www.github.com/threebb1/utvwcwdvks/discussions/69
http://github.com/threebb1/utvwcwdvks/discussions/69
https://github.com/threebb1/utvwcwdvks/discussions/70
https://www.github.com/threebb1/utvwcwdvks/discussions/70
http://github.com/threebb1/utvwcwdvks/discussions/70
https://github.com/threebb1/utvwcwdvks/discussions/71
https://www.github.com/threebb1/utvwcwdvks/discussions/71
http://github.com/threebb1/utvwcwdvks/discussions/71
https://github.com/threebb1/utvwcwdvks/discussions/72
https://www.github.com/threebb1/utvwcwdvks/discussions/72
http://github.com/threebb1/utvwcwdvks/discussions/72
https://github.com/threebb1/utvwcwdvks/discussions/73
https://www.github.com/threebb1/utvwcwdvks/discussions/73
http://github.com/threebb1/utvwcwdvks/discussions/73
https://github.com/threebb1/utvwcwdvks/discussions/74
https://www.github.com/threebb1/utvwcwdvks/discussions/74
http://github.com/threebb1/utvwcwdvks/discussions/74
https://github.com/threebb1/utvwcwdvks/discussions/75
https://www.github.com/threebb1/utvwcwdvks/discussions/75
http://github.com/threebb1/utvwcwdvks/discussions/75
https://github.com/threebb1/utvwcwdvks/discussions/76
https://www.github.com/threebb1/utvwcwdvks/discussions/76
http://github.com/threebb1/utvwcwdvks/discussions/76
https://github.com/threebb1/utvwcwdvks/discussions/77
https://www.github.com/threebb1/utvwcwdvks/discussions/77
http://github.com/threebb1/utvwcwdvks/discussions/77
https://github.com/threebb1/utvwcwdvks/discussions/78
https://www.github.com/threebb1/utvwcwdvks/discussions/78
http://github.com/threebb1/utvwcwdvks/discussions/78
https://github.com/threebb1/utvwcwdvks/discussions/79
https://www.github.com/threebb1/utvwcwdvks/discussions/79
http://github.com/threebb1/utvwcwdvks/discussions/79
https://github.com/threebb1/utvwcwdvks/discussions/80
https://www.github.com/threebb1/utvwcwdvks/discussions/80
http://github.com/threebb1/utvwcwdvks/discussions/80
https://github.com/threebb1/utvwcwdvks/discussions/81
https://www.github.com/threebb1/utvwcwdvks/discussions/81
http://github.com/threebb1/utvwcwdvks/discussions/81
https://github.com/threebb1/utvwcwdvks/discussions/82
https://www.github.com/threebb1/utvwcwdvks/discussions/82
http://github.com/threebb1/utvwcwdvks/discussions/82
https://github.com/threebb1/utvwcwdvks/discussions/83
https://www.github.com/threebb1/utvwcwdvks/discussions/83
http://github.com/threebb1/utvwcwdvks/discussions/83
https://github.com/threebb1/utvwcwdvks/discussions/84
https://www.github.com/threebb1/utvwcwdvks/discussions/84
http://github.com/threebb1/utvwcwdvks/discussions/84
https://github.com/threebb1/utvwcwdvks/discussions/85
https://www.github.com/threebb1/utvwcwdvks/discussions/85
http://github.com/threebb1/utvwcwdvks/discussions/85
https://github.com/threebb1/utvwcwdvks/discussions/86
https://www.github.com/threebb1/utvwcwdvks/discussions/86
http://github.com/threebb1/utvwcwdvks/discussions/86
https://github.com/threebb1/utvwcwdvks/discussions/87
https://www.github.com/threebb1/utvwcwdvks/discussions/87
http://github.com/threebb1/utvwcwdvks/discussions/87
https://github.com/threebb1/utvwcwdvks/discussions/88
https://www.github.com/threebb1/utvwcwdvks/discussions/88
http://github.com/threebb1/utvwcwdvks/discussions/88
https://github.com/threebb1/utvwcwdvks/discussions/89
https://www.github.com/threebb1/utvwcwdvks/discussions/89
http://github.com/threebb1/utvwcwdvks/discussions/89
https://github.com/threebb1/utvwcwdvks/discussions/90
https://www.github.com/threebb1/utvwcwdvks/discussions/90
http://github.com/threebb1/utvwcwdvks/discussions/90
https://github.com/threebb1/utvwcwdvks/discussions/91
https://www.github.com/threebb1/utvwcwdvks/discussions/91
http://github.com/threebb1/utvwcwdvks/discussions/91
https://github.com/threebb1/utvwcwdvks/discussions/92
https://www.github.com/threebb1/utvwcwdvks/discussions/92
http://github.com/threebb1/utvwcwdvks/discussions/92
https://github.com/threebb1/utvwcwdvks/discussions/93
https://www.github.com/threebb1/utvwcwdvks/discussions/93
http://github.com/threebb1/utvwcwdvks/discussions/93
https://github.com/threebb1/utvwcwdvks/discussions/94
https://www.github.com/threebb1/utvwcwdvks/discussions/94
http://github.com/threebb1/utvwcwdvks/discussions/94
https://github.com/threebb1/utvwcwdvks/discussions/95
https://www.github.com/threebb1/utvwcwdvks/discussions/95
http://github.com/threebb1/utvwcwdvks/discussions/95
https://github.com/threebb1/utvwcwdvks/discussions/96
https://www.github.com/threebb1/utvwcwdvks/discussions/96
http://github.com/threebb1/utvwcwdvks/discussions/96
https://github.com/threebb1/utvwcwdvks/discussions/97
https://www.github.com/threebb1/utvwcwdvks/discussions/97
http://github.com/threebb1/utvwcwdvks/discussions/97
https://github.com/threebb1/utvwcwdvks/discussions/98
https://www.github.com/threebb1/utvwcwdvks/discussions/98
http://github.com/threebb1/utvwcwdvks/discussions/98
https://github.com/threebb1/utvwcwdvks/discussions/99
https://www.github.com/threebb1/utvwcwdvks/discussions/99
http://github.com/threebb1/utvwcwdvks/discussions/99
https://github.com/threebb1/utvwcwdvks/discussions/100
https://www.github.com/threebb1/utvwcwdvks/discussions/100
http://github.com/threebb1/utvwcwdvks/discussions/100
https://github.com/threebb1/utvwcwdvks/discussions/101
https://www.github.com/threebb1/utvwcwdvks/discussions/101
http://github.com/threebb1/utvwcwdvks/discussions/101
https://github.com/threebb1/utvwcwdvks/discussions/102
https://www.github.com/threebb1/utvwcwdvks/discussions/102
http://github.com/threebb1/utvwcwdvks/discussions/102
https://github.com/threebb1/utvwcwdvks/discussions/103
https://www.github.com/threebb1/utvwcwdvks/discussions/103
http://github.com/threebb1/utvwcwdvks/discussions/103
https://github.com/threebb1/utvwcwdvks/discussions/104
https://www.github.com/threebb1/utvwcwdvks/discussions/104
http://github.com/threebb1/utvwcwdvks/discussions/104
https://github.com/threebb1/utvwcwdvks/discussions/105
https://www.github.com/threebb1/utvwcwdvks/discussions/105
http://github.com/threebb1/utvwcwdvks/discussions/105
https://github.com/threebb1/utvwcwdvks/discussions/106
https://www.github.com/threebb1/utvwcwdvks/discussions/106
http://github.com/threebb1/utvwcwdvks/discussions/106
https://github.com/threebb1/utvwcwdvks/discussions/107
https://www.github.com/threebb1/utvwcwdvks/discussions/107
http://github.com/threebb1/utvwcwdvks/discussions/107
https://github.com/threebb1/utvwcwdvks/discussions/108
https://www.github.com/threebb1/utvwcwdvks/discussions/108
http://github.com/threebb1/utvwcwdvks/discussions/108
https://github.com/threebb1/utvwcwdvks/discussions/109
https://www.github.com/threebb1/utvwcwdvks/discussions/109
http://github.com/threebb1/utvwcwdvks/discussions/109
https://github.com/threebb1/utvwcwdvks/discussions/110
https://www.github.com/threebb1/utvwcwdvks/discussions/110
http://github.com/threebb1/utvwcwdvks/discussions/110
https://github.com/threebb1/utvwcwdvks/discussions/111
https://www.github.com/threebb1/utvwcwdvks/discussions/111
http://github.com/threebb1/utvwcwdvks/discussions/111
https://github.com/threebb1/utvwcwdvks/discussions/112
https://www.github.com/threebb1/utvwcwdvks/discussions/112
http://github.com/threebb1/utvwcwdvks/discussions/112
https://github.com/threebb1/utvwcwdvks/discussions/113
https://www.github.com/threebb1/utvwcwdvks/discussions/113
http://github.com/threebb1/utvwcwdvks/discussions/113
https://github.com/threebb1/utvwcwdvks/discussions/114
https://www.github.com/threebb1/utvwcwdvks/discussions/114
http://github.com/threebb1/utvwcwdvks/discussions/114
https://github.com/threebb1/utvwcwdvks/discussions/115
https://www.github.com/threebb1/utvwcwdvks/discussions/115
http://github.com/threebb1/utvwcwdvks/discussions/115
https://github.com/threebb1/utvwcwdvks/discussions/116
https://www.github.com/threebb1/utvwcwdvks/discussions/116

全部评论

(0) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期精华帖

热门推荐