\documentclass[10pt]{article}

\usepackage{hyperref}
\usepackage[a4paper, total={6in, 8in}, top=.75in, bottom=1in]{geometry}
\usepackage{graphicx}

\usepackage[normalem]{ulem}
\useunder{\uline}{\ul}{}
\DeclareGraphicsExtensions{.pdf,.png,.jpg}
\usepackage{color}

\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}

\usepackage{listings}
\lstset{language=Java,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily,
  moredelim=[il][\textcolor{pgrey}]{$$},
  moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}
\begin{document}
\title{Object-Oriented Modeling Using UML}
\author{Dr. Yvon Feaster}
\date{}
\maketitle

\section*{Objects and Classes }
In this document we will discuss characteristics of Objects and Classes.  We will discuss UML notations for various OO concepts.  We will discuss a number of important principles in the OO development approach.

\subsection*{Characteristics of Objects and Classes}

\subsubsection*{Object}

\begin{itemize}
	\item has a unique identity, state, and behavior
	\item state is composed of fields or attributes
	\item a field has a name, type, and value 
	\item behavior defined by methods (methods access or manipulate the state of an objects )
	\item values of fields of an object are mutable (changable)
	\item objects can be mutable or immutable (non changable)
	\item mutators are methods that can modify the state of a field (a.k.a., setters)
	\item accessors are methods that do not modify the state of the object (a.k.a, getters)
	\item often referred to as an instance
\end{itemize}


\subsubsection*{Class}

A class characterizes the structure of states and behaviors that are shared by all its instances.\\

\begin{itemize}
	\item features of objects are defined in a class that instantiates the objects.
	\item defines:
		\begin{itemize}
			\item the names and types of all fields
			\item the names, types, and implementations of all methods
		\end{itemize}
	\item values of the of fields are not defined or fixed in the class definition.
	\item values of fields are mutable
	\item each instance of a class has its own state.
	\item implementations of methods are defined in the class definition and are therefore fixed for a given object
	
\end{itemize}
Simple example of a class:

\noindent class Point\\
\{\\
   \indent      int x, y;// fields \\
   \indent      public void move(int dx, int dy); // an attribute or method as we know it\\
\}\\

\subsection*{UML Notation for Classes}
\begin{table}[h]

\label{tab:umlNotation}
\centering
\begin{tabular}{|c|}
\hline
ClassName                                                       \\ \hline
\begin{tabular}[c]{@{}c@{}}field1\\ :\\ field(n)\end{tabular}   \\ \hline
\begin{tabular}[c]{@{}c@{}}method1\\ :\\ method(m)\end{tabular} \\ \hline
\end{tabular}
\caption{UML Class Notation}
\label{tab:umlNotation}
\end{table}


\noindent The UML notation for classes is a rectangular box, as shown in Table~\ref{tab:umlNotation} with as many as three compartments.

\noindent Syntax for fields\\

\noindent Java\\

\noindent [visability] [type] Name [[multiplicity]]  [=Initial value]\\

\noindent UML\\

\noindent [visability] Name [[mulitiplicity]] [:Type] [=Initial value]\\

\noindent Syntax for methods\\

\noindent Java\\

\noindent [visability] [ Type] Name ([Parameter(1), Parameter(2), ....Parameter(n)])\\

\noindent UML\\

\noindent [visability] Name ([Parameter(1), Parameter(2), .... Parameter(n)]) [:Type]\\

\noindent Visability is similar to what you learned in C++

\begin{table}[h]

\label{tab:visability}
\centering
\begin{tabular}{|c|c|c|}
\hline
Visability & Java Syntax & UML Syntax \\ \hline
public     & public      & +          \\ \hline
protected  & protected   & \#         \\ \hline
package    &             & ~          \\ \hline
private    & private     & -     \\ \hline
\end{tabular}
\caption{Visability}
\end{table}
 
\noindent Multiplicity specification of a field specifies whether an object may have multiple occurrences of the field.  \\


\noindent Examples of field and method declarations:\\

\noindent {\em{Field declarations}}\\

\noindent Java Syntax\\

\indent Date birthday\\
\indent public int duration = 100\\
\indent private Student students[0 - MAX\_SIZE]\\

\vspace{2 in}
\noindent UML syntax\\

\indent birthday:Date\\
\indent +duration:int = 100\\
\indent -students[0 - MAX\_SIZE]:Student\\

\noindent {\em{Method declarations}}\\

