UVA CS 4501 - Tastings- Course 1 (6 pages)

Previewing pages 1, 2 of 6 page document View the full content.
View Full Document

Tastings- Course 1



Previewing pages 1, 2 of actual document.

View the full content.
View Full Document
View Full Document

Tastings- Course 1

62 views


Pages:
6
School:
University Of Virginia
Course:
Cs 4501 - Special Topics in Computer Science
Unformatted text preview:

CS 494 Adv SW Design and Development Reading Assignment Read for understanding code if it helps Basic Java program structure Classes and files importing packages main method Types in Java Primitive vs class class hierarchy class Object Boxing References Defining classes A Tasting Course 1 Design patterns Intro example Course 2 Inheritance Interfaces OO Design In Just Java 2 Chapters 1 5 1 20 05 2001 T Horton A 1 Tastings Course 1 1 20 05 A 2 Idioms Patterns Frameworks Idiom a small language specific pattern or technique Idioms Patterns Frameworks A more primitive building block Design pattern a description of a problem that reoccurs and an outline of an approach to solving that problem Generally domain language independent Also analysis patterns Framework A partially completed design that can be extended to solve a problem in a domain Horizontal vs vertical Example Microsoft s MFC for Windows apps using C 1 20 05 A 3 Examples of C Idioms 1 20 05 A 4 Design Patterns Essential Elements Use of an Init function in constructors Pattern name If there are many constructors make each one call a private function Init Init guarantees all possible attributes are initialized Initialization code in one place despite multiple constructors A vocabulary of patterns is beneficial Problem When to apply the pattern what context How to represent organize components Conditions to be met before using Don t do real work in a constructor Define an Open member function Constructors just do initialization Open called immediately after construction Constructors can t return errors They can throw exceptions Solution Design elements relationships responsibilities collaborations A template for a solution that you implement Consequences Results and trade offs that result from using the pattern Needed to evaluate design alternatives 1 20 05 A 5 1 20 05 A 6 A 1 Patterns Are and Aren t Example 1 Singleton Pattern Global variables are bad Name and description of a proven solution to a problem Documentation of a design decision They re not Why Reusable code class libraries etc At a higher level Do not require complex implementations Always the best solution to a given situation Simply a good thing to do 1 20 05 We are tempted to use global variables Why A 7 1 20 05 A 8 Singleton Java implementation Example 1 Singleton Pattern public class MySingleton private static theInstance new MySingleton private MySingleton constructor Context Only one instance of a class is created Everything in the system that needs this class interacts with that one object Controlling access Make this instance accessible to all clients Solution The class has a static variable called theInstance etc The constructor is made private or protected Clients call a public operation getInstance that returns the one instance This may construct the instance the very first time or be given an initializer 1 20 05 public static MySingleton getInstance return theInstance A 9 1 20 05 A 10 1 20 05 A 12 Static Factory Methods Singleton pattern uses a static factory method Factory something that creates an instance Advantages over a public constructor They have names Example BigInteger int int random vs BigInteger probablePrime Might need more than one constructor with same similar signatures Can return objects of a subtype if needed Wrapper class example Double d1 Double valueOf 3 14 Double d2 new Double 3 14 More info Bloch s Effective Java 1 20 05 A 11 A 2 Tastings Course 2 Java Interfaces Note that the word interface Interfaces and Collections in Java Is a specific term for a language construct Is not the general word for communication boundary Is also a term used in UML but not in C 1 20 05 A 13 Why Use Inheritance A 14 Two Types of Inheritance How can inheritance support reuse Implementation Inheritance Why inherit Create a class that 1 2 3 4 1 20 05 Makes sense in problem domain Locates common implementation in superclass Defines a shared API methods so we can Use polymorphism Define a reference or parameter in terms of the superclass A subclass reuses some implementation from an ancestor In Java keyword extends Interface Inheritance If just last two then use Java interface No shared implementation You commit that part of what defines a class is that it meets a particular API We can write methods etc that operate on objects of any class that meets or supports that interface 1 20 05 A subclass shares the interface with an ancestor In Java keyword implements I e this class will support this set of methods A 15 Interfaces and Abstract Classes 1 20 05 A 16 Interfaces in Other Languages Abstract classes A modeling method in UML Cannot create any instances Prefer Java interfaces over abstract classes Existing classes can add an interface Better support for mix in classes E g Comparable interface supports compare Do not need a hierarchical framework Composition preferred over inheritance E g wrapper classes Interfaces in C All methods are pure virtual No data members Use multiple inheritance But abstract classes have some implementation Skeletal implementation classes e g AbstractCollection Disadvantage once released a public interface shouldn t be updated 1 20 05 A 17 1 20 05 A 18 A 3 Collections in Java Defining an Interface ADT more than one implementation meets same interface models same data In Java separate interface from implementation We ll illustrate with fake Java example Java code interface Queue void add Object obj Object remove int size Nothing about implementation here Queue interface Two implementations methods and no fields 1 20 05 A 19 1 20 05 Using Objects by Interface A 20 Implementing an Interface Example class CircularArrayQueue implements Queue CircularArrayQueue int capacity public void add Object o public Object remove public int size Say we had two implementations Queue q1 new CircularArrayQueue 100 or Queue q1 new LinkedListQueue private Object elements private int head private int tail q1 add new Widget Queue q3 new Queue q2 mergeQueue q2 q3 1 20 05 A 21 Implementing an Interface 2 1 20 05 A 22 Real Collection Interfaces in Java Implementation for LinkedListQueue similar Question How to handle errors All collections meet Collection interface boolean add Object obj Iterator iterator int size boolean isEmpty boolean contains Object obj boolean containsAll Collection other See Java API documentation for all methods Array version is bounded add when full Throw an exception perhaps Not an issue for linked list version though 1 20 05 A


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Tastings- Course 1 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 Tastings- Course 1 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?