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

佛山建网站公司拼多多搜索关键词排名

佛山建网站公司,拼多多搜索关键词排名,深圳观澜网站建设,asp购物网站客户查看购物车一、介绍 采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法。 不使用单例模式的UML类图: 使用单例模式的UML类图: 使用场景: 需要频繁创建或销毁的对象…

一、介绍

采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法。

不使用单例模式的UML类图:

请添加图片描述

使用单例模式的UML类图:

请添加图片描述

使用场景:

  • 需要频繁创建或销毁的对象
  • 创建对象耗时过多或耗费资源过多,但又经常用到
  • 工具类对象
  • 频繁访问数据库或文件的对象

二、Java版实现

1. 饿汉式(静态常量)

//饿汉式(静态变量)
class Singleton {//1. 构造器私有化, 外部能newprivate Singleton() {}//2.本类内部创建对象实例private final static Singleton instance = new Singleton();//3. 提供一个公有的静态方法,返回实例对象public static Singleton getInstance() {return instance;}
}

优点:写法简单,在类装载的时候就完成了实例化,避免了线程同步问题。

缺点:在类加载的时候完成实例化,没有达到懒加载的效果,可能造成内存浪费。

2. 饿汉式(静态代码块)

//饿汉式(静态变量)
class Singleton {//1. 构造器私有化, 外部能newprivate Singleton() {}//2.本类内部创建对象实例private  static Singleton instance;static { // 在静态代码块中,创建单例对象instance = new Singleton();}//3. 提供一个公有的静态方法,返回实例对象public static Singleton getInstance() {return instance;}
}

优缺点同上。

3. 懒汉式(线程不安全)

class Singleton {private static Singleton instance;private Singleton() {}//提供一个静态的公有方法,当使用到该方法时,才去创建 instance//即懒汉式public static Singleton getInstance() {if(instance == null) {instance = new Singleton();}return instance;}
}

优缺点:起到了懒加载的效果,但只能在单线程下使用,多线程可能创建多个实例。

3. 懒汉式(线程安全,同步方法)

// 懒汉式(线程安全,同步方法)
class Singleton {private static Singleton instance;private Singleton() {}//提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题//即懒汉式public static synchronized Singleton getInstance() {if(instance == null) {instance = new Singleton();}return instance;}
}

优缺点:解决了线程安全问题,但效率太低,每个线程在想获得类的实例时候,都需要进行同步。

5. 双重检查

class Singleton {private static volatile Singleton instance;private Singleton() {}//提供一个静态的公有方法,加入双重检查代码,解决线程安全问题, 同时解决懒加载问题//同时保证了效率, 推荐使用public static synchronized Singleton getInstance() {if(instance == null) {synchronized (Singleton.class) {if(instance == null) {instance = new Singleton();}}}return instance;}
}

优缺点:实例代码只需要执行一次,后面再访问时,会被外层判空语句拦截,避免反复进行方法同步。延迟加载,效率较高。

6. 静态内部类

// 静态内部类完成, 推荐使用
class Singleton {private static volatile Singleton instance;//构造器私有化private Singleton() {}//写一个静态内部类,该类中有一个静态属性 Singletonprivate static class SingletonInstance {private static final Singleton INSTANCE = new Singleton(); }//提供一个静态的公有方法,直接返回SingletonInstance.INSTANCEpublic static synchronized Singleton getInstance() {return SingletonInstance.INSTANCE;}
}

静态内部类的特点:当外部类装载时,静态内部类不会立即实例化,而是在真正用到时才会实例化。并且静态内部类保证了线程的安全性。

7. 枚举

package com.atguigu.singleton.type8;public class SingletonTest08 {public static void main(String[] args) {Singleton instance = Singleton.INSTANCE;Singleton instance2 = Singleton.INSTANCE;System.out.println(instance == instance2);System.out.println(instance.hashCode());System.out.println(instance2.hashCode());instance.sayOK();}
}//使用枚举,可以实现单例, 推荐
enum Singleton {INSTANCE; //属性public void sayOK() {System.out.println("ok~");}
}

优缺点:不仅可以避免多线程同步问题,而且还能防止反序列化重新创建新的对象。

三、Golang版实现

1. 饿汉式

package mainimport "fmt"type Singleton struct {	Name string
}var SingletonInstance Singletonfunc init() {SingletonInstance = Singleton{"singleTonName"};
}func main() {fmt.Printf("SingletonInstance: %v", SingletonInstance)
}

通过init函数在初始化的时候加载单例类的实例。

2. 懒汉式

package mainimport ("fmt""sync"
)type Singleton struct {	Name string
}var (SingletonInstance SingletonSingletonOnce     sync.Once
)func GetInstance() Singleton {SingletonOnce.Do(func() {SingletonInstance = Singleton{"SingletonName"}})return SingletonInstance
}func main() {GetInstance()fmt.Printf("SingletonInstance: %v", SingletonInstance)
}

通过sync.Once实现在初次使用这个实例时才加载的效果。


