1 / 13

Fra algoritmemønster til generisk metode

Fra algoritmemønster til generisk metode. findEn og findAlle. Hvad har disse metoder tilfælles?. findEn. Hvad har disse metoder tilfælles?. findAlle. Parametrisering (Turtle). // tegn et kvadrat med sidelængde 100 public void square100() { ... }

Download Presentation

Fra algoritmemønster til generisk metode

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fra algoritmemønstertil generisk metode findEn og findAlle

  2. Hvad har disse metoder tilfælles?

  3. findEn

  4. Hvad har disse metoder tilfælles?

  5. findAlle

  6. Parametrisering (Turtle) //tegn et kvadrat med sidelængde 100 public void square100() { ... } //tegnet kvadrat med sidelængde length public void square(int length) { ... } //tegnen polygon af grad degree med sidelængde length public void polygon(int degree, int length) { ... } public void polygon-4-100() { ... } public void polygon-4(int length) { ... } public void polygon(int degree, int length) { ... }

  7. Pointer • Det kan betale sig at lave gode generelle operationer som kan genbruges i mange situationer. • Parametrisering er nøglen til at lave generelle operationer. • Det er svært at ‘opfinde’ de gode generelle operationer (konkret  generelt), men opsøg dem!

  8. Parametrisering? public List<Track> findAllByArtist (List<Track> tracks, String artist) { List<Track> res= new ArrayList<Track>(); for ( Track t: tracks ) if ( t.getArtist().contains(artist) ) res.add(t); return res; } public List<Photo> findAllAfterDate(Date d) { List<Photo> res= new ArrayList<Photo>(); for ( Photo p: photos ) if ( p.getDate().isLaterThan(d) ) res.add(p); return res; }

  9. Parametriseringafelementtype public List<Track> findAllByArtist (List<Track> tracks, String artist) { List<Track> res= new ArrayList<Track>(); for ( Trackt: tracks ) if ( t.getArtist().contains(artist) ) res.add(t); return res; } public List<Photo> findAllAfterDate(Date d) { List<Photo> res= new ArrayList<Photo>(); for (Photop: photos) if ( p.getDate().isLaterThan(d) ) res.add(p); return res; }

  10. Parametriseringafudvælgelseskriterie public List<Track> findAllByArtist (List<Track> tracks, String artist) { List<Track> res= new ArrayList<Track>(); for ( Track t: tracks ) if ( t.getArtist().contains(artist) ) res.add(t); return res; } public List<Photo> findAllAfterDate(Date d) { List<Photo> res= new ArrayList<Photo>(); for ( Photo p: photos ) if ( p.getDate().isLaterThan(d) ) res.add(p); return res; }

  11. Algorithmic Pattern: findAll publicList<T> methodName (List<T> lst, additional params) { List<T> result= new ArrayList<T>(); for (Te: lst) { if ( <<< criteriaSatisfiedBy(e)>>> ) { result.add(e); } } return result; } Instantiation of pattern findAll • Substitute concrete type for T • Choose proper methodname • Choose appropriate additional parameters for the concrete criteria

  12. Generic Method: findAll // find alleobjekteri listen lst der opfylderkriterietc public static <T> List<T> findAll(List<T> lst, Criteria<T>c) { List<T> result= new ArrayList<T>(); for ( Te : lst ) { if ( c.satisfiedBy(e) ) { result.add(e); } } return result; } interface Criteria<T> { booleansatisfiedBy(Te); } List<Person> persons= new ArrayList<Person>(); class SeniorCriteriaimplementsCriteria<Person> {...} findAll(persons, new SeniorCriteria());

  13. Pointer • Det kan betale sig at lave gode generelle operationer som kan genbruges i mange situationer. • Parametrisering er nøglen til at lave generelle operationer. • Det er svært at ‘opfinde’ de gode generelle operationer (konkret  generelt), men opsøg dem!

More Related