Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

5 - Adaptér, Iterátor

Možné řešení z minulého cvičení

Teorie

Adapter

Problém: Propojuji v systému komponenty, které mají navzájem nekompatibilní interface.

Záměr: Klient umí komunikovat s rozhraním A, ale komponenta, kterou chce klient nově použít má rozhraní B. Pak vytvořím adaptér, který bude mít rozhraní A, ale uvnitř bude volání překládat na rozhraní B, které vystavuje požadovaná komponenta. Klient pak místo přímého volání komponenty B volá náš adaptér.

Praktický případ: Máme naší moderní webovou službu s REST rozhraním, která pracuje s daty v JSON struktuře. V rámci logiky webové služby je nezbytná integrace s Legacy SOAP webovou službou, založené na XML datové struktuře.

Iterátor

Problém: Při procházení datové struktury jsme pevně svázáni s její implementací.

Záměr: Pomocí iterátoru budeme abstrahovat samotný algoritmus procházení (tzv. iterování) od implementace datové struktury. Klient používá iterátor tak, že pouze řídí procházení datové struktury pomocí metod next() a isDone() (někdy hasNext()), ale vůbec nemusí znát vlastnosti ani implementaci datové struktury.

Příklady k řešení

Adapter

Stáhněte si následující projekt z gitu:

https://gitlab.fel.cvut.cz/B191_B6B36OMO/b191_b6b36omo/tree/master/cv5.1_assignment

Mějme rozhraní Stack s následující podobou:

public interface Stack {
 
        /*
         *  Method for inserting new element on top of the stack.
         *  @param int Value to insert
         */
        public void push(int toInsert);
 
 
        /*
         *  Method for removing element from top of the stack
         *  @return int Element from top of the stack.
         */
        public int pop();
 
 
        /*
         *  Method for determining, whether stack is empty.
         *  @return boolean Empty stack indicator.
         */
        public boolean isEmpty();
 
        /*
         *  Method for retrieving depth of stack.
         *  @return int Current amount of elements in stack.
         */
        public int getSize();
}

Implementujte adaptér, který bude adaptovat Stack na ExtendedStack s následujícím rozhraním:

public interface ExtendedStack {
 
    /*
     *  Method for inserting new element on top of the stack.
     *  @param int Value to insert
     */
    public void push(int toInsert);
 
    /*
     *  Method for inserting multiple elements on top of the stack. Elements are inserted in order from first element.
     *  @param int[] Array of values to insert.
     */
    public void push(int[] toInsert);
 
    /*
     *  Method retrieves value from the top of the stack, value is not removed from the stack.
     *  @return int Value of the item on top of the stack.
     */
    public int top();
 
    /*
     *  Method for removing element from top of the stack
     *  @return int Element from top of the stack.
     */
    public int pop();
 
    /*
     *   Method for removing first negative element from the stack.
     *   @return int Top-most negative element in stack.
     */
    public int popFirstNegativeElement();
 
 
    /*
     *  Method for determining, whether stack is empty.
     *  @return boolean Empty stack indicator.
     */
    public boolean isEmpty();
 
    /*
         *  Method for retrieving depth of stack.
         *  @return int Current amount of elements in stack.
         */
    public int getSize();
}

Iterátor

Stáhněte si následující projekt:

https://gitlab.fel.cvut.cz/B191_B6B36OMO/b191_b6b36omo/tree/master/cv5.2_assignment

Vytvořte podle následného rozhraní iterároty, které budou:

  • Procházet pole celých čísel - StandardArrayIterator
  • Procházet pozpátku pole celých čísel - ReverseArrayIterator
  • Procházet pouze sudé indexy celočíselného pole - EvenIndexIterator
  • Jednotlivé iterátory otestujte pomocí junit testů

public interface Iterator {
 
    /*
     *  Method for retrieving value of current item.
     *  @return int Value of current item.
     */
    public int currentItem();
 
    /*
     *  Method for shifting iterator to next element of collection.
     *  @return int Value of the actual element (after shift).
     */
    public int next();
 
    /*
     *  Method for verifying, whether the iterator is at the end of data structure.
     *  @return boolean Returns true for last element of collection.
     */
 
    public boolean isDone();
 
    /*
     *  Method for setting iterator to point at the beginning of the data structure.
     *  @return int First value of collection.
     */
    public int first();
}

Java má v Interface iterátoru pouze metody E next() a boolean hasNext(). Zamyslete se nad výhodami/nevýhodami implementace v Java proti právě dokončenému příkladu.
courses/b6b36omo/labs/lab052017.txt · Last modified: 2019/10/21 22:11 by kukacdav