• Keine Ergebnisse gefunden

Die ObjektOrientierte Mühle

N/A
N/A
Protected

Academic year: 2021

Aktie "Die ObjektOrientierte Mühle"

Copied!
15
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Die ObjektOrientierte Mühle

Das Beispiel soll noch einmal das Konzept der Objektorientiertheit erläutern.

Dabei werden außerdem Interfaces, Vererbung, abstrakte Methoden, Queues, SimplyLinked Lists, Exceptions, ...

verwendet.

9.2.2004

(2)

m a r c – o l i v e r p a h l

Muehle

public interface IMuehle {

public Id anliefern(Korn korn);

public void mahlen();

public MehlSack abholen();

}

(3)

m a r c – o l i v e r p a h l

verschiedene Muehlen

Wind

Wasser

Strom Lager

(4)

m a r c – o l i v e r p a h l

-> UML

Muehle

(5)

m a r c – o l i v e r p a h l

UML-Vererbungs-Diagramm

Haus IMuehle

Muehle

WindMuehle WasserMuehle StromMuehle

(6)

m a r c – o l i v e r p a h l

Muehle.java

Haus IMuehle

Muehle

public abstract class Muehle extends Haus implements IMuehle { protected LagerQueue kornlager; protected LagerQueue mehllager;

public Id anliefern(Korn korn){

new Id(); kornlager.push(theId);

return theId;

}

public abstract void mahlen() throws MahlException;

public MehlSack abholen(){

return (MehlSack) mehllager.pop();

} }

(7)

m a r c – o l i v e r p a h l

WindMuehle.java

Muehle

WindMuehle

public class WindMuehle extends Muehle { private boolean wind = true;

public WindMuehle(String name){

super(name);

}

public void mahlen() throws KeinWindException{

while(kornlager.top() != null){

if (!wind) throw new KeinWindException();

Id inBearbeitungId = (Id) kornlager.pop();

// mahlen mit Wasser

mehllager.push(new MehlSack(inBearbeitungId));

} } }

(8)

m a r c – o l i v e r p a h l

Vorteil des Interface

Muehle

? ? ?

(9)

m a r c – o l i v e r p a h l

Vorteil des Interface

Muehle

(10)

m a r c – o l i v e r p a h l

Vorteil der Instanzen/ Objekte

(11)

m a r c – o l i v e r p a h l

Der Test

public static void main(String[ ] args) {

IMuehle[ ] meineMuehlen = new IMuehle[4];

meineMuehlen[0] = new WindMuehle("WindMühle A");

meineMuehlen[1] = new WindMuehle("WindMühle B");

meineMuehlen[2] = new StromMuehle("StromMühle");

meineMuehlen[3] = new WasserMuehle("WasserMühle");

// 10x wird Korn an zufällig gewählte Mühlen geliefert.

for(int i=0; i<10; i++){

meineMuehlen[(int) (4*Math.random())].anliefern(new Korn());

}

// alle Mühlen mahlen lassen

try{ for(int i=0; i<4; i++) meineMuehlen[i].mahlen();

}catch(MahlException e){ /* irgendetwas sinnvolles tun */ };

// Lager von allen Mühlen leeren MehlSack sack;

for(int i=0; i<4; i++) while ((sack = meineMuehlen[i].abholen()) != null);

}

(12)

m a r c – o l i v e r p a h l

Die Queue

public interface IObjectQueue { public void push(Object obj);

public Object pop();

public Object top();

}

Queue: FirstInFirstOut:

23 42 65 7

push(8);

pop();

8

7

(13)

m a r c – o l i v e r p a h l

SimplyLinked List Node

nextNode

Node

nextNode

Node

nextNode

Node

nextNode

Node

nextNode ...

protected class Node{

protected Node nextNode;

protected Object nodeData;

Node(Node next){ nextNode = next; }

Node(Node next, Object data){ nextNode = next; nodeData = data; } void setNextNode(Node next){ nextNode = next; }

void setData(Object data){ nodeData = data; }

Node getNextNode(){ return nextNode; } Object getData(){ return nodeData; } }

(14)

m a r c – o l i v e r p a h l

Die Queue

public class LagerQueue implements IObjectQueue { protected Node head;

public void push(Object obj){

if (head == null) head = new Node(null, obj);

else {

Node actNode = head;

while(actNode.getNextNode() != null) actNode = actNode.getNextNode();

// Jetzt ist actNode der letzte Knoten

actNode.setNextNode(new Node(null, obj));

// Jetzt ist der neue Knoten hinten angehängt }

} ...

}

(15)

m a r c – o l i v e r p a h l

Die Queue

public class LagerQueue implements IObjectQueue { protected Node head;

public void push(Object obj){ ... } public Object top(){

if (head == null) return null; else return head.getData();

}

public Object pop(){

if (head == null) return null; // kein Knoten da Node res = head;

head = res.getNextNode(); // ehemaliger Frontknoten ausgekettet

return res.getData(); // Der ehemalig head wird garbagecollectet }

} Node

nextNode

Node nextNode head

Referenzen

ÄHNLICHE DOKUMENTE

In this paper we study a system consisting of c parallel identical servers and a common queue. The service times are Erlang-r distributed and the interarrival

Deshalb gibt es einen Zugriffsbereich für Vererbung, der alle Subklassen einer Klasse umfasst. Programmelemente, die als geschützt deklariert

Wir zeigen dann, dass wenn wir eine Schlange erst aufbauen, und dann wieder konsumieren, die Rei- henfolge der Elemente gleich bleibt (useQ (buildQ s)= s), während sie sich bei

Wenn in einer &#34;Klasse&#34; KEINE Methode implementiert ist, wird sie als interface &#34;Schnittstelle&#34; bezeichnet. public interface

Wenn im geschützten Block ein Fehler (eine Ausnahme) auftritt:.. • Ausführung des geschützten Blocks

The IMPll-B ARPANET INTERFACE allows the PDPll user, with the addition of appropriate software, to communicate with other Host machines on the ARPANET.. *

During an interrupt acknowledge sequence, SYNC indicates the presence of a device address, DIN or DOUT determine the type of interrupt operation, and IACK is

This appendix presents tables of'P~chine opcodes and operator execution times.. Table C-l presents the opcodes, and Table C-2 presents the operator execution