Skip to content

Instantly share code, notes, and snippets.

View ericdiazcodes's full-sized avatar

ericdiazcodes

View GitHub Profile
@ericdiazcodes
ericdiazcodes / CycleFactory.kt
Created July 15, 2022 17:04
AbstractFactoryDemo in Kotlin
interface Cycle {
val color: Color
fun ride() {
println("Riding my ${this.javaClass.name}")
}
}
interface Bicycle : Cycle
interface Tricycle : Cycle
@ericdiazcodes
ericdiazcodes / CycleFactoryMain.java
Created July 15, 2022 17:00
AbstractFactoryDemo in Java
public static void main(String[] args) {
var redCycleFactory = new RedCycleFactory();
var blueCycleFactory = new BlueCycleFactory();
var redBicycle = redCycleFactory.createBicycle();
var blueTricycle = blueCycleFactory.createTricycle();
// prints -> Ridding my RedBicycle on 2 wheels
redBicycle.ride();
@ericdiazcodes
ericdiazcodes / RedCycleFactory.java
Created July 15, 2022 16:53
AbstractFactoryDemo
public class RedCycleFactory implements CycleFactory {
@Override
public Bicycle createBicycle() {
return new RedBicycle();
}
@Override
public Tricycle createTricycle() {
return new RedTricycle();
}
@ericdiazcodes
ericdiazcodes / CycleFactory.java
Created July 15, 2022 16:46
AbstractFactoryDemo in Java
// Note : Tricycles and Bicycles are "related" objects.
public interface CycleFactory {
Bicycle createBicycle();
Tricycle createTricycle();
}
@ericdiazcodes
ericdiazcodes / CycleImplementations.Java
Last active July 15, 2022 16:42
AbstractFactoryDemo
// Declare Cycle subclass that defines wheelCount
abstract class Bicycle extends Cycle {
@Override
int wheelCount() {
return 2;
}
}
// Concrete imlementations
public class BlueBicycle extends Bicycle {
@ericdiazcodes
ericdiazcodes / Cycle.Java
Last active July 15, 2022 16:34
AbstractFactoryDemo in Java
abstract class Cycle {
abstract int wheelCount();
abstract Color color();
void ride() {
System.out.println("Ridding my " + this.toString() + " on " + wheelCount() + " wheels");
}
}
@ericdiazcodes
ericdiazcodes / BetterCarFactory.kt
Created July 3, 2022 22:35
FactoryDemo in Kotlin - Better factory implementation
// Define an interface that declares what object it will create. Since it's
// an interface, how the Car will be created will be defined by the subclass
interface BetterCarFactory {
fun getCar(): Car
}
// Create subclasses of the Factory that will return their own specific subclass of Car.
class HondaMotorFactory : BetterCarFactory {
override fun getCar(): Car = Accord()
}
@ericdiazcodes
ericdiazcodes / CarFactory.kt
Last active July 3, 2022 21:38
FactoryDemo in Kotlin
// Create a class that will contain a function that returns a particular type.
// The naming will be "*type*Factory
class CarFactory {
// Create a function that will return the type but will contain
// the details of which subclass of that type is desired based on
// some evaluation, in this case, with will be based on car manufacturer.
fun getCarByManufacturer(carManufacturer: CarManufacturer): Car {
return when (carManufacturer) {
CarManufacturer.HondaMotor -> Accord()
@ericdiazcodes
ericdiazcodes / CarMain.java
Created July 1, 2022 18:48
FactoryDemo in Java
public static void main(String[] args) {
CarFactory carFactory = new CarFactory();
Car bmw = carFactory.getCarByManufacturer(new BMWGroup());
Car mustang = carFactory.getCarByManufacturer(new FordMotor());
bmw.drive(); // prints "Driving away in a BMW"
mustang.drive(); // prints "Driving away in a Mustang"
}
@ericdiazcodes
ericdiazcodes / CarFactory.java
Created July 1, 2022 18:46
FactoryDemo in Java
// Create a class that will contain a function that returns a particular type.
// The naming will be "*type*Factory
class CarFactory {
Car getCarByManufacturer(CarManufacturer carManufacturer) {
if (carManufacturer instanceof HondaMotor) {
return new Accord();
} else if (carManufacturer instanceof BMWGroup) {
return new BMW();
} else if (carManufacturer instanceof FordMotor) {