1.单例模式
顾名思义,使一个类最多仅有一个实例化的对象的模式。单例模式根据对象实例化的早晚,分为懒汉式和饿汉式两种。
懒汉式,不调用它,就不实例化对象,懒。。。
/**
* 懒汉式
* */
class Singleton{
private static Singleton mInstance;
private Singleton(){}
public static Singleton getInstance(){
if(null==mInstance){
mInstance=new Singleton();
}
return mInstance;
}
}
饿汉式,因为饥饿嘛,在定义对象的时候就迫不及待的实例化了。
/**
* 饿汉式
* */
class HungrySingleton{
private static HungrySingleton mInstance=new HungrySingleton();
private HungrySingleton(){}
public static HungrySingleton getInstance(){
return mInstance;
}
}
在多线程的情况下,懒汉式需要通过双重锁定来防止重复实例化。
第一个null==mInstance比较好理解,当未实例化时才加同步判断,并实例化。
第二个null==mInstance,是为了防止多线程同时调用时,都执行到同步锁外等待,结果一个线程已经实例化了,其他线程又进行实例化。
/**
* 多线程情况下双重锁定的懒汉式
* */
class SingletonMultiThread{
private static SingletonMultiThread mInstance;
private static byte[] mLock=new byte[0];
private SingletonMultiThread(){}
public static SingletonMultiThread getInstance(){
if(null==mInstance){
synchronized(mLock){
if(null==mInstance){
mInstance=new SingletonMultiThread();
}
}
}
return mInstance;
}
}
记得以前看过一篇Oracle的人写的文章,分析了说双重锁定机制其实也不靠谱。。。
所以。。。还是直接用饿汉式吧,就没有这些烦恼了。。。
2.桥接模式
桥接模式的名字貌似是因为UML图看起来像一座桥。。。
其实桥接模式是指:当系统的实现有多种角度的分类时,尽量的将不同的分类分离出来,是它们独立的变化,降低耦合。
桥接模式充分利用了组合聚合复用原则,即:尽可能的多使用组合聚合,而非继承,只有在具有较强的"is a"的关系时,才考虑继承。
一个较差的,多用继承的设计。
abstract class CellPhoneBrand{
public abstract void run();
}
class CellPhoneBrandH extends CellPhoneBrand{
public void run(){
System.out.println("HTC");
}
}
class CellPhoneBrandS extends CellPhoneBrand{
public void run(){
System.out.println("SAMSUNG");
}
}
class CellPhoneBrandHGame extends CellPhoneBrandH{
public void run(){
super.run();
System.out.println("Play game");
}
}
class CellPhoneBrandSGame extends CellPhoneBrandS{
public void run(){
super.run();
System.out.println("Play game");
}
}
class CellPhoneBrandHMusic extends CellPhoneBrandH{
public void run(){
super.run();
System.out.println("Play music");
}
}
class CellPhoneBrandSMusic extends CellPhoneBrandS{
public void run(){
super.run();
System.out.println("Play music");
}
}
public class BridgePattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
CellPhoneBrand c;
c=new CellPhoneBrandHGame();
c.run();
c=new CellPhoneBrandSGame();
c.run();
c=new CellPhoneBrandHMusic();
c.run();
c=new CellPhoneBrandSMusic();
c.run();
}
}
使用了桥接模式改进
abstract class CellPhoneSoftware{
public abstract void run();
}
class CellPhoneGame extends CellPhoneSoftware{
public void run(){
System.out.println("Play game");
}
}
class CellPhoneMusic extends CellPhoneSoftware{
public void run(){
System.out.println("Play music");
}
}
abstract class CellPhoneBrand{
protected CellPhoneSoftware mCellPhoneSoftware;
public void setSoftware(CellPhoneSoftware cellPhoneSoftware){
mCellPhoneSoftware=cellPhoneSoftware;
}
public abstract void run();
}
class HTC extends CellPhoneBrand{
public void run(){
System.out.println("HTC");
if(null!=mCellPhoneSoftware)
mCellPhoneSoftware.run();
}
}
class SAMSUNG extends CellPhoneBrand{
public void run(){
System.out.println("SAMSUNG");
if(null!=mCellPhoneSoftware)
mCellPhoneSoftware.run();
}
}
public class BridgePatternBetter {
public static void main(String[] args) {
// TODO Auto-generated method stub
CellPhoneBrand c;
c=new HTC();
c.setSoftware(new CellPhoneMusic());
c.run();
c=new SAMSUNG();
c.setSoftware(new CellPhoneGame());
c.run();
}
}