CS3220 Web and Internet Programming About Data Models Chengyu Sun California State University, Los Angeles
Broadly speaking, the world consists of things and actions performed by things or on things
Things and Actions in Computer Programs Most of computer programs are created to help people solve problems in the real world (or in the case of video games, problems in imaginary worlds) Real World Computer Program Things Data Actions Actions (Operations)
Data and Operations in Programming Languages Values (a.k.a. Literals) Variables Structs Data Functions Methods Operations
Data and Operations in OO Languages Class { Fields Methods } Data Operations
Data and Operations in MVC A class that represents only data Model Displays the result of an operation View Controller Implements an operation
Importance of Data Modeling Data is half of your application If data is not modeled correctly, operations will be wrong no matter what you do
What's Wrong with the Code? List<String> surveys = new ArrayList<String>(); List<String> questions = new ArrayList<String>(); List<String> choices = new ArrayList<String>();
What's Wrong with the Code? public class Survey { String name; List<String> questions; List<String> choices; }
What's Wrong with the Code? public class Survey { String name; String question; List<String> choices; }
What's Wrong with the Code? public class Survey { String name; Map<String, List<String>> questions; }
Create Data Model Classes Look for nouns in the problem description Simple types (e.g. number, string, data/time) become fields Complex types (i.e. things that contain other things) become classes Add an id field when necessary If it passes the "English test", it's probably a good class