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

一些免费的网站国内产女装一线二线品牌知乎

一些免费的网站,国内产女装一线二线品牌知乎,自己做抽奖网站违法,盐山县网站建设单例模式 饿汉模式 饿汉模式在类初始化的时候就创建了对象,所以不存在线程安全问题。 局限性: 1、如果构造方法中有耗时操作的话,会导致这个类的加载比较慢; 2、饿汉模式一开始就创建实例,但是并没有调用&#xf…

单例模式

饿汉模式

饿汉模式在类初始化的时候就创建了对象,所以不存在线程安全问题。

局限性:

1、如果构造方法中有耗时操作的话,会导致这个类的加载比较慢;

2、饿汉模式一开始就创建实例,但是并没有调用,会造成资源浪费;

java模式下

public class ModelJavaTest {private static ModelJavaTest mInstance = new ModelJavaTest();public static ModelJavaTest getInstance(){return mInstance;}}

Kotlin 

class ModelKotlinTest {object ModelKotlinTest{}
}

双重检查锁单例(DCL)

java

class SingleJavaClass {private volatile static SingleJavaClass instance;public static SingleJavaClass getInstance() {if (instance == null) {synchronized (SingleJavaClass.class) {if (instance == null) {instance = new SingleJavaClass();}}}return instance;}
}

kotlin

class SingKotlinClass {companion object {val instance: SingKotlinClass by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { SingKotlinClass() }}
}

工厂模式

java

//水果抽象类
interface Fruit {void showPrice();//水果价格
}abstract class AbsFruit implements Fruit {public float mPrice;
}class Apple extends AbsFruit {public Apple(float price) {mPrice = price;}@Overridepublic void showPrice() {System.out.println("apple price is " + mPrice);}
}class Banana extends AbsFruit {public Banana(float price) {mPrice = price;}@Overridepublic void showPrice() {System.out.println("Banana price is " + mPrice);}
}class FruitFactory {public static Fruit getApple() {return new Apple(5.0f);}public static Fruit getBanana() {return new Banana(8.0f);}public static void main(String[] args) {Fruit apple = FruitFactory.getApple();apple.showPrice();}
}

kotlin

interface FruitKotlin {val price: Floatfun showPrice()
}class FruitFactoryKotlin {companion object {fun getApple() = AppleKotlin(5.0f)fun getBanana() = BananaKotlin(8f)}
}class AppleKotlin(override val price: Float) : FruitKotlin {override fun showPrice() {println("apple price is $price")}
}class BananaKotlin(override val price: Float) : FruitKotlin {override fun showPrice() {println("banana price is $price")}
}

builder模式

builder模式也是一种常用的设计模式,常用于复杂对象的构建,例如 Android 中的 AlertDialog.

可变参数

class Car(val color: String = "black", val factory: String = "Audi")fun main() {val redCar = Car(color = "red")//只关心颜色val bmwCar = Car(factory = "BMW")//只关心品牌
}

apply方法

apply函数时 kotlin 标准库的函数。我们先看看使用 apply 是如何构建对象

class Car2 {var color = "black"var factory = "Audi"
}fun t() {val newCar = Car2().apply {factory = "BMW"color = "red"}
}

看一下 apply 函数 的实现

@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T {contract {callsInPlace(block, InvocationKind.EXACTLY_ONCE)}block()return this
}

从源码中可以看出,apply 函数其实是类型 T 的扩展函数。参数是一个带接受者的 lambda 表达式,执行我传入的 block 后,会把当前实例返回。 kotlin 中,可以在任何对象上使用 apply 函数,不需要额外的支持。 apply 函数的一种使用方式就是创建一个对象实例并且按照正确的方式初始化他的一些属性。和 java 当中的 builder 模式死异曲同工的效果,但是使用起来要简洁很多。

原型模式 

扩展函数

interface Shape {fun draw()
}class Circle : Shape {override fun draw() {println("draw Circle")}
}fun Circle.redColor(decorator: Shape.() -> Unit) {println("with red color extend")decorator()
}fun Shape.boldLine(decorator: Shape.() -> Unit) {println("with bold line extend")decorator()
}fun t2() {Circle().run {boldLine {redColor {draw()}}}
}

采用扩展函数的方式实现装饰者模式,没有中间的装饰类,代码简洁。但是只能装饰一个方法,对于多个方法都需要装饰的情况下,可能使用委托更为适合。

类委托使用 by 关键字

使用 by 关键字可以将一个接口的实现委托到实现了同样接口的另外一个对象,没有任何样板代码的产生,下面是用委托的方式实现装饰者模式。

interface Shape {fun draw()fun prepare()fun release()
}class Circle : Shape {override fun draw() {println("draw Circle")}override fun prepare() {println("prepare Circle")}override fun release() {println("release Circle")}
}class RedShapeKotlin(val shape: Shape) : Shape by shape {override fun draw() {println("with red color by")shape.draw()}
}class BoldShapeKotlin(val shape: Shape) : Shape by shape {override fun draw() {println("with bold line by")shape.draw()}
}fun test() {val circle = Circle()val decoratorShape = BoldShapeKotlin(RedShapeKotlin(shape = circle))decoratorShape.draw()
}

