Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE-2811 Software Component Design

Similar presentations


Presentation on theme: "SE-2811 Software Component Design"— Presentation transcript:

1 SE-2811 Software Component Design
4/12/2019 SE-2811 Software Component Design Week 2, Day 1 17q ,7-11,13-14,17 Jan Ingenhousz: Discoverer that light is needed for photosynthesis and that the green part of the plant is what does that. SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick Dr. Josiah Yoder

2 Slide style: Dr. Hornick
SE-2811 4/12/2019 6x = + Glucose from CO2 and Water But Jan Ingenhousz didn’t discover this process. He discovered that light is needed for photosynthesis and that the green part of the plant is what does that. 6x + 6x + SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick Dr. Josiah Yoder

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

4 Slide style: Dr. Hornick
SE-2811 4/12/2019 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; } We talked through The Big-O run time of this algorithm How to change it to reach Slide 4 Problems with that implementation and goals for improving The Strategy Pattern solution’s class structure and implementation This implements InsertionSort which is not, for large problems, the best way of sorting. But our focus here is on a different problem: How can we use different sorting criteria? SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick Dr. Josiah Yoder

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

6 Applying the Strategy Pattern: Evidence 1
SE-2811 4/12/2019 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 Dr. Josiah Yoder

7 Applying the Strategy Pattern: Evidence 2
SE-2811 4/12/2019 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 Dr. Josiah Yoder

8 Applying the Strategy Pattern: Action! (Code v5)
SE-2811 4/12/2019 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 Dr. Josiah Yoder

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

10 But what about more interesting patterns?
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 { turnRight(random(180));

13 Slide style: Dr. Hornick
public abstract class MovementBehavior { public abstract void turn(); } SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick

14

15 Using the strategies: GrasbaDrive stripedDrive = new GrasbaDrive(new StripedMovement()); GrasbaDrive randDrive = new GrasbaDrive(new RandomMovement()); // note would also have stripedDrive.getBehaviorStrategy().setDriver(stripedDrive); Need the reference back to the container because the movement will depend on sensors, actuators on the Grassba But needing a reference to the container SIGNIFICANTLY reduces reusability. Caution: dependency from element to container

16 Containers and elements
Class Window { private List<Pane> panes; public addPane(p) { panes.add(p); } … } class Dialog extends Pane { private Window parent; public Dialog(Hive p) { parent = p; } Sensible? Sure: some dialogs are displayed in a larger window But consider: Dialog d = new Dialog(null); Some dialogs don’t Effectively need new class for dialogs that stand alone General issue: If element has reference to container, can only use with that container This makes reusing the class much harder Avoid having elements refer to their containers

17 Using the strategies: GrasbaDrive stripedDrive = new GrasbaDrive(new StripedMovement()); GrasbaDrive randDrive = new GrasbaDrive(new RandomMovement()); // note would also have stripedDrive->getBehaviorStrategy()->setDriver(stripedDrive); Need the reference back to the container because the movement will depend on sensors, actuators on the Grassba In this case, strategy tightly coupled with GrassbaDrive, so we must violate the rule

18 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

19 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?

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

21 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.

22 Null Object pattern, reconsidered
How are the Strategy, Null Object patterns similar? How are they different? A significant part of the course: identify problems and solve those problems using (design) patterns

23 Slide style: Dr. Hornick
Strategy Pattern What problem does it solve? What are the alternatives? What are its advantages? What are its disadvantages? SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick

24 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

25 Question: How can you prevent any instances of a class from being created? Don’t write it?... Telling programmers, “be good” works for a time, but sooner or later someone will make a mistake… Tell programmers, “just don’t create instances”?

26 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

27 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;

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

29 Important Concepts Why a private constructor?
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

30 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


Download ppt "SE-2811 Software Component Design"

Similar presentations


Ads by Google