Skip to content

Instantly share code, notes, and snippets.

@deepakshrma
Last active February 26, 2022 10:22
Show Gist options
  • Save deepakshrma/29db105da89cb223d86b813f14b64cbd to your computer and use it in GitHub Desktop.
Save deepakshrma/29db105da89cb223d86b813f14b64cbd to your computer and use it in GitHub Desktop.
Kotlin - Design Pattern In Easy Way | Creational Patterns
class Car {
String type;
String model;
String color;
int speed;
Car(String type, String model, String color) {
this.type = type;
this.model = model;
this.color = color;
}
@Override
public String toString() {
return "Car{" +
"type='" + type + '\'' +
", model='" + model + '\'' +
", color='" + color + '\'' +
", speed=" + speed +
'}';
}
}
public class BuilderDemo {
public static void main(String args[]) {
List<Car> cars = Arrays.asList(
new Car("Ford", "Focus", "red"),
new Car("Toyota", "Auris", "blue"),
new Car("Volkswagen", "Golf", "green")
);
cars.forEach(System.out::println);
}
}
import java.util.Arrays;
import java.util.List;
class Car {
String type;
String model;
String color;
int speed = 10; // Default speed
Car(String type, String model, String color, int speed) {
this.type = type;
this.model = model;
this.color = color;
this.speed = speed;
}
Car(String type, String model, String color) {
this.type = type;
this.model = model;
this.color = color;
}
Car(String type, String model) {
this.type = type;
this.model = model;
this.color = "black"; // Default color in constructor
}
// rest of the code
}
public class BuilderDemo {
public static void main(String args[]) {
List<Car> cars = Arrays.asList(
new Car("Ford", "Focus", "red", 100),
new Car("Toyota", "Auris", "blue"),
new Car("Volkswagen", "Golf")
);
cars.forEach(System.out::println);
}
}
import java.util.Arrays;
import java.util.List;
class Car {
private final String type;
private final String model;
private String color;
private int speed;
Car(String type, String model, String color, int speed) {
this.type = type;
this.model = model;
this.color = color;
this.speed = speed;
}
// Define getters only
@Override
public String toString() {
return "Car{" +
"type='" + type + '\'' +
", model='" + model + '\'' +
", color='" + color + '\'' +
", speed=" + speed +
'}';
}
public static class CarBuilder {
private final String type;
private final String model;
private String color;
private int speed;
public CarBuilder(String type, String model) {
this.type = type;
this.model = model;
}
public CarBuilder color(String color) {
this.color = color;
return this;
}
public CarBuilder speed(int speed) {
this.speed = speed;
return this;
}
public Car build() {
Car car = new Car(this.type, this.model, this.color, this.speed);
this.setDefaults(car);
return car;
}
private void setDefaults(Car car) {
if (car.speed == 0) {
car.speed = 10;
}
if (car.color == null) {
car.color = "black";
}
}
}
}
public class BuilderDemo {
public static void main(String[] args) {
Car fordCar = new Car.CarBuilder("Ford", "Focus").speed(100).color("red").build();
Car toyotaCar = new Car.CarBuilder("Toyota", "Auris").color("blue").build();
Car volksCar = new Car.CarBuilder("Volkswagen", "Golf").build();
List<Car> cars = Arrays.asList(
fordCar,
toyotaCar,
volksCar
);
cars.forEach(System.out::println);
}
}
class CarKt(
private val type: String,
private val model: String,
private val color: String = "black", // default color
private val speed: Int = 10 // Default Speed
){
override fun toString(): String {
return "CarKt(type='$type', model='$model', color='$color', speed=$speed)"
}
}
fun main(args: Array<String>) {
val fordCar = CarKt("Ford", "Focus", "red", 100)
val toyotaCar = CarKt("Toyota", "Auris", "blue")
val volksCar = CarKt("Volkswagen", "Golf")
val cars = listOf(
fordCar,
toyotaCar,
volksCar
)
cars.forEach { x -> println(x) }
}
// rest of the code
fun main(args: Array<String>) {
val fordCar = CarKt("Ford", "Focus", "red", 100)
val toyotaCar = CarKt("Toyota", "Auris", speed = 120)
val volksCar = CarKt("Volkswagen", "Golf")
val cars = listOf(
fordCar,
toyotaCar,
volksCar
)
cars.forEach { x -> println(x) }
/*
CarKt(type='Ford', model='Focus', color='red', speed=100)
CarKt(type='Toyota', model='Auris', color='black', speed=120)
CarKt(type='Volkswagen', model='Golf', color='black', speed=10)
*/
}
class LazySingleton {
private static volatile LazySingleton instance = null;
// private constructor
private LazySingleton() {
}
public static LazySingleton getInstance() {
if (instance == null) {
synchronized (LazySingleton.class) {
// Double check
if (instance == null) {
instance = new LazySingleton();
}
}
}
return instance;
}
}
package singleton
class LazySingletonKt {
companion object {
@Volatile
private var instance: LazySingletonKt? = null
fun get(): LazySingletonKt? {
if (instance == null) {
synchronized(LazySingletonKt::class.java) {
if (instance == null) {
instance = LazySingletonKt()
}
}
}
return instance
}
}
}
fun main() {
val lazySingletonKt1 = LazySingletonKt.get();
val lazySingletonKt2 = LazySingletonKt.get();
println(lazySingletonKt1 == lazySingletonKt2) // true
}
object SingletonObjectKt {
var id: Int = 0;
fun getId() = this.toString()
}
fun main() {
val lazySingletonKt1 = SingletonObjectKt;
lazySingletonKt1.id = 1;
val lazySingletonKt2 = SingletonObjectKt;
println(lazySingletonKt1.id == lazySingletonKt2.id)
println(lazySingletonKt1.id)
println(lazySingletonKt1.getId() == lazySingletonKt2.getId())
println(lazySingletonKt1.getId())
}
enum CarType {
SMALL, LUXURY
}
abstract class Car {
private CarType model = null;
public Car(CarType model) {
this.model = model;
}
protected abstract void construct();
}
class LuxuryCar extends Car {
LuxuryCar() {
super(CarType.LUXURY);
construct();
}
@Override
protected void construct() {
System.out.println("Building luxury car");
}
}
class SmallCar extends Car {
SmallCar() {
super(CarType.SMALL);
construct();
}
@Override
protected void construct() {
System.out.println("Building small car");
}
}
class CarFactory {
public static Car buildCar(CarType model) {
Car car = null;
switch (model) {
case LUXURY:
car = new LuxuryCar();
break;
default:
car = new SmallCar();
break;
}
return car;
}
}
public class FactoryDemo {
public static void main(String[] args) {
System.out.println(CarFactory.buildCar(CarType.SMALL));
System.out.println(CarFactory.buildCar(CarType.LUXURY));
}
}
enum class CarTypeKt {
SMALL, LUXURY
}
abstract class CarKt(val model: CarTypeKt) {
protected abstract fun construct()
}
class LuxuryCarKt : CarKt(CarTypeKt.LUXURY) {
override fun construct() {
println("Building luxury car")
}
init {
construct()
}
}
class SmallCarKt : CarKt(CarTypeKt.SMALL) {
override fun construct() {
println("Building small car")
}
init {
construct()
}
}
object CarFactoryKt {
fun buildCar(model: CarTypeKt = CarTypeKt.SMALL) = when (model) {
CarTypeKt.LUXURY -> LuxuryCarKt()
else -> SmallCarKt()
}
}
fun main() {
println(CarFactoryKt.buildCar(CarTypeKt.LUXURY).model)
println(CarFactoryKt.buildCar().model)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment