samedi 26 décembre 2020

Need to get a uml diagram for the Bird and ToyDuck interface using adapter pattern

** I am confused as to how to design the whole diagram explaining the client, target, adapter and adaptee.

Suppose you have a Bird class with fly() , and makeSound()methods. And also a ToyDuck class with squeak() method. Let’s assume that you are short on ToyDuck objects and you would like to use Bird objects in their place. Birds have some similar functionality but implement a different interface, so we can’t use them directly. So we will use adapter pattern.

Need help asap! here is my code:**

// Java implementation of Adapter pattern 

interface Bird 
{ 
    // birds implement Bird interface that allows 
    // them to fly and make sounds adaptee interface 
    public void fly(); 
    public void makeSound(); 
} 

class Sparrow implements Bird 
{ 
    // a concrete implementation of bird 
    public void fly() 
    { 
        System.out.println("Flying"); 
    } 
    public void makeSound() 
    { 
        System.out.println("Chirp Chirp"); 
    } 
} 

interface ToyDuck 
{ 
    // target interface 
    // toyducks dont fly they just make 
    // squeaking sound 
    public void squeak(); 
} 

class PlasticToyDuck implements ToyDuck 
{ 
    public void squeak() 
    { 
        System.out.println("Squeak"); 
    } 
} 

class BirdAdapter implements ToyDuck 
{ 
    // You need to implement the interface your 
    // client expects to use. 
    Bird bird; 
    public BirdAdapter(Bird bird) 
    { 
        // we need reference to the object we 
        // are adapting 
        this.bird = bird; 
    } 

    public void squeak() 
    { 
        // translate the methods appropriately 
        bird.makeSound(); 
    } 
} 

class Main 
{ 
    public static void main(String args[]) 
    { 
        Sparrow sparrow = new Sparrow(); 
        ToyDuck toyDuck = new PlasticToyDuck(); 

        // Wrap a bird in a birdAdapter so that it 
        // behaves like toy duck 
        ToyDuck birdAdapter = new BirdAdapter(sparrow); 

        System.out.println("Sparrow..."); 
        sparrow.fly(); 
        sparrow.makeSound(); 

        System.out.println("ToyDuck..."); 
        toyDuck.squeak(); 

        // toy duck behaving like a bird 
        System.out.println("BirdAdapter..."); 
        birdAdapter.squeak(); 
    } 
} 

Aucun commentaire:

Enregistrer un commentaire