\noindent Java Syntax\\

\indent void move(int dx, int dy)\\
\indent public int getSize()\\

\noindent UML syntax\\

\indent ~move(dx:int, dy:int)\\
\indent +getSize():int\\

\noindent UML representation of the Point class\\

%\noindent{Java syntax}

\begin{table}[h]
\centering
\label{tab:javaSyntax}
\begin{tabular}{|c|}
\hline
Point                                                                 \\ \hline
\begin{tabular}[c]{@{}c@{}}private int x\\ private int y\end{tabular} \\ \hline
public void move(int dx, int dy)                                      \\ \hline
\end{tabular}
\caption{Java Syntax}
\end{table}

%\noindent{UML syntax}

\begin{table}[h]
\centering

\label{tab:umlSyntax}
\begin{tabular}{|c|}
\hline
Point                                                   \\ \hline
\begin{tabular}[c]{@{}c@{}}-x:int\\ -y:int\end{tabular} \\ \hline
+move(dx:int, dy:int)                                   \\ \hline
\end{tabular}
\caption{UML Syntax}
\end{table}

\subsection*{UML Notation for Objects}

The UML notation for objects is a rectangular box with one or two compartments.

\begin{table}[h]
\centering
\begin{tabular}{|c|}
\hline
{\ul objectName:ClassName}                                                      \\ \hline
\begin{tabular}[c]{@{}c@{}}field1 = value1\\ ...\\ field2 = value2\end{tabular} \\ \hline
\end{tabular}

\vspace {1 cm}
\centering
\label{tab:umlExample}
\begin{tabular}{|c|}
\hline
{\ul p1:Point}                                        \\ \hline
\begin{tabular}[c]{@{}c@{}}x = 0\\ y = 0\end{tabular} \\ \hline
\end{tabular}
\caption{UML Notation for Objects and Example}
\end{table}

The name and class is underlined in the top row of the table with the fields and values listed in the bottom.

\subsection*{Principles of Object-Oriented Development Approach}
This section will touch on several important concepts in OO software development, including:

\begin{itemize}
	\item Modularity
	\item Abstraction
	\item Encapsulation
	\item Ploymorphism
	\item Inheritance
\end{itemize}


