Patos Con Interfaces La Mejor Solucion

Clase Main

public class SolutionDucks {
public static void main(String[] args) {

RubberDucks pRubber;
pRubber= new RubberDucks();

WoodDucks pWood;
pWood = new WoodDucks();

RedHeadDucks pRH;
pRH = new RedHeadDucks();

pRH.Show();
System.out.println("\n");
pRubber.Show();
System.out.println("\n");
pWood.Show();
}
}

Clase Madre

public class Ducks {
Flyable flyable;
Quackable quackable;

public void Display (String name){
System.out.println("Mostrando Patito de "+name);
}

public void Swim(){
System.out.println("Puede nadar");
}
}

Tipos de Patos

public class RubberDucks extends Ducks{
RubberDucks(){
super();
flyable=new NoFly();
quackable=new Squeeze();
}
String name="CABEZA ROJA";
public void Show(){
Display(name);
Swim();
Fly();
Quack();
}
public void Fly(){
flyable.Fly();
}
public void Quack(){
quackable.Quack();
}
}

public class WoodDucks extends Ducks{
WoodDucks(){
super();
flyable=new NoFly();
quackable=new NoQuack();
}
String name="MADERA";
public void Show(){
Display(name);
Swim();
Fly();
Quack();
}
public void Fly(){
flyable.Fly();
}
public void Quack(){
quackable.Quack();
}
}

public class RedHeadDucks extends Ducks{
RedHeadDucks(){
super();
flyable=new FlywithWings();
quackable=new Quacks();
}
String name="CABEZA ROJA";
public void Show(){
Display(name);
Swim();
Fly();
Quack();
}
public void Fly(){
flyable.Fly();
}
public void Quack(){
quackable.Quack();
}
}

Interfaces

interface Quackable{
public void Quack();
}

interface Flyable{
public void Fly();
}

Clases que implementan las interfaces

public class Squeeze implements Quackable{
Squeeze() {}
public void Quack(){
System.out.println("Hace Squeeze");
}
}

public class Quacks implements Quackable{
Quacks() {}
public void Quack(){
System.out.println("Hace Quack!!");
}
}

public class NoQuack implements Quackable{
NoQuack() {}
public void Quack(){
System.out.println("No puede hacer Quack");
}
}

public class NoFly implements Flyable{
NoFly(){}
public void Fly(){
System.out.println("No puede nadar");
}
}

public class FlywithWings implements Flyable{
FlywithWings(){}
public void Fly(){
System.out.println("Vuela con alas");
}
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License