180 likes | 808 Views
Chapter 13 Encapsulation. Encapsulation. Good design separates use from implementation . Details are kept hidden from users. We drive cars without knowing how spark plugs work. This gives us flexibility to change an implementation. encapsulation is a package. Procedural abstraction.
E N D
Encapsulation • Good design separates use from implementation. • Details are kept hidden from users. • We drive cars without knowing how spark plugs work. • This gives us flexibility to change an implementation. encapsulation is a package Prof. Dr. Debora Weber-Wulff
Procedural abstraction • Find (almost) common code • use an abstraction, with parameters if necessary • if the code is longer than a page (~50 lines) break it up into bits! Don´t use the copy button! COPY Prof. Dr. Debora Weber-Wulff
Rules of Thumb • Succinct description of what not how • Fit on a single page • needed more than one place • make a new method Prof. Dr. Debora Weber-Wulff
The Description Rule of Thumb • Each method should have a well – defined purpose. • Each purpose in your program should have its own method. • The user needs to be able to understand what your method does, not have to understand how it works. • There can be multiple methods per description, for example with different parameters. • Succinct description make your code more readable and easier to maintain. Prof. Dr. Debora Weber-Wulff
A method whose function is not succinctly describable is probably not a good method. • Keep • It • Simple • Stupid Prof. Dr. Debora Weber-Wulff
SMASH! bbbjhzt jgjhg hkj jgjhgj uznbk ghlkö jhgkjä kk jhanbbk kk kjbkjkjkk dgkjdkd ggg dkgölddöd dökdölkg dölk ödlf dlö ökgk dfk ölkg gk döfö flgö d gldgdd The Length Rule of Thumb • 1 method ≈ 1 page • (better: 1 screen!) • If it gets larger: describe the work in an outline and break it up! • Don´t worry about inefficiency! • A good compiler (i. e. one that costs money) can optimize it. Prof. Dr. Debora Weber-Wulff
The Repitition Rule of Thumb • If the same code appears twice (or more often!): abstract or encapsulate it! • Inside a class, this method is usually private, but can be public if it turns out to be useful. • Removing redundancy shortens programs! 1 place instead of 4! Prof. Dr. Debora Weber-Wulff
if (theValue < 0) { • System.out.println ("the value is: " + theValue); • global Variable ++; • theAnswer = ((-someConstant) * theValue%2); • } else if (theValue > 0) { • System.out.println ("the value is: " + theValue); • global Variable ++; • theAnswer = ((someConstant) * theValue%2); • } else { • System.out.println ("the value is: " + theValue); • global Variable ++; • theAnswer = 0; • } ??? Prof. Dr. Debora Weber-Wulff
Protecting Internal Structure • private • !An object can call the private method of any other instance of the same class! • packages / libraries • package java.lang; • But any visible name accessable by • java.lang.String • import cs101.util.*; • is just a short form indicator • Not real encapsulation • No hierarchy Prof. Dr. Debora Weber-Wulff
Protecting Internal Structure (cont.) • inheritance • hidden behavior through subclassing • labeling with superclass hides members • But inheritance can break encapsulation! • interfaces • has no implementation, but defines what has to exist. • But implementator can circumvent this! Prof. Dr. Debora Weber-Wulff
Inner Classes I • An inner class is a class defined inside another. • Static classes • - top – level inside another class • - use new OuterClass.InnerClass() as the constructor • - keyword static • - has access to static members of the containing class Prof. Dr. Debora Weber-Wulff
Inner Classes II • Member classes • - not static • - one inner class per instance of the outerclass • - priveledged access to private data: • Check / Checking Account • - use new Instance.InnerClass() Prof. Dr. Debora Weber-Wulff
Inner Classes III • Local classes • - statement, not a member • - defined in a block • - one class per execution • - can see lots of interesting fields • - DON´T BOTHER with this now • Anonymous classes • - only good for one instance • - new TypeName {<members>} • - good for event handling Prof. Dr. Debora Weber-Wulff