Git Product home page Git Product logo

designpatternsinkotlin-'s Introduction

Design Patterns In Kotlin

Samples of design patterns in Kotlin.

Defination : Gneral and reusable solution to common problems in software design (Not algorithm)

Patterns :

Creational :

  • Builder

  • Prototype

Creational:

They are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation

Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.

=> Uml :

  1. builder interface
interface UserBuilder {
    fun setName(_name:String):UserBuilder
    fun setAddress (_Address:String):UserBuilder
    fun setAge (_age:Int):UserBuilder
    fun setPhone (_phone:String):UserBuilder
    fun build():User
}
  1. User Class
data class User (val name  : String? ,val age :Int? , val address:String?,val phone :String?) {}
  1. User builder
 class UserBuilderImpl :UserBuilder {
    private var name  : String? =null
     private var age :Int? = null
     private  var address:String? = null
     private var phone :String? = null
        override fun setName(_name: String): UserBuilder {
            this.name =_name
            return this
        }

        override fun setAddress(_Address: String): UserBuilder {
            this.address =_Address
            return this
        }

        override fun setAge(_age: Int): UserBuilder {
            this.age =_age
            return this
        }

        override fun setPhone(_phone: String): UserBuilder {
            this.phone=_phone
            return this
        }

        override fun build(): User {
           return User(this.name, this.age, this.address, this.phone)
        }
    }
  1. Using code in main
fun main(){
    val user =UserBuilderImpl().setName("ahmed")
            .setAge(15).build()
        print(user.toString())
}

Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes (Clonning)

=> Uml :

1. First Prototype
 class FirstSuit :Cloneable {
    var name = "XR8"
    fun properties(){
        println("flying")
    }

     public override fun clone(): FirstSuit {
         return super.clone()as FirstSuit
     }
 }
  1. Second Prototype
 class SecondSuit:Cloneable {

    fun properties(){
        print("can dive")
    }

    public override fun clone(): SecondSuit {
        return super.clone() as SecondSuit
    }
}
  1. Using code in main
fun main(){
    val firstSuit = FirstSuit()
    firstSuit.name = "LL1"
    val firstSuitClone = firstSuit.clone()
    println(firstSuitClone.name)
    val secondSuit = SecondSuit()
    val secondSuitCloneable = secondSuit.clone()
    secondSuitCloneable.properties()

}

designpatternsinkotlin-'s People

Contributors

ahmedsamir9 avatar mahmoudmabrok avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.