1 / 9

Design Patterns

Design Patterns. Ein Muster ( pattern ) ist eine Idee, die sich in einem praktischen Kontext als nützlich erwiesen hat und dies auch in anderen sein wird. Pattern unterstützen die Lösung von Anwendungsproblemen, indem sie für typische Teilprobleme erprobte Lösungsansätze bereitstellen.

regina
Télécharger la présentation

Design Patterns

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. Design Patterns • Ein Muster (pattern) ist eine Idee, die sich in einem praktischen Kontext als nützlich erwiesen hat und dies auch in anderen sein wird. • Pattern unterstützen die Lösung von Anwendungsproblemen, indem sie für typische Teilprobleme erprobte Lösungsansätze bereitstellen. • Das Erkennen solcher Pattern ist jedoch schwierig und erfordert Übung. • Ein Pattern ist eine Gruppe von Klassen mit feststehenden Verantwortlichkeiten und Interaktionen. Es kann eine Gruppe von Klassen sein, die durch Beziehungen verknüpft ist, oder eine Gruppe von kommunizierenden Objekten.

  2. Design Patterns It starts with a simple SimUDuck application: Improve the application: make ducks fly 

  3. Design Patterns Then something went wrong: a rubberduck was flying Overridequack() tosqueak Overridefly() to do nothing other classes are added to the app.  Override quack() to do nothing Override fly() to do nothing Is inheritance a good solution ???

  4. Design Patterns The solution could be an interface: Is this a good solution ???

  5. Design Patterns Identify the aspects of your application that vary and separate them from what stays the same Design Principle: • fly() and quack() are the parts of the SimUDuck application that vary across ducks. • The solution is to create a set of new classes to represent each behaviour Design Principle: Program to an interface, not to an implementation

  6. Design Patterns Implementing the duck behaviors: • With this design, other types of objects can reuse the behavior-classes • New behaviors can be added without modifying any existing classes

  7. Design Patterns • First add two instance variables to the class Duck • Then implement a method that performs the behavior Integrating the duck behaviors: public class Duck { protected QuackBehavior quackBehavior; public void performQuack() { quackBehavoir.quack(); } } public class MallardDuck extends Duck { public MallardDuck () { quackBehavior = new Quack(); flyBehavior = new FlyWithWings(); } }

  8. Design Patterns Client fly behavior The big picture  the strategy pattern quack behavior

  9. Design Patterns Design Principle: Favor composition over inheritance. • Composition defines a HAS-A relationship between two classes • HAS-A can be better than IS-A • For the SimUDuck application the composition can be found between the classes Duck and FlyBehavior

More Related