230 likes | 317 Views
Explore component architecture, late integration, component principle, and source code redundancy in program design and development. Learn about the advantages and disadvantages of the component paradigm.
E N D
Generikus Programszerkezetek • Nagy István <nagyist@delfin.klte.hu> • Vég Csaba<vega@dragon.klte.hu> KLTE
Komponens paradigma • Technológiai szempont: • late integration • komponens-architektúra (wiring) • a használat előtérbe helyeződése(funkcionális-csomópontok) • az elemek funkciókon keresztül kezelhetők (nem pedig közvetlenül) • Komponens: “Szaktudáskonzerv”
Komponens-elv (absztrakció) • Komponens-elv a tervezésben és programozásban • adott (nem tetszőleges) módon programozunk • attribútumok helyett tulajdonságok (property)(olvasó/író metódusok) • elnevezések • elemek elrendezése • „szabvány” szerinti forma, tapasztalat közvetlen megszerzése • könnyebben értelmezhető, struktúrája könnyebben azonosítható • könnyebben módosítható
abstract class Text max(): int length(): int read(pos:int) write(pos: int, ch: char) caretPos(): int setCaret(pos:int) delete(pos:int) type(ch:char) rubout() getMaximumLength(): int getLength(): int getCharacter(pos:int): char insertCharacter(pos:int, ch:char) getCaretPos(): int setCaretPos(pos:int) removeCharacter(pos:int) insertCharacter(ch:char) ruboutCharacter()
abstract class Text getMaximumLength(): int getLength(): int getCharacter(pos:int): char insertCharacter(p:int, c:char) getCaretPos(): int setCaretPos(pos:int) removeCharacter(pos:int) insertCharacter(ch:char) ruboutCharacter() getCaretPos(): int setCaretPos(pos:int) getMaximumLength(): int getLength(): int setLength(l:int) getCharacter(pos:int): char getCharacter(): char setCharacter(pos:int, ch: char) setCharacter(ch: char) insertCharacter(pos:int, ch:char) insertCharacter(ch:char) removeCharacter(pos:int) removeCharacter() ruboutCharacter()
A „nyelvek túl flexibilisek” • Feleslegesen sok lehetőséget engednek meg • A komponens elv egy bevált és jól működő változatot határoz meg • De: redundancia a forrásban!
Redundancia /** The alpha property */ private int alpha; /** get method for property alpha */ public int getAlpha() { return alpha; } /** set method for property alpha */ public void setAlpha(int alpha_) { alpha=alpha_; }
Redundancia Copy & Paste ? /** The alpha (double) property */ private double cacheOfAlpha; private boolean cacheOfAlphaIsInvalide =true; /** get method for property alpha */ public double getAlpha() { if(cacheOfAlphaIsInvalide) { cacheOfAlphaIsInvalide=false; cacheOfAlpha=computeAlpha(); } return cacheOfAlpha; } /** compute method for property alpha */ protected double computeAlpha() { return 5*7; }
Redundancia • Hátrányok: • többletmunka • Inkonzisztencia veszélye • „Az igazi programozó, úgy programozik,mint a gép”(Vég Cs.) • Automatizálás lehetősége
Forrásgenerálás • Automatikus forrásgenerálás: generikus szerkezetek segítségével Library Preprocessor Result Text Source
Generikus szerkezetek • Makro eszköz • Preprocesszor • Definícók - definíciós könyvtárak • Hívás definition files original file result file Original text.. {Call 1} Original text.. {Call 2} ... Original text.. Modified text Original text.. New text ... Definition 1 Definition 2 ... +
Generikus szerkezet alkalmazása • Hívás = tulajdonságlista • általános szintaktika • alapértelmezett tulajdonságok • UML-szerű megszorítások {property} {name=alpha} {type=int} {readable} {writeable} {property} {name=alpha1} {type=int} {!readable} {writeable} {property} {name=alpha} {type=double} {cached=5*7}
Generálás /** The alpha (int) property */ private int alpha; /** get method for property alpha */ public int getAlpha() { return alpha; } /** set method for property alpha */ public void setAlpha(int alpha_) { alpha=alpha_; } {property} {name=alpha} {type=int} {readable} {writeable}
Definíció (egyszerűsített) {property}: {<property-description} /**The %name% (%type%) property*/ {property} {!derived}:{<attribute}private %type% %name%; {property} {readable}: {<read-method} %visibility% %type% get_%name%() { return %name%; } {property} {writeable}: {<write-method}%visibility% void set_%name%(%type% %name%_) {%name%=%name%_; } {property} {derived}: {<read-method} %visibility% %type% get_%name%() {return compute_%name%();} {<compute-method} protected %type% compute_%name%() { return %derived%; } /** The alpha (int) property */ public int getAlpha() { return alpha; }
Definíció {cached} {property} {cached}: {<cache}private %type% cacheOf_%name%; {<cacheFlag} private boolean cacheOf_%name%IsInvalide =true; {<read-method} %visibility% %type% get_%name%() { if(cacheOf_%name%IsInvalide) { cacheOf_%name%IsInvalide=false; cacheOf_%name%=compute_%name%(); } return cacheOf_%name%; }
Forrásmódosítás • Hívások speciális megjegyzésként • A makroeszközmódosítja a programforrást Library Preprocessor IDE IDE ! Result Text Source
Forrásmódosítás /// {property} {name=alpha} {type=double} {cached=5*7} /** The alpha (double) property */ private double cacheOfAlpha; private boolean cacheOfAlphaIsInvalide =true; /** get method for property alpha */ public double getAlpha() { if(cacheOfAlphaIsInvalide) { cacheOfAlphaIsInvalide=false; cacheOfAlpha=computeAlpha(); } return cacheOfAlpha; } /** compute method for property alpha */ protected double computeAlpha() { return 5*7; } Less tangled, smaller code Easier maintance More Reusable Common Code Localization
Forrásmódosítás előnyei • A generált eredmény azonnal felhasználható • eredeti állomány a fejlesztőeszközben szerkeztés alatt áll • Az esetleges hibák könnyen megtalálhatóak és javíthatóak /// {property} {name=alpha} {type=doublw} {cached=5*7} /** The alpha (doublw) property */ private doublw cacheOfAlpha; ...
Generikus szerkezetek előnyei II. • Testreszabás: • Korábbi definíciók felüldefiniálása lehetséges • felüldefiniálás közvetlenül akár az eredmény-szöveggel is {property} {readable}: {~read-method} %visibility% %type% get_%name%() { System.out.println(“get_%name%() method”); return %name%; }
Elemző • Külön elemző definiálásának lehetősége • Rövidebb hivatkozások • Speciális körülményekhez igazítható • Alapértelmezett elemző : +alpha: int < {property} {name=alpha} {type=int} {readable} {writeable}
Elemző • További megszorítások (UML): +alpha: double {cached=5*7} {property} {name=alpha} {type=double} {cached=5*7} • Specifikus elemző: {ui-elem} @ 23,41 JLabel(„Név”) {ui-elem} @ 78,41 JTextField() {from=person.getName()}
Generikus szerkezetek • Generikus szerkezetek: • közvetlenül újrafelhasználható szaktudás • Alkalmazási területek: • Komponensek • Felhasználói felületek • Adatbázis-illesztés
Irodalom • C. Szyperski.Component Software. Beyond Object-oriented programming.Addison-Wesley.(elméleti áttekintés, alapelvek, technológiák áttekintése) • Vég Cs. Alkalmazásfejlesztés a Unified Modeling Language szabványos jelöléseivel.Logos 2000., 1999. máj.(UML ismertetése, OO szemlélet, UML komponens-elvű kiterjesztése, RDD alkalmazásfejlesztési módszer) <www.logos2000.hu>