\subsubsection*{Modularity}
Modularity is defined by \textit{techopedia} \url{http://www.techopedia.com/definition/24772/modularity} as follows:  ``In softwre engineering, 
modularity refers to the extent to which a software may be divided into smaller modules. .....  Modularity is successful because developers 
use prewritten code, which saves resources.  Overall, modularity provides greater software development manageability. " \\

\noindent They also say,  ``software engineering modularity allows typical applications to be divided into modules, as well as integration with similar modules, which 
helps developers use prewritten code.  Modules are divided based on functionality, and programmers are not involved with the functionalities of
of other modules.  Thus, new functionalities may be easily programmed in separate modules."\\

\noindent Our book defines the Principle of Modularity as follows:\\
A complex software system that should be decomposed into a set of highly cohesive but loosely coupled modules.   What that heck does that mean?  Let's define cohesive and coupled.  
The book says ``\textit{Cohesion} refers to the functional relatedness of the entities within a module."  It says, ``\textit{coupling} refers to the interdependency among different modules."  
So basically, modules (I think of these as classes)  need to be related but individual.  

\noindent Advantages of modularity:
\begin{itemize}
	\item makes software easier to maintain
	\item allows reuse of software
	\item makes testing software easier
\end{itemize}

\noindent  The concept of modularity is not new.  With procedural languages you see it in the form of small routines and functions.  In OO it takes the form of classes and packages.  

\subsubsection*{Abstraction}
Our book says ``abstraction means separating the essential from the nonessential characteristics of an entity.  The result is simpler but sufficiently accurate approximation of the original entity, obtained by removing or ignoring the nonessential characteristics."

\noindent The principle of Abstraction provided in our book is as follows:\\
\noindent ``The behaviors, or functionalities, of a module should be characterized in a succinct and precise description known as the {\em{contractual interface}} of the module.  In other words, the contractual interface captures the essences of the behavior of the module.  The contractual interface is an abstraction of the module."

\noindent Are you sufficiently confused yet? \\

\noindent Here a few examples that may help you understand.

1.  From our book:  Let us consider the telephone.  The mechanism for providing telephone service is a rather complex one.  It involves routing and connecting calls, converting voice to electronic signals and back to voice, etc.  However, telephone users don't need to understand how the phone system work.  They only need to understand how to use the phone, such as dialing, speaking, hanging up.  The user manual in this case is the contractual interface of the telephone service, and it serves as an abstraction of the telephone service from the user's perspective.  

2.  From \url{http://whatis.techtarget.com/definition/abstraction}  Consider yourself as an object.  You are arranging to meet a blind date and are deciding how to describe yourself so you will be recognizable to your date.  You would include information concerning where you will be located, your height, hair color, maybe even what you will be wearing.  This is all data that will help the procedure (your date finding you) work smoothly.  You should include the relevant information necessary.  However there are various bits of information, about you, that is not relevant to this situation since it will not help your date find you, such as:  your SSN, your hobbies, and what  you took to ``show and tell" in the fifth grade.  This information can be abstracted since you do not need to reveal this in this situation.  If the date goes well you may eventually share what you took to ``show and tell".  :-)

3.  From http://programmers.stackexchange.com/questions/230401/confused-about-the-definition-of-abstraction-in-oop.  You as a person have different relationships in different roles. When you are at school, then you are a \textbf{``Student"}. When you are at work, you are an\textbf{``Employee"}. When you are at government institution, you can be viewed as a \textbf{``Citizen"}. So it boils down to in what context are we looking at an entity/object. So if I am modeling a \textbf{Payroll System}, I will look at you as an \textbf{Employee(PRN, Full Time/Part Time, Designation)}. If am modeling a \textbf{Course Enrollment System}, then I will consider your aspects and characteristics as a \textbf{Student(Roll Number, Age, Gender, Course Enrolled)}. And if I am modeling a \textbf{Social Security Information System} then I will look at your details as a \textbf{Citizen(like DOB, Gender, Country Of Birth, etc.)}  Abstraction focuses on relevant or necessary details only.  

\subsubsection*{Encapsulation}
Encapsulation (a.k.a, information hiding) is closely related to abstraction.  Remember Abstraction focusses on only necessary details.   Encapsulation is hiding details from the outer world.  Encapsulation means hiding the details of the object and providing a decent interface for the entities in the outer world to interact with that object or entity.  It reduces coupling among modules.  \\

\noindent Simpler put from \url{http://www.tutorialspoint.com/java/java_encapsulation.htm}, ``encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods.  If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class."  Below is an example depicting encapsulation, also from tutorialspoint.com.


\begin{lstlisting}
/* File name : EncapTest.java */
public class EncapTest{

   private String name;
   private String idNum;
   private int age;

   public int getAge(){
      return age;
   }

   public String getName(){
      return name;
   }

   public String getIdNum(){
      return idNum;
   }

   public void setAge( int newAge){
      age = newAge;
   }

   public void setName(String newName){
      name = newName;
   }

   public void setIdNum( String newId){
      idNum = newId;
   }
}





/* File name : RunEncap.java */
public class RunEncap{

   public static void main(String args[]){
      EncapTest encap = new EncapTest();
      encap.setName("James");
      encap.setAge(20);
      encap.setIdNum("12343ms");

      System.out.print("Name : " + encap.getName()+ 
                             " Age : "+ encap.getAge());
    }
}

output:  Name : James Age : 20
\end{lstlisting}

\noindent The public ``getter" and ``setter" methods are the access points to the class fields from the outside world. \\

\noindent Before we move on, consider the telephone system again.  (This is a continued example from the book.)  ``The telephone service is an example of an application in which the contractual interface and implementation are separated.  In the past, signals were transmitted in analog mode. Over time, telephone services have been upgraded until nowadays, the signals can be transmitted in digital mode with encryption.  Although the implementation of telephone service has changed, the contractual interface  remains the same.  (in other words the user still makes a call the same way)  The only effects on telephone users are that they enjoy better sound quality and greater security."  

\noindent A couple advantages of encapsulation:

\begin{itemize}
	\item improves maintainability, flexibility, and re-usability
	\item fields can be made ``read-only" by not using setter methods, or ``write-only" by not defining getter methods  
	\item user does not know what is happening behind the scene.  Any Java object that can pass an IS-A test is considered to be polymorphic.  
\end{itemize}

\subsubsection*{Polymorphism}
In CPSC 1020 and (hopefully) 2100 we learned about Polymorphism, so we will not spend a great deal of time on it.  Basically, it is defined by \url{http://www.tutorialspoint.com/java/java_polymorphism.htm}, as the ability of an object to take on many forms.  The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.  Any Java object that can pass an IS-A test is considered to be polymorphic.  Below is an example:  

\begin{lstlisting}
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
\end{lstlisting}
The Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above example:
\begin{itemize}
    \item A Deer IS-A Animal
    \item A Deer IS-A Vegetarian
    \item A Deer IS-A Deer
    \item A Deer IS-A Object
\end{itemize}
\subsection*{Modeling Relationships and Structures}
In this section we introduce the UML class diagrams for modeling object-oriented software systems and various types of relations among the classes.  Class diagrams, for example, consist of nodes that represent classes and interfaces and links that represent relationships among classes.  \\

\noindent The relationships that we will look at are inheritance, association, and dependency.  We saw graphical notations for classes earlier, we will now look at notations for relationships.  

\subsubsection*{Inheritance}
Inheritance is one of the most important relationships in OO modeling.  Inheritance can be defined as the process where one object acquires the properties of another.  The keyword associated with inheritance is extends and implements.  Inheritance implies ``IS-A" relationship.  

\noindent Various forms of inheritance relations are as follows:

\begin{itemize}
	\item extension relationship between 2 classes  -- class C2 extends class C1, class C2 is known as a subclass of class C1, and C1 is known as a superclass of class C2
	\item extension relationship between 2 interfaces -- when interface I2 extends interface I1, interface I2 is known as a subinterface of interface I1, and interface I1 is known as a superinterface of interface I2 
	\item implementation relation between a class and an interface -- when class C2 implements interfece I1, class C2 is known as an implementation of interface I1, and interface I1 is known as an interface of class C2
\end{itemize}
\noindent Figure~\ref{fig:cht22ex} is a graphical depiction of the above.

\begin{figure}[h]
\begin{center}
  \leavevmode
  \includegraphics[width =0.80\textwidth]{cht22ex.pdf}
\end{center}
\vspace{-.7 cm}
\caption{UML Notation for Inheritance Relationships}
\label{fig:cht22ex}
\end{figure}

\noindent Extension is represented by a solid link, implementation by a dashed link,  and the superclass and superinterface is represented by a hollow triangle pointing toward the superclass.  
\begin{figure}[h]
\begin{center}
  \leavevmode
  \includegraphics[width =0.80\textwidth]{ch23ex.pdf}
\end{center}
\vspace{-.7 cm}
\caption{Class Diagram:  Inheritance Relation Among Various Student Groups}
\label{fig:ch23ex}
\end{figure}

\noindent Figure~\ref{fig:ch23ex} is an example of inheritance relations among classes that represent various student groups.  The Student class represents students in general.  This is the superclass of subclasses, Nondegree, Undergraduate, and Graduate.  Graduate is the superclass of Master and PhD.  In other words Nondegree, Undergraduate, and Graduate each have an ``is-a" relationship with Student.  Just as Master and PhD both have a ``is-a" relationship with Graduate.  (note:  OO in general supports multiple inheritance relationships, however Java only allows one extends)


\paragraph{Levels of Abstraction}
Class and interfaces represent abstractions.  The inheritance relationship organizes the classes and interfaces into different levels of abstraction.  The higher the class is in the tree or hierarchy the more general the abstraction.  The lower levels of the tree are considered more specialized.  Look at Figure~\ref{fig:ch23ex} again.  The Student class is very general. However, the Master and PhD classes (leaf classes) are highly specialized.  


\subsubsection*{Association}
Association models the various relationships between classes. The graphical notation for association is a solid line between the two classes with an optional label and optional adornment attached to either end.  Figure~\ref{fig:ch25ex} is an an example of association between {\em{Student, Faculty,}} and {\em{Course}} classes.  The \textit{teach} and \textit{enroll} keywords are the names of the association between {\em{Faculty -- Course}} and {\em{Student -- Course}}  classes.  The solid arrow with no tail indicates the direction of the  association with respect to the name. As an example, the arrow next to the \textit{enroll} means that ``a student enrolls in a course".  Unless otherwise specified, the default association is left to right top to bottom.  Hence, ``Faculty teaches Course".  The keywords ``advisor and advisee" are the role names associated with \textit{Faculty} and \textit{Student}. The multiplicity specification is a comma-separated sequence of integers values.  The integer interval can be one of the following:

\begin{itemize}
	\item \textit{l .. u}  specifies a closed, that is, inclusive, range of integers from the lower bound to upper bound.  If the upper bound is an (*), this indicates an unlimited upper bound
	\item \textit{i} specifies a singleton range that contains integer \textit{i}, which is an integer literal.
	\item \textit{*} specifies the entire nonnegative range: 0,1,2,3,....
\end{itemize}

\begin{figure}[h]
\begin{center}
  \leavevmode
  \includegraphics[width =0.80\textwidth]{ch25exv3.pdf}
\end{center}
\vspace{-.7 cm}
\caption{Class Diagram:  Association Relationships}
\label{fig:ch25ex}
\end{figure}

\noindent  In Figure~\ref{fig:ch25ex}, the enroll association is many-to-many -- a student may enroll in many courses and a course may have any number of students enrolled.  The teach association is one-to-many -- each course has only one Faculty.  Also, each student only has one advisor but an advisor can have many advisees.

\subsection*{Aggregation and Composition}
Aggregation is an association that represents a \textit{has-a or part-whole} relationship with another class.  The stronger form of aggregation is \textit{composition} which implies exclusive ownership of the component class by the aggregate class.  Figure~\ref{fig:ch26ex} depicts the UML notation for the aggregation relationship. Aggregate is indicated by a hollow diamond pointing toward the aggregate class and composition is indicated by a solid diamond pointing toward the composite class.  Figure~\ref{fig:ch27ex} shows an example of the aggregation relationships among the {\em{University, College, Department, Faculty, and Student}} classes.  All relationships shown in the diagram are aggregation relationships:

\begin{itemize}
	\item a student is part of a department
	\item a faculty member is a part of a department
	\item a department is a part of a college
	\item a college is a part of a university
\end{itemize}
\noindent From the first two items we see a student or a faculty member may exist without being a part of a department.  The lifetimes of the students and the faculty members are independent from the lifetime of the department to which they belong.  In the last two items we see the department exclusively belongs to a college, and a college exclusively belongs to a university.  
 
\noindent The distinction between aggregation and composition is entirely conceptual.  There is not difference in the implementation.

\begin{figure}[h]
\begin{center}
  \leavevmode
  \includegraphics[width =0.60\textwidth]{ch26ex.pdf}
\end{center}
\vspace{-.7 cm}
\caption{UML Notation for Aggregation Relationships,
Aggregation on Left -- Composition on Right}
\label{fig:ch26ex}
\end{figure}

\begin{figure}[h]
\begin{center}
  \leavevmode
  \includegraphics[width =0.90\textwidth]{ch27ex.pdf}
\end{center}
\vspace{-.7 cm}
\caption{Class Diagram: Aggregation Relationship}
\label{fig:ch27ex}
\end{figure}

\subsection*{Dependency}
Dependency is a directed relationship between named elements, classes, interfaces, components, artifacts, and packages. 

The following is directly from the our book (Object-Oriented Software Development Using Java, by Xiaoping Jia) ``Dependency is a relationship between entities such that the proper operation of one entity depends on the presence of the other entity, and changes in one entity would affect the other entity.  A common form of dependency is the {\em{use}} relation among classes.  In other words, class C1 depends on class C2 if C1 uses C2 in places such as the parameters, local variables, or return types of its methods.  The graphical notation for dependency relationships is a dashed line with an arrow pointing in the direction of the dependency, as show in Figure~\ref{fig:ch28ex}.\\

The class diagram shown in Figure~\ref{fig:ch29ex} illustrates the dependency relationship.  The Registar class has a number of methods for adding and removing courses in the course schedules and enrolling and dropping students in courses.  These methods take parameters that are instances of CourseSchedule, Course, and Student classes.  Therefore, the Registarar class uses, that is, depends on, CourseSchedule, Course and Student classes."


\noindent Figure~\ref{fig:ch29ex} shows the Registrar class uses \textit{CourseSchedule, Course} and \textit{Student} classes.
\begin{figure}[h]
\begin{center}
  \leavevmode
  \includegraphics[width =0.50\textwidth]{ch28ex.pdf}
\end{center}
\vspace{-.7 cm}
\caption{UML Notation for Dependency Relationship}
\label{fig:ch28ex}
\end{figure}

%\vspace{-1 cm}
\begin{figure}[h!]
\begin{center}
  \leavevmode
  \includegraphics[width =0.80\textwidth]{ch29ex.pdf}
\end{center}
\vspace{-.7 cm}
\caption{Class Diagram: Dependency Relationship}
\label{fig:ch29ex}
\end{figure}

\end{document}


