Unformatted text preview:

Encapsula on COMP 401 Spring 2013 Lecture 04 1 22 2013 Picking up from last me Abstrac on De ning a class to represent an abstrac on Instance elds hold object state Instance methods de ne 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 elds from being put into an inconsistent or erroneous state Avoids situa on in which external code is dependent on this speci c 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 de ned 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 elds private Amend eld declara on with private access modi er 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 Bene ts Can support read only elds by NOT providing a seVer SeVer can validate new value to prevent misuse or illegal values Can de ne derived or complex proper es that are actually related to mul ple eld 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 e ect 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 Point for comparison setA setB and setC in Triangle validate by making sure that points are dis nct checking for co linearity Added area method Added check colinearity method No ce that I ve chosen a speci c precision for the check based on area Derived Proper es Property that is a combina on or transforma on of object state elds 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 Read only 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 eld as private prevents access from code outside of the class But no ce that there is no dis nc on about access between di erent 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 Re wrote distanceTo and equals using geVers for x and y values Encapsula on In Prac ce Part 2 Separate Exposed Behavior De ne 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 le Should have same name as le 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 modi er as in class ClassName implements InterfaceA InferfaceB Interface Naming Conven ons Interface name must be di erent 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 di eren 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 speci c class that implements Point No ce that Triangle only interacts with interface not any speci c implementa on Advantage of Encapsula on Can provide di erent implementa ons of the same behavior lec4 ex1 v7 Create a new implementa on of Point based on polar coordinates Exposed vs Internal Behavior Exposed behavior should be re ected in the interface s that a class implements Recall that any method declared in interface must be de ned by an implemen ng class as a public method Internal behavior should be hidden Use private modi er on these methods to ensure that access only occurs within the class lec4 ex1 v8 Con nued applica on of encapsula on principle to Triangle by de ning Triangle as an interface rewri ng what used to be the class Triangle as the class PointTriangle that implements the interface hiding internal behaviors as private methods


View Full Document

UNC-Chapel Hill COMP 401 - comp401sp13lec04Encapsulation

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Loading Unlocking...
Login

Join to view comp401sp13lec04Encapsulation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view comp401sp13lec04Encapsulation and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?