Recitation 9

Download Report

Transcript Recitation 9

Recitation 9
October 29, 2010
Today’s Goals:
 Practice with abstract classes
 Create an abstract factory method
 Use the built in List interface and
implementing classes
 Vector
 ArrayList
Abstract classes
 You should be familiar with abstract classes from
lecture
 Abstract classes contain methods that have not yet
been defined
 These methods will later be defined by subclasses
public abstract class ACourse {
String title, dept;
…
public String getTitle() {
return title;
}
public String getDepartment() {
return dept;
}
abstract public int getNumber();
}
Abstract Method
(no body!)
Abstract Factory Method
 An abstract factory method is a method that will
instantiate an object. It’s concrete type is not
defined in the abstract class. For example:
 abstract Car createCar();
 In a subclass, this method may be realized
with different types:




return new ScionXB();
return new HondaFit();
…
return new ToyotaMatrix();
Abstract Factory Method
…
abstract <T> <M>(…) ;
…
extends
…
…
<T> <M>(…) {
<T> <M> (…){
return new <C1> (…)
return new <C2> (…)
}
}
…
…
Abstract method that returns an instance of some type
T – like a factory, it creates and initializes an object.
Abstract Factory Method
CarFactory
…
abstract Car createCar(Color color) ;
…
ToyotaFactory extends CarFactory
HondaFactory extends CarFactory
…
…
Car createCar(Color color) {
Car createCar(Color color) {
return new ScionXB(color);
return new HondaFit(color);
}
}
…
…
The subclasses instantiate different concrete classes with their Factory method - but
they both are instances of a common type: Car, defined in the abstract factory method.
Built-in collections
 You’ve written several variable-sized
collections for different objects in this
course.
 Java actually has such collections built-in!
 import java.util.____; (List, Vector, ArrayList)
 Examples of built-in collections in Java
include ArrayLists and Vectors, which both
implement the List interface
Creating and Using Lists
 How to create a basic List:
 List myList1= new ArrayList();
 List myList2= new Vector();
 Adding something to a List:
 myList.add(element)
 Accessing something in a List (at location i):
 String myElement = (String) myList.get(i);
 We have to cast since this List holds generic Objects
 Determining the size of the List
 int cursize = myList.size();
Recitation Specification
 Goal: We’ve created two implementations of the
StringHistory interface, and want you to refactor to
remove any duplicate code.
 Copy the following files into your project:
 StringHistory, ArrayListStringHistory, VectorStringHistory
 Refactor the existing implementations by creating a new
abstract class named AbstractStringHistory
 Extract out all duplicate code (including constructors!) from
ArrayListStringHistory and VectorStringHistory
 Requirement: any instance variable(s) must be private
 Hint: You’ll need to use an abstract factory method!
 Create a main class to test both types of StringHistory
String History Interface
public interface StringHistory {
public void addElement(String element);
public String elementAt(int index);
public int size();
}
VectorStringHistory
public class VectorStringHistory implements StringHistory{
private List history;
public VectorStringHistory (){
history = new Vector();
}
public void addElement(String element) {
history.add(element);
}
public String elementAt(int index) {
return (String)history.get(index);
}
public int size() {
return history.size();
}
}
ArrayListStringHistory
public class ArrayListStringHistory implements StringHistory{
private List history;
public ArrayListStringHistory(){
history = new ArrayList();
}
public void addElement(String element) {
history.add(element);
}
public String elementAt(int index) {
return (String)history.get(index);
}
public int size() {
return history.size();
}
}
Setup and Submission
 Please follow the instructions on the course
website to set up and submit your projects!
 Set up your project:
 http://www.cs.unc.edu/~dewan/comp114/f10/Recitations/mak
eproject/
 Submit an assignment:
 http://www.cs.unc.edu/~dewan/comp114/f10/Recitations/sub
mit/
 Work with a single partner!