01.02.2013 Views

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Going from multiple responsibilies to a single responsibility<br />

Once you’ve done an analysis, you can take all the methods that<br />

don’t make sense on a class, and move those methods to classes<br />

that do make sense for that particular responsibility.<br />

We used our analysis<br />

to figure out that<br />

these four methods<br />

really aren’t the<br />

responsibility <strong>of</strong><br />

Automobile.<br />

Now Automobile<br />

has only a single<br />

responsibility:<br />

dealing with its<br />

own basic functions.<br />

Automobile<br />

+ start() :void<br />

+ stop() :void<br />

+ changeTires(tires : Tire[]) :void<br />

+ drive() :void<br />

+ wash() :void<br />

+ checkOil() :void<br />

+ getOil() :int<br />

Automobile<br />

+ start() :void<br />

+ stop() :void<br />

+ getOil() :int<br />

Q: How does SRP analysis work when a method takes<br />

parameters, like wash(Automobile) on the CarWash class?<br />

A: Good question! For your SRP analysis to make any sense, you<br />

need to include the parameter <strong>of</strong> the method in the method blank. So<br />

you would write “The CarWash washes [an] automobile itself.” That<br />

method makes sense (with the Automobile parameter), so it<br />

would stay on the CarWash class.<br />

Download at WoweBook.Com<br />

Driver<br />

+ drive(a : Automobile) :void<br />

CarWash<br />

+ wash(a : Automobile) :void<br />

Mechanic<br />

good-enough design<br />

It’s a driver’s<br />

responsibility to drive<br />

the car, not the<br />

automobile itself.<br />

+ changeTires(a : Automobile, tires : Tires[]) :void<br />

A CarWash<br />

class can<br />

handle washing<br />

an automobile.<br />

A mechanic is responsible for<br />

changing tires and checking<br />

the oil on an automobile.<br />

Q: But what if CarWash took in an Automobile parameter<br />

as part <strong>of</strong> its constructor, and the method was just wash()?<br />

Wouldn’t SRP analysis give you a wrong result?<br />

A: It would. If a parameter that might cause a method to make<br />

sense, like an Automobile for the wash() method on<br />

CarWash, is passed into a class’s constructor, your SRP analysis<br />

might be misleading. But that’s why you always need to apply a good<br />

amount <strong>of</strong> your own common sense and knowledge <strong>of</strong> the system in<br />

addition to what you learn from the SRP analysis.<br />

you are here 4 159

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!