60 likes | 182 Views
More on Strategy. Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors) at runtime. Cohesion: How focused the responsibilities of a class are. Cohesion : Does a class do many unrelated things? If “yes”, then it has low cohesion (bad)
E N D
More on Strategy • Cohesion vs. Coupling • Implementing the Strategy Pattern • Changing strategies (behaviors) at runtime SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder
Cohesion: How focused the responsibilities of a class are Cohesion: Does a class do many unrelated things? If “yes”, then it has low cohesion (bad) Does a class represent only one thing? If “yes”, then it has high cohesion (good) • Some of our definitions of cohesion from last time: • Few behaviors in a class • Each class should have one focus SE-2811 Dr. Mark L. Hornick
Coupling: How closely two or more classes are related Does changing code in one class require changes in another class?? • If “yes”, then it has high coupling (bad) • Changing swim or quack behaviors does not require changes to the Duck class (low coupling) SE-2811 Dr. Mark L. Hornick
Week 2, Day 1 • Cohesion vs. Inheritance • Implementing the Strategy Pattern • (?) Changing strategy at run-time Tomorrow: • Factory Pattern Lab: • Use Strategy & Factory Patterns (See online) SE-2811 Dr. Mark L. Hornick
Q1 • A Duck class that swims, quacks, and displays itself exhibits (pick the best choice) • Low cohesion • High cohesion • Low coupling • High coupling SE-2811 Dr. Mark L. Hornick
Q2 • Mallard, Redhead, Decoy, and Mute duck classes that each implement similar quacking methods exhibit (pick the best choice) • Low cohesion • High cohesion • Low coupling • High coupling SE-2811 Dr. Mark L. Hornick