Skip to content

Instantly share code, notes, and snippets.

View ericdiazcodes's full-sized avatar

ericdiazcodes

View GitHub Profile
@ericdiazcodes
ericdiazcodes / CarManufacturer.java
Last active July 1, 2022 18:44
FactoryDemo in Java
// For this example, we are shopping for a Car based on the CarManufacturer
interface CarManufacturer {
}
final class HondaMotor implements CarManufacturer {
}
final class BMWGroup implements CarManufacturer {
}
@ericdiazcodes
ericdiazcodes / Earth.kt
Created June 30, 2022 05:42
SingletonDemo in kotlin
object Earth { }
@ericdiazcodes
ericdiazcodes / Mars.java
Created June 30, 2022 05:36
SingletonDemo in Java - `final` keyword variation
class Mars {
// Declare private static member variable as `final` to inform the
// compiler that this value will never change.
private static final Mars SINGLETON_INSTANCE = new Mars();
// Make the constructor private to avoid any other instances of this class
// From being created.
private Mars() {
@ericdiazcodes
ericdiazcodes / Earth.java
Last active June 30, 2022 05:22
SingletonDemo in Java
class Earth {
// Have a private and static member variable to hold the singleton instance.
private static Earth SINGLETON_INSTANCE;
// Make the constructor private to avoid any other instances of this class
// From being created.
private Earth() {
}
fun ConstraintLayout.updateConstraints(instructions: List<ConstraintInstructions>) {
ConstraintSet().also {
it.clone(this)
for (instruction in instructions) {
if (instruction is ConnectConstraint) it.connect(instruction.startID, instruction.startSide, instruction.endID, instruction.endSide)
if (instruction is DisconnectConstraint) it.clear(instruction.startID, instruction.startSide)
}
it.applyTo(this)
}
}
ConnectConstraint(R.id.image, ConstraintSet.Top, R.id.title, ConstraintSet.BOTTOM)
<TextView
android:id="@+id/title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/image"
interface ConstraintInstructions
data class ConnectConstraint(val startID: Int, val startSide: Int, val endID: Int, val endSide: Int) : ConstraintInstructions
data class DisconnectConstraint(val startID: Int, val startSide: Int) : ConstraintInstructions