# MVC Design Pattern Example in Ring
# Simple Task Manager Application
load "stdlib.ring"
# ============================================
# MAIN APPLICATION
# ============================================
# Create instances of Model, View, and Controller
taskModel = new TaskModel
taskView = new TaskView
taskController = new TaskController(taskModel, taskView)
# Add some sample data
taskModel.addTask("Learn Ring", "Study the Ring programming language")
taskModel.addTask("Build MVC App", "Create an example MVC application")
# Run the application
taskController.run()
# ============================================
# MODEL - Handles data and business logic
# ============================================
class TaskModel
tasks = []
nextId = 1
func addTask(title, description)
task = [
:id = nextId,
:title = title,
:description = description,
:completed = false
]
add(tasks, task)
nextId++
return task
func getAllTasks()
return tasks
func getTask(id)
for task in tasks
if task[:id] = id
return task
ok
next
return null
func updateTask(id, title, description)
for i = 1 to len(tasks)
if tasks[i][:id] = id
taskRef = Ref(tasks[i])
taskRef[:title] = title
taskRef[:description] = description
return true
ok
next
return false
func toggleComplete(id)
for i = 1 to len(tasks)
if tasks[i][:id] = id
taskRef = Ref(tasks[i])
taskRef[:completed] = !taskRef[:completed]
return true
ok
next
return false
func deleteTask(id)
for i = 1 to len(tasks)
if tasks[i][:id] = id
del(tasks, i)
return true
ok
next
return false
# ============================================
# VIEW - Handles presentation and output
# ============================================
class TaskView
func displayMenu()
see nl + "===== TASK MANAGER =====" + nl
see "1. Add Task" + nl
see "2. View All Tasks" + nl
see "3. Toggle Task Complete" + nl
see "4. Update Task" + nl
see "5. Delete Task" + nl
see "6. Exit" + nl
see "Enter choice: "
func displayTasks(tasks)
if len(tasks) = 0
see nl + "No tasks found!" + nl
return
ok
see nl + "===== YOUR TASKS =====" + nl
for task in tasks
status = "[ ]"
if task[:completed]
status = "[X]"
ok
see status + " ID: " + task[:id] + " - " + task[:title] + nl
see " Description: " + task[:description] + nl
next
func displayTask(task)
if task = null
see nl + "Task not found!" + nl
return
ok
status = "Incomplete"
if task[:completed]
status = "Complete"
ok
see nl + "===== TASK DETAILS =====" + nl
see "ID: " + task[:id] + nl
see "Title: " + task[:title] + nl
see "Description: " + task[:description] + nl
see "Status: " + status + nl
func displayMessage(message)
see nl + message + nl
func getInput(prompt)
see prompt
give input
return input
# ============================================
# CONTROLLER - Handles user interaction logic
# ============================================
class TaskController
model
view
func init(taskModel, taskView)
model = taskModel
view = taskView
func run()
running = true
while running
view.displayMenu()
choice = view.getInput("")
switch choice
on "1"
addTask()
on "2"
viewAllTasks()
on "3"
toggleTaskComplete()
on "4"
updateTask()
on "5"
deleteTask()
on "6"
running = false
view.displayMessage("Goodbye!")
other
view.displayMessage("Invalid choice. Please try again.")
off
end
func addTask()
title = view.getInput("Enter task title: ")
description = view.getInput("Enter task description: ")
model.addTask(title, description)
view.displayMessage("Task added successfully!")
func viewAllTasks()
tasks = model.getAllTasks()
view.displayTasks(tasks)
func toggleTaskComplete()
id = number(view.getInput("Enter task ID to toggle: "))
if model.toggleComplete(id)
view.displayMessage("Task status updated!")
else
view.displayMessage("Task not found!")
ok
func updateTask()
id = number(view.getInput("Enter task ID to update: "))
task = model.getTask(id)
if task = null
view.displayMessage("Task not found!")
return
ok
view.displayTask(task)
title = view.getInput("Enter new title: ")
description = view.getInput("Enter new description: ")
if model.updateTask(id, title, description)
view.displayMessage("Task updated successfully!")
ok
func deleteTask()
id = number(view.getInput("Enter task ID to delete: "))
if model.deleteTask(id)
view.displayMessage("Task deleted successfully!")
else
view.displayMessage("Task not found!")
ok