110,531
社区成员
发帖
与我相关
我的任务
分享
public class TReader // 负责从控制台上读取一串字符
{
public string Read()
{
Console.WriteLine("Please input a str:");
return Console.ReadLine();
}
}
public class TPrinter // 将字符串打印在控制台上。
{
public void Print(string str)
{
Console.WriteLine("inputed:" + str);
}
}
public class TReader
{
public static TReader Instance
{
get
{
if (_Instance == null)
{
_Instance = new TReader();
}
return _Instance;
}
}
private static TReader _Instance;
public string Read()
{
Console.WriteLine("Please input a str:");
return Console.ReadLine();
}
}
public class TPrinter
{
public static TPrinter Instance
{
get
{
if (_Instance == null)
{
_Instance = new TPrinter();
}
return _Instance;
}
}
private static TPrinter _Instance;
public void Print(string str)
{
Console.WriteLine("inputed:" + str);
}
}
public class G_<T> where T : class,new()
{
public static T Instance
{
get
{
if (_Instance == null)
{
_Instance = new T();
}
return _Instance;
}
set
{
_Instance = value;
}
}
private static T _Instance;
}
public class TReader // 负责从控制台上读取一串字符
{
public string Read()
{
Console.WriteLine("Please input a str:");
return Console.ReadLine();
}
}
public class TPrinter // 将字符串打印在控制台上。
{
public void Print(string str)
{
Console.WriteLine("inputed:" + str);
}
}
class Program // 主程序
{
static void Main(string[] args)
{
string str = G_<TReader>.Instance.Read(); //从控制台上读取字符串
G_<TPrinter>.Instance.Print(str); // 将字符串打印在控制台上
Console.ReadLine();
}
}
public class SingletonGenerator<T>
{
private object locker;
private bool initialized = false;
private Func<T> func;
private T instance;
public SingletonGenerator(Func<T> funcParam)
{
initialized = false;
func = funcParam;
locker = new object();
}
public T Value
{
get
{
if (!this.initialized)
{
lock (this.locker)
{
if (!this.initialized)
{
this.instance = this.func();
this.initialized = true;
}
}
}
return this.instance;
}
}
}
public class G2_<T> where T:class,new()
{
...
}
....
void AppInit()
{
G_<ClassName>.Instance = new ClassName( param1,param2);
}
....
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
G_Form_<Form2>.Instance.Show();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
public class G_Form_<T> where T : Form, new()
{
public static T Instance
{
get
{
if (_Instance == null)
{
_Instance = new T();
_Instance.FormClosed += new FormClosedEventHandler(_Instance_FormClosed);
}
return _Instance;
}
protected set
{
_Instance = value;
}
}
static void _Instance_FormClosed(object sender, FormClosedEventArgs e)
{
_Instance = null;
}
private static T _Instance;
}