温州市住房建设局网站,代做网站跳转,济源网站建设电话,WordPress会员密码查看2023-05-09 设计模式#xff08;单例#xff0c;工厂#xff09;
单例模式
顾名思义#xff0c;就是整个系统对外提供的实例有且只有一个
特点#xff1a;
1、单例类只有一个实例
2、必须是自己创建唯一实例
3、必须给所以对象提供这个实例
分类#xff…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();}
}