12
LECTURE 28: PRINCIPLE OF LEAST KNOWLEDGE Computer Science 313 – Advanced Programming Topics

Lecture 28: Principle of Least Knowledge

  • Upload
    hani

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

Computer Science 313 – Advanced Programming Topics. Lecture 28: Principle of Least Knowledge. Public Fields are Evil. Always make fields private (or protected) Cannot control field ’s uses Can be used by any class Modified anywhere in program Cannot track any of this - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 28: Principle of Least Knowledge

LECTURE 28:PRINCIPLE OF LEAST KNOWLEDGE

Computer Science 313 – Advanced Programming Topics

Page 2: Lecture 28: Principle of Least Knowledge

Public Fields are Evil

Always make fields private (or protected)

Cannot control field’s uses Can be used by any class Modified anywhere in program Cannot track any of this

Makes future changes difficult Cannot remove or replace field Code may rely on its exact type May not be able to change bases

Page 3: Lecture 28: Principle of Least Knowledge

Getters Can Be Evil

After calling getter: Cannot control field’s uses

Can be used by any class Modified anywhere in program Cannot track any of this

Makes future changes difficult Cannot remove or replace field Code may rely on its exact type May not be able to change bases

Page 4: Lecture 28: Principle of Least Knowledge

Coupling

“Measure” of how independent classes are Classes using own fields & methods are

independent Changes to independent classes are simple

Any change to dependent class causes ripples Consider all classes dependent on one

being changed May affect classes dependent on those

classes Completely independent classes

unrealistic One class for entire program required to do

this Instead focus on how to minimize coupling

Page 5: Lecture 28: Principle of Least Knowledge

Principle of Least Knowledge More commonly called Law of

Demeter (Stupid name that does not really mean

anything) Only call methods belonging to:

Current object (e.g., this) Current object’s fields Object allocated within the method One of the parameters to the method

May not limit dependent classes But certainly makes tracking them down

easier

Page 6: Lecture 28: Principle of Least Knowledge

Violating the Principle

Fixing something, but what is problem? Using getters, fields can travel

everywhere Dependent classes hard to find & count Most common Java violation of this rule:

System.err.print(“Bar);System.out.println(“Foo”);

Would this code really be any better?System.getErr().print(“Bar”);

System.getOut().println(“Foo”);

Page 7: Lecture 28: Principle of Least Knowledge

Getters are not the Solution Getter could make finding uses harder!public void printOutEntireDictionary(Dict d) {

PrintWriter e = System.getErr();PrintWriter o = System.getOut();PrintWriter f = new PrintWriter(new File…));for (Entry<String,String> ent : d) { f.println(ent.getKey()+ent.getValue()); e.println(ent.getKey()+“\t”+ent.getValue()); o.println(ent.getKey()+“”+ent.getValue()); translateToGerman(ent, f); translateToSpanish(ent, o); translateToSpanish(ent, e);

Page 8: Lecture 28: Principle of Least Knowledge

What Can Be Done?

public void buildPool(Store s) { Fish attack = null; try { attack = s.getSharks(); attack.addLasers(); } catch (Exception e) { attack = s.getSeaBass(); attack.mutate(); attack.makeIllTempered(); } pool.populate(attack);}

Page 9: Lecture 28: Principle of Least Knowledge

What Can Be Done?

public void buildPool(Store s) { Fish attack = null; try { attack = s.getSharks(); attack.addLasers(); } catch (Exception e) { attack = s.getSeaBass(); attack.mutate(); attack.makeIllTempered(); } pool.populate(attack);}

Page 10: Lecture 28: Principle of Least Knowledge

Ugly Solution

public void armFish(Fish f) { f.addLasers();}public void buildPool(Store s) { Fish attack = null; try { attack = s.getSharks(); armFish(attack); } catch (Exception e) { attack = s.getSeaBass(); : :

Page 11: Lecture 28: Principle of Least Knowledge

Prettier Façade Solution

public void buildPool(EvilStore s) { Fish attack = null; try { attack = s.getSharksWithLasers(); } catch (Exception e) { attack = s.getITMutatedSeaBass(); } pool.populate(attack);}

Page 12: Lecture 28: Principle of Least Knowledge

For Next Class

Keep working on Lab #6 due on Wednesday

Read pages 275-286 for Monday Discusses another very useful design

pattern Should have used last CSC213, if you

had known Overlaps with other patterns, which are

similar? Next set of pattern reports due after

break To be submitted day we return: Monday,

April 12th

Worth 12% of final grade, so do a good job