原型模型
程序员文章站
2022-04-09 18:52:26
跟单例模型只创建一个对象相反,原型模型用于快速的创建相同的多个对象。 Main中调用: 输出结果: ......
跟单例模型只创建一个对象相反,原型模型用于快速的创建相同的多个对象。
public class prototype { private static prototype prototype = new prototype(); public string sdbconnection = ""; private prototype() { console.writeline("构造函数初始化!!"); } public static prototype createinstance(string sdb) { prototype _prototype = (prototype)prototype.memberwiseclone(); _prototype.sdbconnection = sdb; return _prototype; } }
main中调用:
class program { static void main(string[] args) { prototype prototype = prototype.createinstance("server=1;database=a"); prototype prototype1 = prototype.createinstance("server=2;database=b"); prototype prototype2 = prototype.createinstance("server=3;database=c"); prototype prototype3 = prototype.createinstance("server=4;database=d"); console.writeline(prototype.sdbconnection); console.writeline(prototype1.sdbconnection); console.writeline(prototype2.sdbconnection); console.writeline(prototype3.sdbconnection); console.read(); } }
输出结果:
上一篇: 用Arcade表达式添加标签