This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | interface Cycle { | |
| val color: Color | |
| fun ride() { | |
| println("Riding my ${this.javaClass.name}") | |
| } | |
| } | |
| interface Bicycle : Cycle | |
| interface Tricycle : Cycle | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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(); | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class RedCycleFactory implements CycleFactory { | |
| @Override | |
| public Bicycle createBicycle() { | |
| return new RedBicycle(); | |
| } | |
| @Override | |
| public Tricycle createTricycle() { | |
| return new RedTricycle(); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Note : Tricycles and Bicycles are "related" objects. | |
| public interface CycleFactory { | |
| Bicycle createBicycle(); | |
| Tricycle createTricycle(); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Declare Cycle subclass that defines wheelCount | |
| abstract class Bicycle extends Cycle { | |
| @Override | |
| int wheelCount() { | |
| return 2; | |
| } | |
| } | |
| // Concrete imlementations | |
| public class BlueBicycle extends Bicycle { | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | abstract class Cycle { | |
| abstract int wheelCount(); | |
| abstract Color color(); | |
| void ride() { | |
| System.out.println("Ridding my " + this.toString() + " on " + wheelCount() + " wheels"); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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() | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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" | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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) { | 
NewerOlder