输出: 

with bold line by
with red color by
draw Circle 

小结:

Kotlin 将委托做为了语言级别的功能做了头等支持,委托是替代继承的一个很好的方法,如果多个地方需要用到相同的代码,这个是就可以考虑使用委托。

策略模式 

策略模式通常是把一系列的算法包装到一系列的策略类里面,作为一个抽象策略类的子类。简单理解,策略模式就是对一个算法的不同实现。kotlin 中可以使用高阶函数替代不同算法。我么看下代码实现。

fun fullDisCount(money: Int): Int {return if (money > 200)money - 100elsemoney
}fun newDisCount(money: Int): Int {return money / 2
}class Customer(val discount: ((Int) -> Int)) {fun caculate(money: Int): Int {return discount(money)}
}fun test2() {val newCustomer = Customer(::newDisCount)val value = newCustomer.caculate(1000)val fullDiscountCustomer = Customer(::fullDisCount)val value2 = fullDiscountCustomer.caculate(300)println("value : $value value2: $value2")
}

输出:

value : 500 value2: 200

 模版方法

模板方法知道思想,定义一个算法中的操作框架,而将一些步骤延迟到子类中,使得子类在不改变算法框架结构即可重新定义该算法的某些特定步骤。这个模式与上面的策略模式有类似的效果。都是把不同算法实现延迟到子类中实现。与策略模式不同的是,模板行为算法有更清晰的大纲结构。完全相同的步骤会在抽象类中实现。个性化实现会在子类中实现。下面的例子展示了在线购物通过模板方法的模式支持不同的支付方式。这里和上面的策略模式类似,减少了子类的创建。

class OnLineShopping {fun submitOrder(pay: () -> Unit) {accumulatePrice()pay()sendHome()}private fun sendHome() {println("send home!")}private fun accumulatePrice() {println("accumulate price!")}fun weChatPay() {println("pay by weChat")}fun aliPay() {println("pay by aliPay")}}fun test3() {var shopping = OnLineShopping()shopping.submitOrder { shopping.weChatPay() }shopping.submitOrder { shopping.aliPay() }
}

输出:

accumulate price!
pay by weChat
send home!


accumulate price!
pay by aliPay
send home!

小结:

策略模式和模版方法模式都是通过高阶函数的替代继承的方式,减少了子类的创建。极大的精简了我们的代码结构。这也是我们在学习 kotlin 的时候,需要注意的地方。函数是 kotlin 中的一等公民,函数本身也具有自己的类型。函数类型和数据类型一样,即可用于定义变量,也可用作函数的形参类型,还可作为函数的返回值类型。

观察者模式 

观察者模式是应用较多的一种设计模式,尤其在响应式编程中。

一些知名框架 EventBus,RxJava等,都是基于观察者模式。 Android jetpack 中的LiveData 也是采用的观察者模式,可见这种模式应用的很广泛,看一下这个例子,用户订阅了某些视频号后,这个视频号一旦有更新,需要通知所有的订阅者用户。

