Adapter Pattern, Public Interface Set Interface, Public Class Person | CSI 3471, Study notes of Software Engineering

Material Type: Notes; Professor: Grabow; Class: Software Engineering I; Subject: Computer Science; University: Baylor University; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/17/2009

koofers-user-fux
koofers-user-fux 🇺🇸

9 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 of 6
Adapter Pattern
package adapterprj;
import java.util.*;
public interface SetInterface {
public boolean insert( Object anObject );
public boolean remove( Object anObject );
public boolean isIn(Object anObject);
public boolean isEmpty();
public void clear();
public int count();
public String toString();
public Iterator iterator();
}
package adapterprj;
import java.util.*;
public class PersonSet
implements SetInterface {
TreeSet theSet;
// **************************************
/**
*
*/
public PersonSet() {
theSet = new TreeSet();
}
// **************************************
/**
*
* @param anObject Object
* @return boolean
*/
public boolean insert(Object anObject) {
// Add element only if element not null
if (anObject instanceof Person) {
Person aPerson = (Person) anObject;
if (!aPerson.isNull()) {
return theSet.add(aPerson);
}
else {
return false;
}
}
else {
return false;
}
pf3
pf4
pf5

Partial preview of the text

Download Adapter Pattern, Public Interface Set Interface, Public Class Person | CSI 3471 and more Study notes Software Engineering in PDF only on Docsity!

Adapter Pattern

package adapterprj; import java.util.*;

public interface SetInterface {

public boolean insert( Object anObject ); public boolean remove( Object anObject ); public boolean isIn(Object anObject); public boolean isEmpty(); public void clear(); public int count(); public String toString(); public Iterator iterator(); }

package adapterprj;

import java.util.*;

public class PersonSet implements SetInterface {

TreeSet theSet;

// ************************************** /**

*/ public PersonSet() { theSet = new TreeSet(); }

  • @param anObject Object
  • @return boolean */ public boolean insert(Object anObject) { // Add element only if element not null if (anObject instanceof Person) { Person aPerson = (Person) anObject;

if (!aPerson.isNull()) { return theSet.add(aPerson); } else { return false; } } else { return false; }

  • @param anObject Object
  • @return boolean */ public boolean remove(Object anObject) { // Add element only if element not null if (anObject instanceof Person) { Person aPerson = (Person) anObject;

// Remove a particular element from set return theSet.remove(aPerson); } else { return false; } }

// ************************************** /**

  • @param anObject Object
  • @return boolean */ public boolean isIn(Object anObject) { // Add element only if element not null if (anObject instanceof Person) { Person aPerson = (Person) anObject;

return theSet.contains(aPerson); } else { return false; } }

// ************************************** /**

  • @return boolean */ public boolean isEmpty() { return theSet.isEmpty(); }

// ************************************** /**

*/ public void clear() { // Remove all elements of set theSet.clear(); }

String tempLast = Common.normalize(aLastname); if (Common.isValidName(tempFirst) && Common.isValidName(tempLast)) { m_firstname = tempFirst; m_lastname = tempLast; } else { m_firstname = Common.NULL_STRING; m_lastname = Common.NULL_STRING; } }

// Extractors // *************************************************** /**

  • Get the first name
  • @return a String representing first name */ public String getFirstname() { return m_firstname; }
  • Get the last name
  • @return a String representing last name */ public String getLastname() { return m_lastname; }

/**

  • Is firstname and lastname an empty string? */ public boolean isNull() { boolean result; result = (m_firstname == Common.NULL_STRING) && (m_lastname == Common.NULL_STRING); return result; }

// Relational operators // *************************************************** /**

  • Determine equality based on both first and last names
  • @param aPerson
  • @return boolean */ public boolean isEqual(Person aPerson) { return (compareTo(aPerson) == 0); }

/**

  • If last names are equal, then base comparison solely on
  • the first names
  • @param aPerson
  • @return boolean */

public boolean isLess(Person aPerson) { return (compareTo(aPerson) < 0); }

  • If last names are equal, then base comparison solely on
  • the first names
  • @param aPerson
  • @return boolean */ public boolean isGreater(Person aPerson) { return (compareTo(aPerson) > 0); }
  • @return firstname + lastname */ public String toString() { return (m_firstname + " " + m_lastname); }
  • Same as isEqual
  • @param aPerson
  • @return boolean */ public boolean equals(Person aPerson) { return (compareTo(aPerson) == 0); }
  • *

    less than: negative integer; * equal: zero; * greater than: positive integer<\P> * @param anObject * @return int */ public int compareTo(Object anObject) { Person aPerson = (Person) anObject; int lastCmp = m_lastname.compareToIgnoreCase(aPerson.m_lastname);

if (lastCmp == 0) { return m_firstname.compareToIgnoreCase(aPerson.m_firstname); } else { return lastCmp; } } }

package adapterprj;

public class Common {

// Operations and data that are common to all of the other classes