1 / 24

SE-2811 Software Component Design

SE-2811 Software Component Design. Week 2, Day 1. 6x. =. +. 6x. +. 6x. +. Class Schedule Manager. Schedule.sort().

lyonsr
Télécharger la présentation

SE-2811 Software Component Design

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. SE-2811Software Component Design • Week 2, Day 1 SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick

  2. 6x = + 6x + 6x + SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick

  3. Class Schedule Manager SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick

  4. Schedule.sort() public void sort() { ArrayList<Class> newList = new ArrayList<>();for(int oldIndex = 0; oldIndex < classes.size(); oldIndex++) {int newIndex = 0; Class newClass = classes.get(oldIndex);while (newIndex < newList.size() && newList.get(newIndex).getName() .compareTo(newClass.getName()) <= 0) { newIndex++; } newList.add(newIndex, newClass); }classes = newList;} SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick

  5. Class Schedule Manager (2) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick

  6. Applying the Strategy Pattern: Evidence 1 The Strategy Pattern is a behavioral pattern usually considered and applied at design-time. Premise: Your application requires similar objects whose behavior varies. SE-2811 Dr. Mark L. Hornick

  7. Applying the Strategy Pattern: Evidence 2 As a designer, you watch for inheritance patterns that result in excessive behavior overrides and/or code duplication among classes. SE-2811 Dr. Mark L. Hornick

  8. Applying the Strategy Pattern: Action!(Code v5) Leave behavior that is truly shared in abstract classes. Isolate behavior(s) that vary and declare interfaces that define those behaviors Implement the behaviors in separate concrete classes whose references can be passed to the constructor SE-2811 Dr. Mark L. Hornick

  9. Grassba • Goal: automatically mow your lawn • Issue: need to support different patterns • Classic solution: if statement

  10. public class GrassbaDrive { public void move() { step(); if ( blocked() ) { if ( style == STRIP ) turnRight(180); else if ( style == CROSS ) turnRight(90); else turnRight(random(180)); } } … } • But what about more interesting patterns? • What if need to store pattern-specific information? • What if there are large numbers of patterns?

  11. Solution: write a class public class GrassbaDrive { public GrassbaDrive(MovementStrategy s) { strategy = s; strategy.setDrive(this); } public void move() { step(); is ( isBlocked() ) behaviorStrategy.turn(); } … } public class StripedMovement extends MovementBehavior { public void turn() { turnRight(180); } }

  12. public class CrissCrossMovement extends MovementBehavior { public void turn() { turnRight(90); } } public class RandomMovement extends MovementBehavior { public void turn() { turnright(random(180)); } }

  13. Using the strategies: GrasbaDrive stripedDrive = new GrasbaDrive(new StripedMovement()); GrasbaDrive randDrive = new GrasbaDrive(new RandomMovement()); // note would also have stripedDrive->getBehaviorStrategy()->setDriver(stripedDrive); How does this affect reusability?

  14. General form Is there a preference for interfaces over abstract classes? • Elements: • Context: class that encapsulates, uses specific behavior • Strategy: interface that defines the behavior • ConcreteStrategy: implements a specific behavior

  15. When to use the Strategy pattern? Strategy pattern: a behavioral pattern • Captures run-time behavior • Indicator: application requires similar objects whose behavior varies • But is there another way to capture the different behavior?

  16. Alternative to Strategy pattern • Have an abstract base class with alternatives • But what happens if add gas engine/electric motor option?

  17. Alternative to Strategy pattern • In general: significant code duplication, too many overrides • What is this like with Strategy? Inheritance simpler in some cases, strategy pattern better in others.

  18. Singleton • Sometimes it is important to have only one instance of a class • One factory (that creates many “product” instances) • More on this later! • One window manager • One event logger The challenge: • Ensure that a certain class has only one instance • Provide global access to it

  19. Question: How can you prevent any instances of a class from being created? Don’t write it?... Tell programmers, “just don’t create instances”?

  20. Moving towards a solution… • Restrict the ability to construct more than one instance of a Singleton class • Make the class responsible for keeping track of its one and only instance • Provide a global (static) access method

  21. Basic solution public class Singleton { private static Singleton uniqueInstance = new Singleton(); // private constructor cannot be called; // no instances can be created. private Singleton() { } // return the same instance to all callers of this method public static Singleton getInstance() { return uniqueInstance; } }

  22. Example publicclass Printer { private static Printer uniqueInstance = new Printer(); private Printer() { } publicstatic getInstance() { return uniqueInstance; } public write(String text) { System.out.println(text); // or something fancier… } }

  23. Important Concepts Could you make the constructor protected? • Why a private constructor? Instantiating the Singleton class directly: compilation error • Only the Singleton class can create instances of itself • How can you access the single instance of the Singleton class? getInstance() is a static method, so accessible through the class name • Singleton.getInstance() • Provides global access to the instance

  24. Summary Lazy versus eager instantiation • Lazy initialization: create instance when needed • Eager initialization: instance created at load time, before needed • And whether or not it is actually ever accessed Singleton class: encapsulates its sole instance • Only one instance, has global access • Wins over public, global variables • Retain control of the instance • Can ensure just a single instance

More Related