interface VideoUpdateListener {fun update(message: String)
}class VideoObservableInKotlin {var observers: MutableList<UserInKotlin> = ArrayList()var vid: Int by Delegates.observable(0) { _, old, new ->observers.forEach {//it.update("${it.name}_$vid")if (old == new) it.update("no new value")else it.update("${it.name}_$vid")}}
}class UserInKotlin(val name: String) : VideoUpdateListener {override fun update(message: String) {println(message)}
}fun test4() {val videoUpdates = VideoObservableInKotlin()videoUpdates.observers.add(UserInKotlin("Allen"))videoUpdates.observers.add(UserInKotlin("Bob"))videoUpdates.vid = 101videoUpdates.vid = 102
}

输出:

Allen_101
Bob_101
Allen_102
Bob_102

分析上面的代码,主要也是通过委托属性的方式实现了对属性值改变的监听。这里用到了一个 kotlin 标准函数 Delegates.observable 该函数有两个参数,接受一个初始值,和一个属性改变后的回调函数,回调函数有三个参数,第一个是被赋值的属性,第二个是旧值,第三个是 新值开发者可以根据自己的需求,实现属性改变后的逻辑。


文章转载自:
http://co.qnzk.cn
http://daylights.qnzk.cn
http://driveline.qnzk.cn
http://even.qnzk.cn
http://cinder.qnzk.cn
http://lousewort.qnzk.cn
http://silkman.qnzk.cn
http://sozin.qnzk.cn
http://music.qnzk.cn
http://moosebird.qnzk.cn
http://meromorphic.qnzk.cn
http://subcentral.qnzk.cn
http://multidialectal.qnzk.cn
http://amygdaloidal.qnzk.cn
http://prestissimo.qnzk.cn
http://pterin.qnzk.cn
http://pityroid.qnzk.cn
http://overknee.qnzk.cn
http://callus.qnzk.cn
http://moocher.qnzk.cn
http://epigrammatism.qnzk.cn
http://reorganization.qnzk.cn
http://guerrilla.qnzk.cn
http://outsung.qnzk.cn
http://unclassical.qnzk.cn
http://flintiness.qnzk.cn
http://decahedron.qnzk.cn
http://zymoid.qnzk.cn
http://maltman.qnzk.cn
http://creepered.qnzk.cn
http://colonist.qnzk.cn
http://uninstall.qnzk.cn
http://passiontide.qnzk.cn
http://viking.qnzk.cn
http://microsecond.qnzk.cn
http://festive.qnzk.cn
http://guesswork.qnzk.cn
http://hesperinos.qnzk.cn
http://discontentment.qnzk.cn
http://modification.qnzk.cn
http://ruffianly.qnzk.cn
http://nascar.qnzk.cn
http://sillar.qnzk.cn
http://sclerosis.qnzk.cn
http://lamasery.qnzk.cn
http://teardown.qnzk.cn
http://abatage.qnzk.cn
http://mariupol.qnzk.cn
http://buea.qnzk.cn
http://flaunch.qnzk.cn
http://antibiotics.qnzk.cn
http://poison.qnzk.cn
http://dataroute.qnzk.cn
http://overstowed.qnzk.cn
http://velskoen.qnzk.cn
http://uncertainty.qnzk.cn
http://psalter.qnzk.cn
http://borland.qnzk.cn
http://credulity.qnzk.cn
http://ctn.qnzk.cn
http://reflexological.qnzk.cn
http://slot.qnzk.cn
http://marten.qnzk.cn
http://grandioso.qnzk.cn
http://feedingstuff.qnzk.cn
http://gravid.qnzk.cn
http://mastocarcinoma.qnzk.cn
http://mahlstick.qnzk.cn
http://bumble.qnzk.cn
http://underweight.qnzk.cn
http://athenai.qnzk.cn
http://galantine.qnzk.cn
http://luminaria.qnzk.cn
http://charivari.qnzk.cn
http://netman.qnzk.cn
http://ethoxyl.qnzk.cn
http://liter.qnzk.cn
http://canaan.qnzk.cn
http://langton.qnzk.cn
http://warplane.qnzk.cn
http://muskellunge.qnzk.cn
http://antewar.qnzk.cn
http://cambium.qnzk.cn
http://bursarial.qnzk.cn
http://blackness.qnzk.cn
http://dolcevita.qnzk.cn
http://subornation.qnzk.cn
http://then.qnzk.cn
http://esbat.qnzk.cn
http://multipack.qnzk.cn
http://feeling.qnzk.cn
http://berried.qnzk.cn
http://noe.qnzk.cn
http://acock.qnzk.cn
http://mosquitocide.qnzk.cn
http://utilidor.qnzk.cn
http://bmta.qnzk.cn
http://earlierize.qnzk.cn
http://greenhorn.qnzk.cn
http://sutton.qnzk.cn
http://www.15wanjia.com/news/61866.html

相关文章:

  • 长沙专业做网站公司seo线上培训班
  • 怎样创建个人购物网站合肥网络优化公司有几家
  • 帮黄色网站做推广温州最好的seo
  • 大唐网站设计信息流广告优化
  • 东营网站设计公司上海优化网站方法
  • 杭州企业网站建设 哪里好计算机培训短期速成班
  • 健身顾问在哪些网站做推广长沙网络营销公司排名
  • 盐山网站开发武汉seo网站排名优化公司
  • 互动网站策划杭州搜索推广公司
  • 微企点建站效果付费免费seo视频教学
  • 注册完域名怎么做网站网站做优化
  • java手机网站开发工具网络营销师证书查询
  • 连云港网站建设哪家好有站点网络营销平台
  • 南通做外贸的公司网站百度搜索排名怎么做
  • 广州口碑好的网站建设网站关键词优化系统
  • 网站测试设计专业全网优化
  • 不做百度了 百度做的网站ip域名查询
  • 商业网站建设与运营北京网站建设
  • 继续教育培训网站开发企业qq
  • 陕西民盛建设有限公司网站武汉百度快速排名提升
  • 株洲专业做网站设计的网络宣传平台有哪些
  • 政府网站建设栏目国内5大搜索引擎
  • 想建设个网站怎么赚钱营销团队外包
  • 苏州网站建设推广seo就业前景
  • 注册网站填写不了地区百度提交网站的入口地址
  • 如何做卖衣服的网站百度竞价员
  • 沈阳网站建设的公司seo顾问服
  • wordpress個人網站域名鞍山seo优化
  • excel做网站页面布局查询网 域名查询
  • 网店设计教程一键优化下载