Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Kotlin in Action: Object classes and patterns  RSS feed

 
Ranch Hand
Posts: 135
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How different is the object  patterns in Kotlin from the Java language implementation? Is there standard built-in patterns in Kotlin ?
 
author
Ranch Hand
Posts: 149
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is that in Kotlin there's a special keyword, , which allows you to create a singleton. In Java, you need to implement it manually.

Several other design patterns become unnecessary in Kotlin - for example, instead of using the Strategy pattern, you can simply use a value of a functional type.
 
Sheriff
Posts: 12084
197
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as the object keyword in Kotlin makes it more straightforward to implement Singleton, isn't using a functional value just another implementation alternative? To me, the pattern itself is more about the context and the general characteristics of the solution applied since there can be many different ways to implement a particular pattern.

In general, the Strategy Pattern involves using different implementations of an abstract API. How you implement that can be via full-blown objects, which I assume you could also do in Kotlin, or via a functional type, which are probably easier to do in Kotlin. With the introduction of lambda expressions in Java 8, you might even get pretty close to how it's done in Kotlin.
 
Junilu Lacar
Sheriff
Posts: 12084
197
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I mean. This article (https://medium.com/@ahmedrizwan/kotlin-design-patterns-the-strategy-724c1ab632f6) shows an example of Strategy in Kotlin (function values) vs. Java (objects).

The Java 8 (lambdas) version would be something like this:

You can try this out here: https://repl.it/Mpyv/3

Admittedly, this version is not much shorter than the Java example using objects and classes but it still does look similar to the Kotlin version.

 
Ranch Hand
Posts: 470
8
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Divya Shiv wrote:Hi,

How different is the object  patterns in Kotlin from the Java language implementation? Is there standard built-in patterns in Kotlin ?



Kotlin is 100 per cent compatible with Java, Kotlin generates java code,  all the design patterns remain the same. There are some shortcuts, object companion for singletons, by for Delegation patterns, the Observer pattern is changed by RXJava that is 100% applicable from Kotlin, giving you for free lambda expressions, even if runs on java7, also Kotlin has some inbuilt functions that mimicks streams and functional paradigms, but usually people implement RxJava as best practice
 
Divya Shiv
Ranch Hand
Posts: 135
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


  • Here's what I mean. This article (https://medium.com/@ahmedrizwan/kotlin-design-patterns-the-strategy-724c1ab632f6) shows an example of Strategy in Kotlin (function values) vs. Java (objects).

    The Java 8 (lambdas) version would be something like this:


  • Thanks Junilu for the detailed explanation with reference providing examples on implementations of patterns.

     
    Divya Shiv
    Ranch Hand
    Posts: 135
    Java Netbeans IDE Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Giovanni Montano wrote:

    Divya Shiv wrote:Hi,

    How different is the object  patterns in Kotlin from the Java language implementation? Is there standard built-in patterns in Kotlin ?



    Kotlin is 100 per cent compatible with Java, Kotlin generates java code,  all the design patterns remain the same. There are some shortcuts, object companion for singletons, by for Delegation patterns, the Observer pattern is changed by RXJava that is 100% applicable from Kotlin, giving you for free lambda expressions, even if runs on java7, also Kotlin has some inbuilt functions that mimicks streams and functional paradigms, but usually people implement RxJava as best practice




    Thank you Giovanni for the clarification of base  patterns remaining same in both.
     
    Divya Shiv
    Ranch Hand
    Posts: 135
    Java Netbeans IDE Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dmitry Jemerov wrote:The difference is that in Kotlin there's a special keyword, , which allows you to create a singleton. In Java, you need to implement it manually.

    Several other design patterns become unnecessary in Kotlin - for example, instead of using the Strategy pattern, you can simply use a value of a functional type.



    Thanks  Dmitry for the brief explanation. The implementations of patterns is different one of which is functional way.
     
    Good night. Drive safely. Here's a tiny ad for the road:
    Why should you try IntelliJ IDEA ?
    https://coderanch.com/wiki/696337/IntelliJ-IDEA
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!