概述
Singleton Pattern
1. Lazy Singleton Pattern
(1) thread unsafe codes
Java:
class Lazy
{
private static Lazy lazy=null;
private Lazy(){}
public static synchronized Lazy getLazy()
{
if(lazy==null)
lazy=new Lazy();
return lazy;
}
public void mm()
{
System.out.println("MM");
}
}
class Test
{
public static void main(String[] ss)
{
Lazy l1=Lazy.getLazy();
l1.mm();
Lazy l2=Lazy.getLazy();
l2.mm();
System.out.println(l1==l2);
}
}
C#:
using System;
class Singleton2
{
private volatile static Singleton2 s1;
private Singleton2(){}
private static readonly object lockThis = new object();
public static Singleton2 getS1()
{
lock(lockThis)
{
if(s1==null)
s1=new Singleton2();
}
return s1;
}
public void mm()
{
Console.WriteLine("mmmmmmmmmmm2");
}
}
class Test
{
public static void Main(String[] ss)
{
Singleton2 s1=Singleton2.getS1();
s1.mm();
Singleton2 s2=Singleton2.getS1();
s2.mm();
Console.WriteLine(s1==s2);
}
}
C++:
#include<iostream>
using namespace std;
class Sington
{
private:
Sington(){}
public:
static Sington * s;
static Sington * getInstance()
{
if(s == NULL)
{
s = new Sington();
}
return s;
}
void doSomething()
{
cout<<"I'm doing sth....."<<endl;
}
};
Sington* Sington::s=0;
void main()
{
Sington * ss=Sington::getInstance();
ss->doSomething();
Sington * aa=Sington::getInstance();
aa->doSomething();
cout<<(ss==aa)<<endl;
}
(2)thread safe codes
1)synchronized method
Java:
class Lazy
{
private static Lazy lazy=null;
private Lazy(){}
public static synchronized Lazy getLazy()
{
if(lazy==null)
lazy=new Lazy();
return lazy;
}
public void mm()
{
System.out.println("MM");
}
}
class Test
{
public static void main(String[] ss)
{
Lazy l1=Lazy.getLazy();
l1.mm();
Lazy l2=Lazy.getLazy();
l2.mm();
System.out.println(l1==l2);
}
}
C#:
using System;
class Singleton2
{
private volatile static Singleton2 s1;
private Singleton2(){}
private static readonly object lockThis = new object();
public static Singleton2 getS1()
{
lock(lockThis)
{
if(s1==null)
s1=new Singleton2();
}
return s1;
}
public void mm()
{
Console.WriteLine("mmmmmmmmmmm2");
}
}
class Test
{
public static void Main(String[] ss)
{
Singleton2 s1=Singleton2.getS1();
s1.mm();
Singleton2 s2=Singleton2.getS1();
s2.mm();
Console.WriteLine(s1==s2);
}
}
2). Eager Singleton Pattern
Java:
class Eager
{
private static Eager eager=new Eager();
private Eager(){}
public static Eager getEager()
{
return eager;
}
public void mm()
{
System.out.println("MM");
}
}
class Test
{
public static void main(String[] ss)
{
Eager e1=Eager.getEager();
e1.mm();
Eager e2=Eager.getEager();
e2.mm();
System.out.println(e1==e2);
}
}
C#:
using System;
class Eager
{
private static Eager eager=new Eager();
private Eager(){}
public static Eager getEager()
{
return eager;
}
public void mm()
{
Console.WriteLine("MM");
}
}
class Test
{
public static void Main(String[] ss)
{
Eager e1=Eager.getEager();
e1.mm();
Eager e2=Eager.getEager();
e2.mm();
Console.WriteLine(e1==e2);
}
}
3). Double Check Singleton Pattern
Java:
class Singleton2
{
private volatile static Singleton2 s1;
private Singleton2(){}
public static Singleton2 getS1()
{
if(s1==null)
{
synchronized(Singleton2.class)
{
if(s1==null)
s1=new Singleton2();
}
}
return s1;
}
public void mm()
{
System.out.println("mmmmmmmmmmm2");
}
}
class Test
{
public static void main(String[] ss)
{
Singleton2 s1=Singleton2.getS1();
s1.mm();
Singleton2 s2=Singleton2.getS1();
s2.mm();
System.out.println(s1==s2);
}
}
C#:
using System;
class Lazy
{
private static Lazy lazy=null;
private Lazy(){}
private static readonly object lockThis = new object();
public static Lazy getLazy()
{
if(lazy==null)
{
lock(lockThis)
{
if(lazy==null)
lazy=new Lazy();
}
}
return lazy;
}
public void mm()
{
Console.WriteLine("MM");
}
}
class Test
{
public static void Main(String[] ss)
{
Lazy l1=Lazy.getLazy();
l1.mm();
Lazy l2=Lazy.getLazy();
l2.mm();
Console.WriteLine(l1==l2);
}
}
最后
以上就是辛勤夏天为你收集整理的Design Patterns的全部内容,希望文章能够帮你解决Design Patterns所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复