Forums Register Login

Kotlin in Action: Object classes and patterns

+Pie Number of slices to send: Send
Hi,

How different is the object  patterns in Kotlin from the Java language implementation? Is there standard built-in patterns in Kotlin ?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send


  • 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.

    +Pie Number of slices to send: Send
     

    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.
    +Pie Number of slices to send: Send
     

    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.
    moose poop looks like football shaped elk poop. About the size of this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 576 times.
    Similar Threads
    Kotlin in Action: What topic/concepts in Kotlin are completely different from Java?
    really basic question, arrays .util package
    Pure OO
    Big Nerd Ranch Guide: Kotlin on Android?
    How realistic are the examples in Language Implementation Patterns
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 00:08:27.