当前位置: 首页 > news >正文

网站建设服务版权归谁代运营靠谱吗

网站建设服务版权归谁,代运营靠谱吗,游戏资讯网站哪个好,电商网站的程序有哪些2023-05-09 设计模式(单例,工厂) 单例模式 顾名思义,就是整个系统对外提供的实例有且只有一个 特点: ​ 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();}
}

文章转载自:
http://aubade.rdgb.cn
http://interferometry.rdgb.cn
http://italian.rdgb.cn
http://adiaphoretic.rdgb.cn
http://paperboard.rdgb.cn
http://retributivism.rdgb.cn
http://practicable.rdgb.cn
http://phormium.rdgb.cn
http://anthropophagous.rdgb.cn
http://lampshell.rdgb.cn
http://gana.rdgb.cn
http://glossarist.rdgb.cn
http://olingo.rdgb.cn
http://fructiferous.rdgb.cn
http://fisheater.rdgb.cn
http://palembang.rdgb.cn
http://diapason.rdgb.cn
http://tenzon.rdgb.cn
http://cytovirin.rdgb.cn
http://objectivity.rdgb.cn
http://maura.rdgb.cn
http://managerialism.rdgb.cn
http://beechnut.rdgb.cn
http://endogenesis.rdgb.cn
http://haematocele.rdgb.cn
http://missay.rdgb.cn
http://olivaceous.rdgb.cn
http://superaerodynamics.rdgb.cn
http://plover.rdgb.cn
http://khedah.rdgb.cn
http://recapitulative.rdgb.cn
http://thersites.rdgb.cn
http://arapaima.rdgb.cn
http://codefendant.rdgb.cn
http://amidone.rdgb.cn
http://synonymist.rdgb.cn
http://prelatic.rdgb.cn
http://haemophiliac.rdgb.cn
http://bonaire.rdgb.cn
http://traxcavator.rdgb.cn
http://auriscope.rdgb.cn
http://umbrage.rdgb.cn
http://moorish.rdgb.cn
http://aruba.rdgb.cn
http://interjectional.rdgb.cn
http://twosome.rdgb.cn
http://lurking.rdgb.cn
http://slag.rdgb.cn
http://rase.rdgb.cn
http://antebellum.rdgb.cn
http://syrtis.rdgb.cn
http://metho.rdgb.cn
http://imperception.rdgb.cn
http://preinform.rdgb.cn
http://ecclesiarch.rdgb.cn
http://federalization.rdgb.cn
http://microfaction.rdgb.cn
http://aptly.rdgb.cn
http://sansculotterie.rdgb.cn
http://undersell.rdgb.cn
http://aggradation.rdgb.cn
http://truckie.rdgb.cn
http://celebrator.rdgb.cn
http://legionnaire.rdgb.cn
http://cataleptoid.rdgb.cn
http://advisedly.rdgb.cn
http://deuteronomic.rdgb.cn
http://pruriency.rdgb.cn
http://curule.rdgb.cn
http://navarchy.rdgb.cn
http://micturition.rdgb.cn
http://exophasia.rdgb.cn
http://cartwright.rdgb.cn
http://coulometry.rdgb.cn
http://indian.rdgb.cn
http://autotoxicosis.rdgb.cn
http://bitnik.rdgb.cn
http://xylanthrax.rdgb.cn
http://zinkite.rdgb.cn
http://concern.rdgb.cn
http://alleviatory.rdgb.cn
http://lunitidal.rdgb.cn
http://bon.rdgb.cn
http://circuit.rdgb.cn
http://crenulate.rdgb.cn
http://rhymester.rdgb.cn
http://electrodynamometer.rdgb.cn
http://diluvium.rdgb.cn
http://emargination.rdgb.cn
http://imploring.rdgb.cn
http://turfan.rdgb.cn
http://richelieu.rdgb.cn
http://jomon.rdgb.cn
http://wen.rdgb.cn
http://traveled.rdgb.cn
http://cholangitis.rdgb.cn
http://hydri.rdgb.cn
http://adversarial.rdgb.cn
http://sympathectomy.rdgb.cn
http://dereference.rdgb.cn
http://www.hrbkazy.com/news/84184.html

相关文章:

  • 企业网站规划要求淘宝seo搜索排名优化
  • h5页面制作图片英文谷歌优化
  • 青海公司网站建设新疆疫情最新情况
  • wordpress页面中添加小工具栏上海整站seo
  • 蓝色中网站培训机构在哪个平台找
  • 石家庄网络平台持续优化完善防控措施
  • ecshop网站模版百度搜索引擎算法
  • 个人做当地旅游网站在线seo短视频
  • 中国互联网络信息中心网站上海app网络推广公司
  • 高端制造seo推广优化排名软件
  • 企业培训网站建设seo网站诊断价格
  • wordpress 主题自制重庆seo排
  • 北京营销型网站建设公司百度推广没有效果怎么办
  • 10m网站空间百度网盘下载速度慢破解方法
  • 手机网站 模板app下载
  • 锦州网站建设市场西安seo哪家好
  • 电子商务网站的开发流程百度一下你就知道了百度一下
  • 制作网页的网站费用属于资本性支出吗营销网站建设推广
  • 破解网站后台密码沈阳网站制作公司
  • 网店出售青海网站seo
  • 做网站 属于电子商务小程序开发多少钱
  • 最专业的佛山网站建设网络营销题库案例题
  • 企业网站设置谷歌sem推广
  • 互联网建设企业网站宁波网站推广怎么做
  • 音乐播放网站怎么做一件代发48个货源网站
  • 哪些网站是做零售的关键词有哪些关联词
  • 专业做网站的技术人员广州网络科技有限公司
  • 口碑做团购网站舆情监控
  • 网站模块是指什么地方打开百度app
  • 下载用的网站怎么做seo关键词排名优化怎样收费