Ohjelmistoarkkitehtuurit Kevät 2012-2013 Johannes Koskinen http://www.cs.tut.fi/~ohar/ 1 12. Kehysarkkitehtuurit Johdanto Kehystyypit Kehysten osittaminen Kehykset ja suunnittelumallit Kehysten etuja ja ongelmia Yhteenvetoa 2 1
Mikä on (ohjelmisto)kehys? Ohjelmistokehys on olioparadigman tapa toteuttaa tuotealusta. Kehys muodostuu kokoelmasta luokkia, jotka toteuttavat tuoteperheen yhteisen arkkitehtuurin ja toiminnallisuuden. Kehys erikoistetaan tuotteeksi. 3 Kehys erikoistetaan toimivaksi tuotteeksi Kehys Sovelluskohtainen koodi kontrolli Erikoistamisrajapinta 4 2
Sovelluskehys vs. perinteinen ohjelmakirjasto: Hollywoodperiaate Aliluokkia, komponentteja Sovelluskohtainen Sovellus Uudelleenkäytettävä Sovelluskehys Aliohjelmia, luokkia, moduuleita Hollywood-periaate: Älä soita meille, me soitetaan teille 5 Erikoistamistekniikat kehyksissä rajapinnan toteutus (~takaisinkutsu) periytyminen (~takaisinkutsu) olioiden/komponenttien luonti, alustus ja konfigurointi geneeristen luokkien (template) instantiointi refleksiivisyys 6 3
Kehystyypit Erikoistamisen tulos Sovelluskehys: erikoistamisen tulos on sovellus Komponenttikehys (framelet): erikoistamisen tulos on komponentti Hierarkkinen kehys: erikoistamisen tulos on uusi kehys Erikoistamismekanismi Muunneltava kehys: erikoistaminen tehdään lähinnä periytymisellä (ja metodin ylimäärittelyllä) Koottava kehys: erikoistaminen tehdään lähinnä instantioinnilla (+ parametroinnilla) ja alustuskonfiguroinnilla Plugin-kehys: erikoistaminen tehdään lähinnä rajapintojen toteutuksella 7 Muunneltavat kehykset A B 8 4
Koottavat kehykset A <<create>> B 9 Plugin-kehykset rajapintoja komponentteja 10 5
Kehysten osittaminen Käsitemallipohjainen lähestymistapa Komponenttipohjainen lähestymistapa Kerrostaminen: hierarkkiset kehykset 11 Käsitemallipohjainen lähestymistapa OO-arkkitehtuurityyli : 1. Tee sovellusalueen käsitemalli 2. Hae ja lisää käsitemalliin yleistykset (kantaluokat) 3. Muunna käsitemalli luokkamalliksi, lisää oletustoteutukset, rajapinnat 4. Tunnista variaatiopisteet luokkamallista 5. Suunnittele variaatiopisteiden toteutus (esim. suunnittelumalleja soveltamalla) 12 6
Esimerkki: Simulointikehys 13 <<framework>> SimulationFW Muunneltava kehys <<interface>> Creature setmyworld(world) show() getx(): int gety(): int move() interact(creature) growold() die() * World getsize(): int add(creature) remove(creature) show() simulate(int, CreatureFactory) <<interface>> CreatureFactory 1 createcreature(): Creature DefaultCreature <<create>> DefaultCreatureFactory xcoord ycoord age setmyworld(world)... die() <<create>> createcreature(): Creature EatingCreature energy interact(creature) SimulationApp main() <<create>> <<create>> EatingCreatureFactory createcreature(): Creature 14 7
<<framework>> SimulationFW DefaultCreature xcoord ycoord age <<interface>> Creature setmyworld(world) show() getx(): int gety(): int move() interact(creature) growold() die() setmyworld(world)... die() * Koottava kehys World getsize(): int add(creature) remove(creature) show() simulate(int,creaturefactory) <<create>> EatingCreature energy interact(creature) <<create>> <<create>> <<interface>> CreatureFactory 1 createcreature(): Creature DefaultCreatureFactory createcreature(): Creature EatingCreatureFactory createcreature(): Creature SimulationApp <<create>> main() 15 <<framework>> SimulationFW <<interface>> Creature setmyworld(world) show() getx(): int gety(): int move() interact(creature) growold() die() * Plug-in kehys World getsize(): int add(creature) remove(creature) show() simulate(int,creaturefactory) 1 PluginLoader <<interface>> CreatureFactory createcreature(): Creature load() <<load>> <<plugin>> EatingApplication EatingCreature energy interact(creature) SimulationApp EatingCreatureFactory main() createcreature(): Creature <<create>> 16 8
Komponenttipohjainen lähestymistapa: Monoliittiset kehykset vs. frameletit Monoliittinen kehys Sovellus Kehys Framelet Framelet Komponentti Komponentti Erikoistus Erikoistus Erikoistus 17 Hierarkkinen kehys 18 9
Hierarkkinen kehys: yleinen simulointikehys Punamuurahaisten simulointisovellus Muurahaisten simulointikehys Hyönteisten simulointikehys Eläinpopulaatioiden simulointikehys Yleinen simulointikehys 19 Hierarkkinen kehys: esimerkkikehys EatingCraeture, EatingCreatureFactory, SimulationApp DefaultCreature, DefaultCreatureFactory World, Creature 20 10
Hierarkkinen kehys: liiketoimintakehys Varastonhallintasovellus Varastonhallintasovelluskehys Spring 21 Strategy Game Framework http://lurgee.net 22 11
Cont. 23 Kysyttävää? 12
Kehykset ja suunnittelumallit Suunnittelumalleilla (GoF) voidaan lisätä järjestelmän joustavuutta sekä tavallisissa sovelluksissa (ylläpidettävyys, siirrettävyys) että kehyksissä (uudelleenkäytettävyys) Hyvin suunniteltu oliosovellus voidaan ymmärtää usein (implisiittisen) kehyksen erikoistuksena. 25 Suunnittelumallit kehyksen erikoistamisrajapintana Sovelluskohtainen koodi Kehys Suunnittelumallin ilmentymä 26 13
Suunnittelumallit ja kehykset Graphicaltem draw * children Kuuluu kehykseen Rectangle Circle FigGroup draw draw draw for all children c: c.draw; 27 Tyypillisiä kehyksissä käytettyjä GoF-suunnittelumalleja Operaatiorunko (Template Method) Strategia (Strategy) Kuorruttaja (Decorator) Tehtaat (Abstract Factory, Factory Method) Tarkkailija (Observer)... 28 14
Operaatiorunko (Template Method) Ongelma: Metodin sovelluskohtainen staattinen variointi 29 Kehyksessä Kehys Creature draw() move(dx, dy) setcolor(backgr) draw() x = x + dx y = y + dy setcolor(color) draw() Sovellus MyCreature draw() piirretään oma eliö 30 15
Strategia (Strategy) Ongelma: Metodin dynaaminen sovelluskohtainen variointi (isäntäolion olemassaolon aikana) 31 Kehyksessä Kehys setcolor(backgr) drawer.draw() x = x + dx y = y + dy setcolor(color) drawer.draw() Creature setdrawer(drawer) move(dx, dy) Drawer draw Sovellus dd = new DeadDrawer(); c.setdrawer(dd); piirretään oma eliö elävänä AliveDrawer draw() DeadDrawer draw() 32 16
Kuorruttaja (Decorator) Ongelma: Miten antaa mahdollisuus liittää sovelluskohtaista toiminnallisuutta tiettyyn kehyksen komponentiin dynaamisesti? 33 Esimerkki 34 17
basic interaction; BasicBehavior Kehys interaction() SpecialBehavior interaction() next.interaction(); Behavior interaction() next behav DefCreature interaction() append(behavior) DefWorld Creature Sovellus Behavior::interaction(); if energy < min { host.die(); } Behavior::interaction(); makesound(); MyBehavior2 MyBehavior1 interaction() interaction() Main: c = new DefCreature(); b1 = new MyBehavior1(); c.append(b1); d = new DefCreature(); b2 = new MyBehavior2(); d.append(b2); DefCreature::interaction: behav.interaction(); Kehys liittää automaattisesti BasicBehavior:in DefCreature-olioon 35 35 Abstrakti tehdas (Abstract Factory) Ongelma: Miten luoda yhdenmukaisesti tietyn sovelluskohtaisen luokkakokoelman olioita kehyksessä? 36 18
Kehyksessä Kehys Obstacle Creature World simulate() ElemFactory makecreature(): Creature makeobstacle(): Obstacle Sovellus MyObstacle MyCreature MyFactory Main: f = new MyFactory() world = new World(f); world.simulate(); return new MyCreature(); return new MyObstacle(); makecreature() makeobstacle() 37 Tarkkailija (Observer) Ongelma: Miten antaa sovelluskohtaisille komponenteille mahdollisuus reagoida jonkin kehyksen komponentin tilan muutoksiin? 38 19
if meteorhit { for all obs: obs.notifymeteor(); } Kehys MeteorSource register() DefWorld simulate() register(meteorobserver) Kehyksessä MeteorObserver notifymeteor() Creature Sovellus MyCreature notifymeteor() 39 Kahden kehyksen yhdistäminen säikeillä GUI-kehys Simulointikehys MainWindow Controller Manager EventLoop säie säie 40 20
Kehysten etuja Kehysten etuja tuotealustojen toteutustekniikkana: Paljon kokemusta (esim. GUI-kehykset) Soveltaa yleistä, tunnettua OO teknologiaa (vrt. DSL) Tukee avoimia variaatiopisteitä (vrt. DSL) Tukee hyvin kerroksittaisia/hierarkkisia tuotealustoja 41 Kehysten ongelmia Teknisesti vaativa tapa tehdä ohjelmistoja, prosessi usein hyvin iteratiivinen Kehyksistä tulee helposti suuria, mutkikkaita ja vaikeasti hallittavia ohjelmistoja (pienentää yrityksen rekkakerrointa) Sovellusten testaaminen voi olla vaikeaa ilman kehyksen koodia 42 21
Yhteenvetoa Kehys on OO tapa toteuttaa tuotealusta Kehysarkkitehtuureja käytetään paljon yrityksissä, kokemukset valtaosalta positiivisia Kehyksen tekeminen on huomattavasti vaativampaa kuin yksittäisen sovelluksen Vältä suurien muunneltavien (white-box) kehysten tekemistä 43 Kysyttävää? 22