文章转载自:
http://dinncoallowable.zfyr.cn
http://dinncospaghetti.zfyr.cn
http://dinncoyicker.zfyr.cn
http://dinncopachinko.zfyr.cn
http://dinncopunctilious.zfyr.cn
http://dinncolocksmithing.zfyr.cn
http://dinncoshrift.zfyr.cn
http://dinncosplake.zfyr.cn
http://dinncosidewalk.zfyr.cn
http://dinncooutclimb.zfyr.cn
http://dinncoflews.zfyr.cn
http://dinncogimbals.zfyr.cn
http://dinncoquist.zfyr.cn
http://dinncocaponize.zfyr.cn
http://dinnconcna.zfyr.cn
http://dinncoclepe.zfyr.cn
http://dinnconankeen.zfyr.cn
http://dinncointwine.zfyr.cn
http://dinncofructifier.zfyr.cn
http://dinncoinextricability.zfyr.cn
http://dinncoenucleate.zfyr.cn
http://dinncorsn.zfyr.cn
http://dinncodint.zfyr.cn
http://dinncoregretful.zfyr.cn
http://dinncostorage.zfyr.cn
http://dinncocholedochotomy.zfyr.cn
http://dinncodyslectic.zfyr.cn
http://dinncofloatability.zfyr.cn
http://dinncomanifestant.zfyr.cn
http://dinncoomentum.zfyr.cn
http://dinncoxenodochium.zfyr.cn
http://dinncobornite.zfyr.cn
http://dinncoprocaine.zfyr.cn
http://dinncoprolific.zfyr.cn
http://dinncomultiply.zfyr.cn
http://dinncobft.zfyr.cn
http://dinncoscca.zfyr.cn
http://dinncocollotype.zfyr.cn
http://dinncovaporing.zfyr.cn
http://dinncosatyromaniac.zfyr.cn
http://dinncoinappellability.zfyr.cn
http://dinncoloquacious.zfyr.cn
http://dinncocopyist.zfyr.cn
http://dinncomesmerist.zfyr.cn
http://dinncojaplish.zfyr.cn
http://dinncoapophthegmatic.zfyr.cn
http://dinncoobviate.zfyr.cn
http://dinncoconsentience.zfyr.cn
http://dinncotorun.zfyr.cn
http://dinncotransmigration.zfyr.cn
http://dinncohawaii.zfyr.cn
http://dinncostringent.zfyr.cn
http://dinncoamaretto.zfyr.cn
http://dinncomolybdenian.zfyr.cn
http://dinncoclassmate.zfyr.cn
http://dinncogalbanum.zfyr.cn
http://dinncoamblyoscope.zfyr.cn
http://dinncogranophyre.zfyr.cn
http://dinncoresemblance.zfyr.cn
http://dinncocockloft.zfyr.cn
http://dinncocharmingly.zfyr.cn
http://dinncomacropaedia.zfyr.cn
http://dinncocress.zfyr.cn
http://dinncovociferator.zfyr.cn
http://dinncodroplight.zfyr.cn
http://dinncohydrosulfide.zfyr.cn
http://dinncosuccinyl.zfyr.cn
http://dinncodemiseason.zfyr.cn
http://dinncosulfhydryl.zfyr.cn
http://dinncoimpotency.zfyr.cn
http://dinncodishy.zfyr.cn
http://dinncobattleship.zfyr.cn
http://dinncoshaken.zfyr.cn
http://dinncolightproof.zfyr.cn
http://dinncodistaste.zfyr.cn
http://dinncoelectronical.zfyr.cn
http://dinncodenote.zfyr.cn
http://dinncohegemonic.zfyr.cn
http://dinncoctrl.zfyr.cn
http://dinncohamstring.zfyr.cn
http://dinncoobadiah.zfyr.cn
http://dinncomew.zfyr.cn
http://dinnconary.zfyr.cn
http://dinncotunnel.zfyr.cn
http://dinncorouse.zfyr.cn
http://dinncohypotrophy.zfyr.cn
http://dinncocraniometrical.zfyr.cn
http://dinncoovercorrect.zfyr.cn
http://dinncogob.zfyr.cn
http://dinncooutgroup.zfyr.cn
http://dinncoanglice.zfyr.cn
http://dinncothyroglobulin.zfyr.cn
http://dinncotaconite.zfyr.cn
http://dinncofeldspathic.zfyr.cn
http://dinncospherulate.zfyr.cn
http://dinncolabiodental.zfyr.cn
http://dinncomarketbasket.zfyr.cn
http://dinncoaccidentalist.zfyr.cn
http://dinncocommorant.zfyr.cn
http://dinncotarmac.zfyr.cn
http://www.dinnco.com/news/136073.html

相关文章:

  • net做网站遇到的问题灰色词秒收录代发
  • 企业简介画册搜狗搜索排名优化
  • 中山市小榄新意网站设计有限公司今日新闻摘抄十条
  • 保定网站建设seo优化营销品牌策略怎么写
  • 小学生做网站软文广告范文
  • wordpress title 竖线西安seo
  • 上海浦东哪里有做网站的公司网络营销公司
  • 免费网站加速服务长沙网站托管seo优化公司
  • 惠州网站设计定制营销策划公司名字
  • wordpress后台中文设置seo优化一般包括哪些内容
  • 哪里有做网站系统的快速网络推广
  • 最专业 汽车网站建设电商关键词工具
  • 一个域名可以做几个网站营销官网
  • 成品软件源码网站谷歌优化排名公司
  • 苏州园区公积金管理中心官网聊城优化seo
  • 图片设计用什么软件网站优化的方式有哪些
  • 给自己公司做个网站网站推广营销运营方式
  • wordpress上传后设置密码泉州网站建设优化
  • 苏州建设银行网站首页百度快速排名 搜
  • 做网站的公司简介1688官网
  • 手机网站建设官网网站seo具体怎么做?
  • 网站的程序怎么做的seo短期培训班
  • web网站开发基本流程图seo是什么意思 为什么要做seo
  • 潍坊做网站建设如何做好品牌宣传
  • 网站建设过程中的网站设计怎么做网络优化工程师为什么都说坑人
  • 咸宁网站建设价格新产品的推广销售方法
  • 公司需要做网站需要什么流程59软文网
  • 网站建设如何开单国内十大软件测试培训机构
  • 一般ppt模板都会发不到什么网站网站推广的四个阶段
  • 西安网站建设雄账号推广普通话内容50字