Adapter (wzorzec projektowy)/kod

Z Wikiźródeł, repozytorium wolnych materiałów źródłowych


Adapter (wzorzec projektowy) • Kod źródłowy
Adapter (wzorzec projektowy)
Kod źródłowy
Kody źródłowe programów stosujących wzorzec projektowy adaptera
Wikipedia
Zobacz w Wikipedii hasło Adapter


Spis treści

[edytuj] Adapter klasowy

[edytuj] Java

/**
 * Java code sample 
 */
 
/* the client class should instantiate adapter objects */
/* by using a reference of this type */ 
interface Stack<T>
{
  void push (T o);
  T pop ();
  T top ();
}
 
/* DoubleLinkedList is the adaptee class */
class DList<T>
{
  public void insert (DNode pos, T o) { ... }
  public void remove (DNode pos) { ... }
 
  public void insertHead (T o) { ... }
  public void insertTail (T o) { ... }
 
  public T removeHead () { ... }
  public T removeTail () { ... }
 
  public T getHead () { ... }
  public T getTail () { ... }
}
 
/* Adapt DList class to Stack interface is the adapter class */
class DListImpStack<T> extends DList<T> implements Stack<T>
{
  public void push (T o) {
    insertTail (o);
  }
 
  public T pop () {
    return removeTail ();
  }
 
  public T top () {
    return getTail ();
  }
}


[edytuj] Adapter obiektowy

[edytuj] Python

# Python code sample
 
class Target:
    def request(self):
        pass
 
class Adaptee:
    def specific_request(self):
        return 'Hello Adapter Pattern!'
 
class Adapter(Target):
    def __init__(self, adaptee):
        self.adaptee = adaptee
 
    def request(self):
        return self.adaptee.specific_request()
 
client = Adapter(Adaptee())
print client.request()