classdef Controller1 < handle %一个controller。 %接收用户鼠标点击,判断鼠标点击区域,加入/删除点则通知Model,Model再通知View %先初始化Model,再初始化View,最后初始化Controller %点击区域。 properties(SetAccess=private) %各个按钮的区域的范围 section=[0 527 0 523; %盘面有效点击区域 -166,-22,465,522; %“退出”图标范围 -166,-40,400,445; %重新开始区域 -166,-80,330,375]; %回退一步区域 modelObj viewObj ctrlUndo ctrlRestart ctrlBye end methods %---------------------------------构造函数--------------------------------- function obj=Controller1(modelObject,viewObject) % obj.modelObj=modelObject; obj.viewObj=viewObject; obj.ctrlUndo= uicontrol('Parent', viewObject.figureHandle,'Style','pushbutton',... 'String',{'回退'},'Position',[1,100,70,25]); obj.ctrlRestart= uicontrol('Parent', viewObject.figureHandle,'Style','pushbutton',... 'String','重新开始','Position',[1,70,70,25]); obj.ctrlBye= uicontrol('Parent', viewObject.figureHandle,'Style','pushbutton',... 'String','退出程序','Position',[1,40,70,25]); set(viewObject.axisHandle,'ButtonDownFcn',{@obj.addpoint}); set(obj.ctrlUndo,'Callback',{@obj.removepoint}); set(obj.ctrlRestart,'Callback',{@obj.restart}); set(obj.ctrlBye,'Callback',{@obj.exit_mvc}); end %--------------------------在图形上添加一个点------------------------------- function addpoint(obj,~,~) CursorXY=get(obj.viewObj.axisHandle, 'CurrentPoint'); x=CursorXY(1,1); y=CursorXY(1,2); obj.modelObj+[x,y]; %#ok<VUNUS> end %--------------------------在图形上去掉一个点------------------------------- function removepoint(obj,~,~) obj.modelObj-1; %#ok<MNEFF> end %---------------------------重新开始计算图形------------------------------- function restart(obj,~,~) obj.modelObj-inf; %#ok<MNEFF> end %------------------------------退出程序------------------------------------ function exit_mvc(~,~,~) close all; end end end
时间: 2023-06-06 07:05:07 浏览: 48
null 是一个编程术语,指的是一个空值或者空对象。在程序中,null 可以表示一个变量没有被初始化,或者一个对象没有被赋值。使用 null 可以避免出现 undefined 或者其他不确定的值,从而保证程序的稳定和安全。
相关问题
golang iris mvc实现表增删改查源代码,包含router/model/service/controller/config
很高兴回答这个问题。以下是一个基于Go语言Iris框架的MVC(模型-视图-控制器)架构的源代码实现:
1、router.go:
```
package router
import (
"github.com/kataras/iris/v12"
"myproject/controller"
)
func Router(app *iris.Application) {
userController := controller.UserController{}
app.Handle("GET", "/users", userController.GetAll)
app.Handle("GET", "/user/{id}", userController.GetByID)
app.Handle("POST", "/user", userController.Create)
app.Handle("PUT", "/user/{id}", userController.UpdateByID)
app.Handle("DELETE", "/user/{id}", userController.DeleteByID)
}
```
2、model.go:
```
package model
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"myproject/config"
)
var (
DB *gorm.DB
)
func Init() {
dsn := config.GetDSN()
var err error
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
DB.AutoMigrate(&User{})
}
type User struct {
gorm.Model
Name string `gorm:"type:varchar(32)"`
Age int `gorm:"default:18"`
}
func (u *User) TableName() string {
return "users"
}
```
3、service.go:
```
package service
import (
"myproject/model"
)
type UserService struct{}
func (us *UserService) GetAll() []*model.User {
var users []*model.User
model.DB.Find(&users)
return users
}
func (us *UserService) GetByID(id int) *model.User {
user := &model.User{}
model.DB.First(user, id)
return user
}
func (us *UserService) Create(user *model.User) {
model.DB.Create(user)
}
func (us *UserService) UpdateByID(id int, user *model.User) {
model.DB.Model(&model.User{}).Where("id=?", id).Updates(&user)
}
func (us *UserService) DeleteByID(id int) {
model.DB.Delete(&model.User{}, id)
}
```
4、controller.go:
```
package controller
import (
"github.com/kataras/iris/v12"
"myproject/model"
"myproject/service"
"strconv"
)
type UserController struct{}
func (uc *UserController) GetAll(ctx iris.Context) {
userService := &service.UserService{}
result := userService.GetAll()
ctx.JSON(result)
}
func (uc *UserController) GetByID(ctx iris.Context) {
userService := &service.UserService{}
id, _ := strconv.Atoi(ctx.Params().Get("id"))
result := userService.GetByID(id)
ctx.JSON(result)
}
func (uc *UserController) Create(ctx iris.Context) {
userService := &service.UserService{}
user := &model.User{}
ctx.ReadJSON(&user)
userService.Create(user)
}
func (uc *UserController) UpdateByID(ctx iris.Context) {
userService := &service.UserService{}
user := &model.User{}
id, _ := strconv.Atoi(ctx.Params().Get("id"))
ctx.ReadJSON(&user)
userService.UpdateByID(id, user)
}
func (uc *UserController) DeleteByID(ctx iris.Context) {
userService := &service.UserService{}
id, _ := strconv.Atoi(ctx.Params().Get("id"))
userService.DeleteByID(id)
}
```
5、config.go:
```
package config
func GetDSN() string {
return "root:123456@tcp(127.0.0.1:3306)/mydb?charset=utf8mb4&parseTime=True&loc=Local"
}
```
这是一份基础的代码实现,你可以根据你的需求进行相应的修改和优化。希望对你有帮助。
@controller和@service是否相同
No, @Controller and @Service are not the same. They are different annotations used in the MVC (Model-View-Controller) architectural pattern.
@Controller is used to mark a class as a controller component. It handles incoming requests, maps them to appropriate methods, and returns responses. Controllers are responsible for handling the user's interaction with the application, processing requests, and preparing data for the view.
@Service is used to mark a class as a service component. Service components contain the business logic of an application and typically handle complex operations, data manipulation, and interactions with repositories or other services. Services are responsible for implementing specific business functionalities and are often called by controllers or other services.
In summary, @Controller and @Service have different roles and responsibilities within the MVC pattern. Controllers handle request handling and response generation, while services encapsulate business logic and perform complex operations.