Encapsula)on+COMP+401,+Spring+2013+Lecture+04+1/22/2013+Picking+up+from+last+)me…+• Abstrac)on+– Defining+a+class+to+represent+an+abstrac)on+– Instance+fields+hold+object+state+– Instance+methods+define+func)ons+/+procedures+associated+with+the+abstrac)on+Mo)va)ng+Encapsula)on+• Consider+lec4.ex1.v1+• What’s+the+danger?+Principle+of+Encapsula)on+• Do+not+expose+the+internal+state+of+an+object+directly.+– Protects+object+fields+from+being+put+into+an+inconsistent+or+erroneous+state.+– Avoids+situa)on+in+which+external+code+is+dependent+on+this+specific+implementa)on.+• Or+said+another+way:+allows+for+implementa)on+of+abstrac)on+to+be+improved/changed+without+breaking+other+code.+• Separate+“exposed”+behavior+from+“internal”+behavior+– Exposed+behavior+• Procedures+/+func)ons+other+objects+/+code+interacts+with.+– Internal+behavior+• Procedures+/+func)ons+defined+only+for+use+by+methods+that+are+part+of+the+class.+Encapsula)on+In+Prac)ce+Part+1:+Do+Not+Expose+Internal+State+• Make+all+fields+private++– Amend+field+declara)on+with+“private”+access+modifier.+• Provide+methods+that+retrieve+and/or+alter+proper)es+– Methods+that+retrieves+a+property+is+called+a+“geVer”.+– Methods+that+set+a+property+is+called+a+“seVer”+• Benefits+– Can+support+“readXonly”+fields+by+NOT+providing+a+seVer+– SeVer+can+validate+new+value+to+prevent+misuse+or+illegal+values.+– Can+define+derived+or+complex+proper)es+that+are+actually+related+to+mul)ple+field+values.+JavaBeans+Conven)ons+• JavaBeans+– So[ware+engineering+framework+• Associated+tools+– Relies+on+code+following+certain+conven)ons+• In+par)cular,+geVers+and+seVers+for+object+proper)es.+• Given+type+T+and+property+P:+– Signature+of+a+geVer:+public T getP()!– Signature+of+a+seVer:+public void setP(T value)!lec4.ex1.v2+• Provides+geVers+for+x+and+y+values+of+a+Point,+but+not+seVers.+– Ensures+Point+is+immutable+• Provides+geVers+and+seVers+for+point+of+a+Triangle+• No)ce+effect+on+original+code+in+main+method+in+Lec4Ex1.java+SeVer+Valida)on+• SeVers+should+validate+their+values+if+possible.+– One+of+the+advantages+of+providing+access+to+proper)es+only+through+methods.+• Illegal+/+improper+values+should+cause+a+run)me+excep)on+like+this:+throw new RuntimeException(“Explanation string”);!+lec4.ex1.v3+• Adds+equals+method+to+Poin t+for+comp arison. +• setA(),+setB(),+and+setC()+in+Triangle+validate+by…+– making+sure+that+points+are+dis)nct+– checking+for+coXlinearity+• Added+area()+method+• Added+check_colinearity()+method+– No)ce+that+I’ve+chosen+a+specific+precision+for+the+check+based+on+area.+Derived+Proper)es+• Property+that+is+a+combina)on+or+transforma)on+of+object+state+fields.+– Can+you+recognize+two+of+these+already+in+Triangle?+• Same+principle+for+geVers+and+seVers+applies+here.+– If+using+JavaBeans+conven)ons,+name+methods+with+proper+form+and+signature.+– ReadXonly+proper)es+should+not+have+a+seVer.+– SeVers+should+validate+if+necessary.+lec4.ex1.v4+• Changed+area()+and+perimeter()+to+getArea()+and+getPerimeter()+to+follow+JavaBeans+conven)ons.+– What+about+individual+side+lengths?+• Could+have+done+the+same,+but+didn’t+to+make+another+point+later+on.+• Created+getPoints()+and+setPoints()+as+derived+proper)es+for+dealing+with+all+three+points+at+once+as+an+array.+Using+Fields+Internally+• Marking+a+field+as+“private”+prevents+access+from+code+outside+of+the+class.+– But+no)ce+that+there+is+no+dis)nc)on+about+access+between+different+instances.+– Look+at+distanceTo()+and+equals()+methods+in+Point+• Does+this+violate+principle+of+encapsula)on?+– Gray+area+• Could+argue+no+since+code+is+within+the+class.+• Could+argue+yes+since+access+to+other+point’s+state+is+outside+the+context+of+the+this+reference.+– My+advice+• Always+safe+to+use+exposed+geVer+/+seVer,+so+do+so.+• There+are+some)mes+good+reasons+not+to,+but+generally+these+are+related+to+issues+of+performance+and+op)miza)on.+lec4.ex1.v5+• ReXwrote+distanceTo()+and+equals()+using+geVers+for+x+and+y+values+Encapsula)on+In+Prac)ce+Part+2:+Separate+Exposed+Behavior+• Define+an+“interface”+for+all+exposed+behavior+– In+Java,+an+interface+is+like+a+contract.+• Indicates+that+a+certain+set+of+public+methods+are+available.+• One+or+more+classes+can+indicate+that+they+implement+the+interface.+– Name+of+interface+can+be+used+as+a+type+name+• Just+like+class+names+are+used+as+type+names.+• Value+of+an+interface+type+variable+can+be+set+to+any+object+that+is+an+instance+of+a+class+that+implements+the+interface.+Interfaces+in+Java+• Like+classes,+should+go+in+their+own+.java+file+– Should+have+same+name+as+file+– Body+of+interface+is+a+just+list+of+method+signatures.+• Implemen)ng+classes+MUST+declare+these+methods+as+public+• Form:+interface InterfaceName {!type method1(parameters);!type method2(parameters);!// etc…!}!• Classes+specify+which+interfaces+they+implement+with+“implements”+modifier+as+in:+class ClassName implements InterfaceA, InferfaceB {!Interface+Naming+Conven)ons+• Interface+name+must+be+different+from+class+names+that+implement+the+interface.+• Conven)on+A+– Start+all+interface+names+with+“I”+for+interface.+• For+example:+ITriangle,+IPoint+– Make+sure+that+class+names+do+not.+• Conven)on+B+– Use+generic+abstrac)on+name+for+interface.+– Make+class+names+descrip)ve+of+implementa)on+• If+no+natural+way+to+do+this,+simply+append+“Impl”+to+generic+abstrac)on+name+to+differen)ate.+• Personally,+I+generally+go+with+conven)on+B.+lec4.ex1.v6+• Separates+Point+into+an+interface+and+an+implemen)ng+class.+– No)ce+that+distanceTo()+and+equals()+are+part+of+behavior+I+want+the+abstrac)on+to+expose.+• No)ce+that+main+method+uses+variables+with+type+Point,+but+that+actual+object+is+of+specific+class+that+implements+Point+• No)ce+that+Triangle+only+interacts+with+interface+(not+any+specific+implementa)on)+Advantage+of+Encapsula)on+• Can+provide+different+implementa)ons+of+the+same+behavior+– lec4.ex1.v7+•
View Full Document