武汉p2p网站建设公司南京企业网站排名优化
2023-05-09 设计模式(单例,工厂)
单例模式
顾名思义,就是整个系统对外提供的实例有且只有一个
特点:
1、单例类只有一个实例
2、必须是自己创建唯一实例
3、必须给所以对象提供这个实例
分类:一般分为饿汉式单例(直接实例化)和懒汉式单例(使用时才实例化)
饿汉式单例
public class SingletonTest {//构造器私有化,防止外部调用private SingletonTest() {}//直接实例化private static SingletonTest sin = new SingletonTest();public static SingletonTest getInstance(){return sin;}
}
懒汉式单例
同步锁(当然还有简单的,去掉synchronized,只是存在安全问题)
public class SingletonTest1 {//构造器私有化,防止外部调用private SingletonTest1() {}private static SingletonTest1 sin = null;//使用同步锁保证一定会创建,并且只会创建一次public static synchronized SingletonTest1 getInstance(){if(sin == null){sin = new SingletonTest1();}return sin;}
}
双重检测锁
public class SingletonTest2 {//构造器私有化,防止外部调用private SingletonTest2() {}//使用volatile,在这里的作用是禁止指令重排//sin = new SingletonTest2();不是一个原子操作//第一步、申请内存,第二步、初始化,第三步、地址引用给sin//如果第三步在第二步前面就执行了,此时另一个现场通过getInstance获取实例时进来判断就不为null了,直接返回,而这个时候其实还并没有实例化//synchronized只能保证代码块相较于外面是正常顺序,代码块内部还是可能由于指令优化导致第三步在第二步前面就执行了private static volatile SingletonTest2 sin = null;//为什么要判断两次呢//第一次是为了减少同步,不用每次进来都去加锁//第二次是如果现在有两个现场同时竞争锁,那必然有一个是处于阻塞状态的//当处于阻塞状态的线程拿到锁后,前面的线程肯定是已经创建过实例了//如果这个时候不判断一下的话就会有实例化一次public static SingletonTest2 getInstance(){if(sin == null){synchronized (SingletonTest2.class){if(sin == null){sin = new SingletonTest2();}}}return sin;}
}
内部类
public class SingletonTest3 {//构造器私有化,防止外部调用private SingletonTest3() {}private static SingletonTest3 sin = null;public static SingletonTest3 getInstance(){return SingletonClass.singleton;}//内部类只有在使用的时候才会初始化,其中初始化是jvm控制的,单线程操作,保证了线程安全,也实现了懒汉式private static class SingletonClass{private static SingletonTest3 singleton = new SingletonTest3();}
}
工厂模式
工厂模式主要是为了屏蔽创建对象的过程,主要分为三类(静态工厂,普通工厂,抽象工厂)
静态工厂(简单工厂)
public interface FactoryTest {void doSome();
}public class FactoryTest1 implements FactoryTest {@Overridepublic void doSome() {System.out.println("FactoryTest1");}
}public class FactoryTest2 implements FactoryTest {@Overridepublic void doSome() {System.out.println("FactoryTest2");}
}public class StaticFactory {public static FactoryTest creatTest(String name){if("FactoryTest1".equals(name)){return new FactoryTest1();}else if("FactoryTest2".equals(name)){return new FactoryTest2();}return null;}
}public class Test1 {public static void main(String[] args) {//通过StaticFactory工厂创建对应的实例类FactoryTest factoryTest1 = StaticFactory.creatTest("FactoryTest1");factoryTest1.doSome();FactoryTest factoryTest2 = StaticFactory.creatTest("FactoryTest2");factoryTest2.doSome();}
}
每次新增一个FactoryTest时,需要修改StaticFactory.creatTest()方法,这样不合理,所以有了普通工厂模式
普通工厂
public interface FactoryTest {void doSome();
}public class FactoryTest1 implements FactoryTest {@Overridepublic void doSome() {System.out.println("FactoryTest1");}
}public class FactoryTest2 implements FactoryTest {@Overridepublic void doSome() {System.out.println("FactoryTest2");}
}public interface ICommonFactory {FactoryTest createTest();
}public class CommonFactory1 implements ICommonFactory {@Overridepublic FactoryTest createTest() {return new FactoryTest1();}
}public class CommonFactory2 implements ICommonFactory {@Overridepublic FactoryTest createTest() {return new FactoryTest2();}
}public class Test1 {public static void main(String[] args) {//通过CommonFactory1工厂创建对应的实例类new CommonFactory1().createTest().doSome();new CommonFactory2().createTest().doSome();}
}
每次新增一个FactoryTest时,只需要新增一个对应的CommonFactory工厂实现类就行,对于之前的工厂和对象不影响
抽象工厂
现在有手机和平板,可以使用上述的普通工厂来创建,但现在给手机和平板分了牌子,有华为手机和苹果手机,华为平板和苹果平板,这样一个产品族的概念(分为手机和平板两个产品族,手机产品族里面包含了华为手机和苹果手机两个产品),那么就可以使用抽象工厂模式。
//手机产品族
public interface Phone {void doSome();
}
public class PhoneHuawei implements Phone {@Overridepublic void doSome() {System.out.println("华为手机");}
}
public class PhonePinguo implements Phone {@Overridepublic void doSome() {System.out.println("苹果手机");}
}//平板产品族
public interface Tablet {void doSome();
}
public class TabletHuawei implements Tablet {@Overridepublic void doSome() {System.out.println("华为平板");}
}
public class TabletPinguo implements Tablet {@Overridepublic void doSome() {System.out.println("苹果平板");}
}//工厂
public interface AbsFactory {Phone buyPhone();Tablet buyTablet();
}
//华为工厂
public class HuaweiFatory implements AbsFactory{@Overridepublic Phone buyPhone() {return new PhoneHuawei();}@Overridepublic Tablet buyTablet() {return new TabletHuawei();}
}
//苹果工厂
public class PinguoFatory implements AbsFactory{@Overridepublic Phone buyPhone() {return new PhonePinguo();}@Overridepublic Tablet buyTablet() {return new TabletPinguo();}
}public class Test1 {public static void main(String[] args) {HuaweiFatory huaweiFatory = new HuaweiFatory();huaweiFatory.buyPhone().doSome();huaweiFatory.buyTablet().doSome();PinguoFatory pinguoFatory = new PinguoFatory();pinguoFatory.buyPhone().doSome();pinguoFatory.buyTablet().doSome();}
}