Softwaretechnik http://kohnlehome.de/informatik/strategy.pdf
Design Pattern: Strategy
1 Designprinzip
Algorithmen kapseln und austauschbar machen durch Komposition statt Vererbung.
2 Java-Code
2.1 Strategieschnittstelle
public i n t e r f a c e S t r a t e g i e S c h n i t t s t e l l e { public abstract void a l g o r i t h m u s ( ) ; }
2.2 Konkrete Strategien
public c l a s s S t r a t e g i e A implements S t r a t e g i e S c h n i t t s t e l l e {
@Override
public void a l g o r i t h m u s ( ) { System . o ut . p r i n t l n ( ” V e r s i o n A” ) ; } }
public c l a s s S t r a t e g i e B implements S t r a t e g i e S c h n i t t s t e l l e {
@Override
public void a l g o r i t h m u s ( ) { System . o ut . p r i n t l n ( ” V e r s i o n B” ) ; } }
2.3 Client
Muß bei ¨Anderung der Strategie nicht ver¨andert werden.
public c l a s s C l i e n t {
private S t r a t e g i e S c h n i t t s t e l l e a l g o r i t h m u s ;
public void s e t S t r a t e g i e ( S t r a t e g i e S c h n i t t s t e l l e a l ){ a l g o r i t h m u s = a l ; }
public void tuwas ( ){ a l g o r i t h m u s . a l g o r i t h m u s ( ) ; } }
2.4 Verwendung des Clients
Anderung der Strategie zur Laufzeit m¨¨ oglich C l i e n t c l i e n t = new C l i e n t ( ) ;
c l i e n t . s e t S t r a t e g i e (new S t r a t e g i e A ( ) ) ; c l i e n t . tuwas ( ) ; // ” V e r s i o n A”
c l i e n t . s e t S t r a t e g i e (new S t r a t e g i e B ( ) ) ; c l i e n t . tuwas ( ) ; // ” V e r s i o n B”
Literatur
[Head First Design Patterns] http://www.oreilly.de/catalog/9780596007126/
[Wikipedia] http://de.wikipedia.org/wiki/Strategie_(Entwurfsmuster)
Franz Kohnle Seite 1 von 1 5. November 2017