1 / 21

Sim-U-Duck

Sim-U-Duck. Welcome to Design Patterns. Simple Sim-U-Duck design. By Joe the programmer. A new requirement. Ducks must be able to fly!. A new requirement. Ducks must be able to fly!. Something went wrong. What happened?.

marcie
Download Presentation

Sim-U-Duck

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. Sim-U-Duck Welcome to Design Patterns

  2. Simple Sim-U-Duck design • By Joe the programmer

  3. A new requirement • Ducks must be able to fly!

  4. A new requirement • Ducks must be able to fly!

  5. Something went wrong... What happened? A localized update to the code caused a non-local side effect (flying rubber ducks)!

  6. Something went wrong...

  7. Something went wrong...

  8. Alternatives • Override methods in subclasses RubberDuck : Duck DecoyDuck : Duck Display() {//rubber duck} Quack(){//squeak} Fly(){//do nothing} Display() {//decoy duck} Fly(){//do nothing} MallardDuck : Duck Duck RedheadDuck : Duck Display() {//mallard duck} Display() //abstract Quack(){//quack} Fly(){//fly} Display() {//redhead duck}

  9. Alternatives • Create interfaces Flyable and Quackable

  10. Alternatives • Create interfaces Flyable and Quackable

  11. New requirement: 48 duck classes! I want a solution withoutduplicate code

  12. The one constant in software development

  13. Design principle N° 1

  14. Separating what changes from what stays the same

  15. Design principle N° 2

  16. Designing the Duck behaviors <<interface>> FlyBehavior FlyWithWings FlyNoWay Fly() { //implements duck flying } Fly() { //do nothing – can’t fly } Fly()

  17. Implementing the Duck behaviors <<interface>> FlyBehavior FlyWithWings <<interface>> QuackBehavior FlyNoWay MuteQuack Squeak Quack Fly() { //implements duck flying } Fly() { //do nothing – can’t fly } Quack() { //do nothing – can’t quack } Quack() { //rubber duckie squeak } Quack() { //implements duck quacking } Fly() Quack()

  18. Integrating the Duck Behavior

  19. Design Principle N°3 • HAS-A can be better than IS-A

  20. Design Principles so far

  21. The big picture

More Related