首页 技术 正文
技术 2022年11月23日
0 收藏 571 点赞 4,071 浏览 920 个字

一,工厂模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _2._1工厂模式
{
class Program
{
static void Main(string[] args)
{
IPeopleFactory a = new ChinePeople();
Console.Write("中国人人穿了" + a.ShowShoes("红色") + "," + a.ShowClothes("红色"));
IPeopleFactory b = new USAPeople();
Console.Write("美国人穿了" + b.ShowShoes("白色") + "," + b.ShowClothes("白色"));
}
}
public interface IPeopleFactory
{
string ShowShoes(string type);
string ShowClothes(string type);
}
public class ChinePeople : IPeopleFactory
{
public string ShowShoes(string type)
{
return type + "上衣";
}
public string ShowClothes(string type)
{
return type + "裤子";
}
}
public class USAPeople : IPeopleFactory
{
public string ShowShoes(string type)
{
return type + "上衣";
}
public string ShowClothes(string type)
{
return type + "裤子";
}
}
}

二,主要用于隔离类对象的使用者和具体类型之间的耦合关系和实现多态等

与抽象工厂模式的区别在抽象工厂模式中详解

三,有时候我们有种疑惑,为什么我们不使用工厂模式,而使用现在的IOC呢?

其实本质上还是因为IOC是通过反射机制来实现的。当我们的需求出现变动时,工厂模式会需要进行相应的变化。但是IOC的反射机制允许我们不重新编译代码,因为它的对象都是动态生成的。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,907
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,432
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727