Review 115 - Relationship ADT->Class->Object ADT is abstract object, consists of values and ops on those values. Class is formalization in a programming lanugage Object is instance of class runtime OO program is a system of objects communicating with each other - Examine a Computer. What is an example of a Property? Behavior? Disk Size; CPU speed; Boot up, shut down. Access Designations : - public/private/protected/package which is default? protected (also allows package access) - when should a property be declared public/private/protected? - accessor and mutator methods. Why use these rather than declaring public? Control/checking. - when should a method be declared public/private/protected? (constructor calling setName, subclass changes, recursive methods private) Mutability Designations : - why declare a variable static? - only one copy necessary for all instances. Beware, however, that this can lead to unintended consequences. - why declare a method static? - no particular class it should attach to. Math.sqrt(4,2) library functions. - what does it mean do declare something final? When would you do this? Constants. Inheritance : - Inheritance. What is it? Why is it useful? - Computer c = new Laptop() is legal; Laptop l = new Computer() is not. - how do you access an overridden method in an inherited class? super(); - encapsulation (information hiding) - abstraction (what not how) Interfaces : - difference between a class and an interface? interface does not have any instantiation, only a footprint. - why use interfaces? - abstract class/interface; when to use each? - Do a UML diagram of the class Relationships has-a (composition) - Computer has-a graphics card GraphicsCard card = new NVidia8000() knows-a (association) - Computer knows-a network Network n; public void setNetwork(Network newNet); uses-a (dependency) - Computer uses-a login checkLogin(new Login(bob, secret)); is-a (inheritance/generalization) - Computer is-a electronic device (extends) implements (realization) - Computer implements CalculatingMachine (implements) Design Patterns command pattern Encapsulate a behavior in a class. * do/undo - if you have classes, you can keep an array/vector of transactions, makes it easier to push/pop commands. * logging - you can just pass the "command" to the logger. factory pattern Create objects abstracted from class. * operating system from computer example - instantiate different class based on context. * Only need to know the interface at compile time * Makes useful naming possible (createOracleConnection) iterator pattern Examine a set of values in an ADT. * StringTokenizer, enumeration. Hides detail of traversal; might be list, might be tree. null object Zero. * ref to iterator, avoid null pointer exception singleton Only one (or discreet number) of an object, saves storage. getConnection() - for single user, only one connection, provides control state uses a class to represent state, context provides api, like strategy but for intent. The State pattern allows an object to change its behavior when its internal state changes. This pattern can be observed in a vending machine. Vending machines have states based on the inventory, amount of currency deposited, the ability to make change, the item selected, etc. When currency is deposited and a selection is made, a vending machine will either deliver a product and no change, deliver a product and change, deliver no product due to insufficient currency on deposit, or deliver no product due to inventory depletion. strategy different algorithm depending on strategy object, context provides api. * pass in a algo, and an adt. comp = new countrycompbyname; Collections.sort(countries, comp) * borders in swing. * avoids having to create many subclasses, or having to create ifs.