CSSE 220 Day 17 Introduction to your Vector

  • Slides: 19
Download presentation
CSSE 220 Day 17 Introduction to your Vector Graphics project Swing. Demo continued Designing

CSSE 220 Day 17 Introduction to your Vector Graphics project Swing. Demo continued Designing classes Screen Layout sketch for VG

Vector. Graphics project Visit http: //www. rosehulman. edu/class/csse 220/current/Projects/Vector. Graphics/teams. html And find your

Vector. Graphics project Visit http: //www. rosehulman. edu/class/csse 220/current/Projects/Vector. Graphics/teams. html And find your teammates. Sit with them. Introduce yourselves:

 With your teammates, continue your Swing. Demo project, helping everyone reach the same

With your teammates, continue your Swing. Demo project, helping everyone reach the same point in the project (pair program, with the teammates furthest along being the observer)

What is good objectoriented design? It starts with good classes…

What is good objectoriented design? It starts with good classes…

Good Classes Typically Often come from nouns in the problem description May… ◦ Represent

Good Classes Typically Often come from nouns in the problem description May… ◦ Represent single concepts Circle, Investment ◦ Be abstractions of real-life entities Bank. Account, Tic. Tac. Toe. Board ◦ Be actors Scanner, Circle. Viewer ◦ Be utilities Math Q 2

What Stinks? Bad Class Smells Can’t tell what it does from its name ◦

What Stinks? Bad Class Smells Can’t tell what it does from its name ◦ Pay. Check. Program Turning a single action into a class ◦ Compute. Paycheck Name isn’t a noun ◦ Interpolate, Spend Q 3

Analyzing Quality of Class Design Cohesion Coupling

Analyzing Quality of Class Design Cohesion Coupling

Cohesion A class should represent a single concept Public methods and constants should be

Cohesion A class should represent a single concept Public methods and constants should be cohesive Which is more cohesive? Cash. Register double NICKEL_VALUE double DIME_VALUE double QUARTER_VALUE void add(Array. List<Coin> coins) … void add(int nickels, int dimes, int quarters) … Coin double get. Value() Q 4

Dependency Relationship When one classes requires another class to do its job, the first

Dependency Relationship When one classes requires another class to do its job, the first class depends on the second Shown on UML diagrams as: ◦ dashed line ◦ with open arrowhead Cash. Register void add(Array. List<Coin> coins) … Coin double get. Value() Q 5

Coupling Lots of dependencies == high coupling Few dependencies == low coupling Which is

Coupling Lots of dependencies == high coupling Few dependencies == low coupling Which is better? Why? Q 6

Quality Class Designs High cohesion Low coupling

Quality Class Designs High cohesion Low coupling

Accessors and Mutators Review Accessor method: accesses information without changing any Mutator method: modifies

Accessors and Mutators Review Accessor method: accesses information without changing any Mutator method: modifies the object on which it is invoked Q 7

Immutable Classes Accessor methods are very predictable ◦ Easy to reason about! Immutable classes:

Immutable Classes Accessor methods are very predictable ◦ Easy to reason about! Immutable classes: ◦ Have only accessor methods ◦ No mutators Examples: String, Double Is Rectangle immutable?

Immutable Class Benefits Easier to reason about, less to go wrong Can pass around

Immutable Class Benefits Easier to reason about, less to go wrong Can pass around instances “fearlessly” Q 8

Side Effects Side effect: any modification of data Method side effect: any modification of

Side Effects Side effect: any modification of data Method side effect: any modification of data visible outside the method ◦ Mutator methods: side effect on implicit parameter ◦ Can also have side effects on other parameters: public void transfer(double amt, Account other) { this. balance -= amt; other. balance += amt; } Avoid this if you can! Document it if you can’t

Documenting Side Effects /** * Transfers the given amount from this * account to

Documenting Side Effects /** * Transfers the given amount from this * account to the other account. Mutates * this account and other. * * @param amt * amount to be transferred * @param other * receiving account (mutated) * */ public void transfer(double amt, Account other) { this. balance -= amt; other. balance += amt; }

Class Design Exercise Suppose you were going to implement a program to let two

Class Design Exercise Suppose you were going to implement a program to let two people play chess against each other. Think about what classes you would need. (Search on-line to find the rules of chess if you aren’t familiar with the game. ) List all the classes that you can think of that might be useful in implementing your program. For now you can assume that users will enter moves in the console, but you’ll display the board in a graphics window.

Class design exercise ◦ From your list of potential classes, decide which ones you

Class design exercise ◦ From your list of potential classes, decide which ones you would use in an actual implementation. Pay particular attention to the rules for good classes that we discussed in class. Sketch a UML class diagram for these classes. Show the public interface (including the arguments of the methods) of each class and the dependency relationships between the classes. Recall that dependency relationships are indicated by dashed lines with open arrow heads. ◦ Write a couple of paragraphs explaining why you chose the classes that you did for your design. Discuss the cohesion and coupling of your classes.

Vector Graphics Show movie See features in instructions Make a Screen Layout sketch, per

Vector Graphics Show movie See features in instructions Make a Screen Layout sketch, per instructions