chapter 13 encapsulation. prof. dr. debora weber-wulff encapsulation good design separates use from...

14
Chapter 13 Encapsulation

Upload: benedict-barnett

Post on 14-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

Chapter 13Encapsulation

Page 2: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

Prof. Dr. Debora Weber-Wulff

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

encapsulationisa

package

Page 3: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

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!

COPYDon´t use the copy button!

Page 4: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

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

Page 5: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

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.

Page 6: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

Prof. Dr. Debora Weber-Wulff

A method whose function is not succinctly describable is probably not a good method.

KeepItSimpleStupid

Page 7: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

Prof. Dr. Debora Weber-Wulff

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.

bbbjhzt

jgjhg

hkj

jgjhgj

uznbk

ghlkö

jhgkjä

kkjhanbbk kk

kjbkjkjkk

dgkjdkd ggg

dkgölddöd

dökdölkg

dölk ödlf

dlö ökgk

dfk ölkg gk

döfö

flgö

dgldgdd

SMASH!

Page 8: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

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!

Page 9: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

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;

}

Jessica
mehrere Folien übereinander.Wie anordnen?
Page 10: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

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 / librariespackage java.lang;But any visible name accessable by

java.lang.Stringimport cs101.util.*;is just a short form indicator Not real encapsulation No hierarchy

Page 11: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

Prof. Dr. Debora Weber-Wulff

Protecting Internal Structure (cont.) • inheritance

hidden behavior through subclassing labeling with superclass hides membersBut inheritance can break encapsulation!

• interfaceshas no implementation, but defines what has to exist.But implementator can circumvent this!

Page 12: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

Prof. Dr. Debora Weber-Wulff

Inner Classes IAn 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

Page 13: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

Prof. Dr. Debora Weber-Wulff

Inner Classes IIMember classes

- not static- one inner class per instance of the

outerclass- priveledged access to private data:

Check / Checking Account- use new Instance.InnerClass()

Page 14: Chapter 13 Encapsulation. Prof. Dr. Debora Weber-Wulff Encapsulation Good design separates use from implementation. Details are kept hidden from users

Prof. Dr. Debora Weber-Wulff

Inner Classes IIILocal 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