Præsentation er lastning. Vent venligst

Præsentation er lastning. Vent venligst

Programmering med interfaces Separering af specifikation, anvendelse og implementation.

Lignende præsentationer


Præsentationer af emnet: "Programmering med interfaces Separering af specifikation, anvendelse og implementation."— Præsentationens transcript:

1 Programmering med interfaces Separering af specifikation, anvendelse og implementation

2 dProg2, F07Regnemaskine.2 Anvendelse, interface, implementation push pop 0 1 2......

3 dProg2, F07Regnemaskine.3 Den onde class Bad { public static void main(String[] args) { int x = 0; Node h = null; Node p = null; read(x); while ( x!=0 ) { p = new Node(); p.v = x; p.next = h; h = p; read(x); } while ( h!=null ) { p = h; h = p.next; x = p.v; write(x); } } } Hvad gør dette program?

4 dProg2, F07Regnemaskine.4 Den gode class Good { public static void main(String[] args) { int x = 0; Stack s = new LinkedStack(); read(x); while ( x!=0 ) { s.push(x); read(x); } while ( !s.isEmpty() ) { x = s.pop(); write(x); } Hvad gør dette program?

5 dProg2, F07Regnemaskine.5 Ond og god, men hvorfor? class Good { public static void main(String[] a) { int x = 0; Stack s = new LinkedStack(); read(x); while ( x!=0 ) { s.push(x); read(x); } while ( !s.isEmpty() ) { x = s.pop(); write(x); } Kommentarer? class Bad { public static void main(String[] a) { int x = 0; Node h = null; Node p = null; read(x); while ( x!=0 ) { p = new Node(); p.v = x; p.next = h; h = p; read(x); } while ( h!=null ) { p = h; h = p.next; x = p.v; write(x); } } }

6 dProg2, F07Regnemaskine.6 Abstraktionsniveauer I de to programmer kan vi identificere (mindst) fire forskellige abstraktionsniveauer. I “den onde” er disse fire niveauer totalt sammenblandet. I “den gode” er disse niveauer klart separeret. Generelt skal vi stræbe efter at designe pro- grammer hvor abstraktionsniveauerne er klart separeret og dermed lette at identificere. Kun herved kan vi håbe på at kunne konstruere vedligeholdelsesvenlige, pålidelige og effektive programmer. application stack linked list “pointers”

7 dProg2, F07Regnemaskine.7 Hvad er en “Stack”? class Good { public static void main (String[] args) { int x = 0; Stack s = new LinkedStack(); read(x); while ( x!=0 ) { s.push(x); read(x); } while ( !s.isEmpty() ) { x = s.pop(); write(x); } interface Stack { /** * post: isEmpty == 'the stack is empty‘ */ public boolean isEmpty(); /** * post: isFull == 'the stack is full‘ */ public boolean isFull(); /** * pre: not isFull * post: e is placed on top of the stack */ public void push(int e); /** * pre: not isEmpty * post: pop == 'the top element' * and the top element is * removed from the stack */ public int pop(); }

8 dProg2, F07Regnemaskine.8 En HP-regnemaskine Indtastning Display 11  11 4  4 - 7 3  3 4  4 5  5 * 20 + 23 * 161

9 dProg2, F07Regnemaskine.9 Rullestakken inde bag scenen Indtastning Display 11  11 4  4 - 7 3  3 4  4 5  5 * 20 + 23 * 161 54375437 display

10 dProg2, F07Regnemaskine.10 Regneudtryk Prefix-udtryk Infix-udtryk Postfix-udtryk Eks. 1 Eks. 2 + 3 * 4 5 * + 3 4 5 3 + 4 * 5 (3+4) * 5 3 4 5 * + 3 4 + 5 *

11 dProg2, F07Regnemaskine.11 Evaluering af postfixudtryk Infix-udtryk Postfix-udtryk 3+4*5 345*+ Indtastning 3  3 4  4 5  5 * 20 + 23

12 dProg2, F07Regnemaskine.12 Hvad sker der bag scenen? 3 4 3 5 4 3 20 3 23 push 4push 5push 3pop b pop a push a*b pop b pop a push a+b

13 dProg2, F07Regnemaskine.13 Algoritme for regnemaskine for alle tegn t i udtryk { hvis ( t er et tal ) push(t) ellers { int b = pop tal af stakken; int a = pop tal af stakken; vælg ( hvis t er ) { ‘+’: push a+b; ‘-’: push a-b; ‘*’: push a*b; ‘-’: push a/b; } 20 3 23 pop b pop a push a+b 3 4 5 * +

14 dProg2, F07Regnemaskine.14 Regnemaskine i Java public int evaluate (String exp, Stack s) { // pre: exp er et lovligt postfixudtryk for (int i = 0; i<exp.length(); i++) { char ch = exp.charAt(i); if ( Character.isDigit(ch) ) s.push(Character.getNumericValue(ch)); else { int b = s.pop(); int a = s.pop(); switch ( ch ) { case '+': s.push(a+b); break; case '-': s.push(a-b); break; case '*': s.push(a*b); break; case '/': s.push(a/b); break; default: ; }//switch }//else } //for return s.pop(); }//evaluate

15 dProg2, F07Regnemaskine.15 En implementation af “Stack” class LinkedStack implements Stack { protected Node sp; public LinkedStack() { sp = null; } public boolean isEmpty() { return sp==null; } public boolean isFull() { return false; } public void push(int e) { Node p = new Node(); p.v = e; p.next = sp; sp = p; } public int pop() { int res = sp.v; sp = sp.next; return res; } 13 9 7  sp Stack s = new LinkedStack() s.push(13); s.push(9); s.push(7); Repræsentationsinvariant:

16 dProg2, F07Regnemaskine.16 Specifikationen er en kontrakt Kontraktperspektivet –to parter –gensidige fordele –gensidige forpligtigelser Skal opfylde start-betingelse Skal opfylde slut-betingelse Kan antage slut-betingelse Kan antage start-betingelse ForpligtigelserFordele Klient Udbyder Service

17 dProg2, F07Regnemaskine.17 Specifikation som brandmur class Calculator { public eval(Stack s) {... b= s.pop(); a= s.pop(); s.push(a+b);... } class LinkedStack implements Stack { protected Node sp; public LinkedStack() { sp= null; } public boolean isEmpty() { return sp==null; } public boolean isFull() { return false; } public void push(int e) { Node p= new Node(); p.v= e; p.next= sp; sp= p; } public int pop() { int res= sp.v; sp= sp.next; return res; } interface Stack { /** * post: isEmpty == the stack is empty */ public boolean isEmpty(); /** * post: isFull == the stack is full */ public boolean isFull(); /** * pre: not isFull * post: e is placed on top of the stack */ public void push(int e); /** * pre: not isEmpty * post: pop == 'the top element' * and the top element is * removed from the stack */ public int pop(); } ApplikationSpecifikationImplementation

18 dProg2, F07Regnemaskine.18 Alternativ implementation af “Stack” class ArrayStack implements Stack { // Class constant final private static int MAX = 100; // Instance variables private int items []; private int sp; // Constructor public ArrayStack() { items = new int[MAX]; MISSING CODE } // Methods public boolean isEmpty() { MISSING CODE } public boolean isFull() { MISSING CODE } public void push(int e) { MISSING CODE } public int pop() { MISSING CODE } } sp 0 Max items Repræsentationsinvariant:

19 dProg2, F07Regnemaskine.19 Endnu et alternativ class CollectionStack implements Stack { // Instance variables private Collection items; // Constructor public CollectionStack() { MISSING CODE } // Methods public boolean isEmpty() { MISSING CODE } public boolean isFull() { MISSING CODE } public void push(int e) { MISSING CODE } public int pop() { MISSING CODE } } 0 items Repræsentationsinvariant: items.size()

20 dProg2, F07Regnemaskine.20 Polymorfi En variabel er kvalificeret med en type, men variablen kan referere til et objekt af en anden type. Dog skal objektets reelle type være en subtype af den kvalificerende type. Der er to måder at lave subtyper på i Java –implementering af et interface –udvidelse af en klasse. Klasseudvidelser (subklasser) skal vi se på i kommende lektioner. class Calculator { public eval(Stack s) {... b = s.pop(); a = s.pop(); s.push(a+b);... } class Engineer { Calculator c = new Calculator(); public calculate() {... c.eval(new LinkedStack()); c.eval(new ArrayStack()); } Qualifier

21 dProg2, F07Regnemaskine.21 Afkobling CalculatorStack ArrayStack CollectionStack LinkedStack 1

22 dProg2, F07Regnemaskine.22 Plug-and-play push pop ArrayStack Der kan monteres både en LinkedStack og en ArrayStack (og alle mulige andre slags stakke man måtte finde på fremover!), fordi regnemaskinen er programmeret i forhold til det generelle Stack-interface. push pop LinkedStack


Download ppt "Programmering med interfaces Separering af specifikation, anvendelse og implementation."

Lignende præsentationer


Annoncer fra Google