Model–View–Controller (MVC) is a design pattern forcomputer user interfaces that divides an application into three areas of responsibility:
1. the Model: the domain objects or data structures that represent the application's state.
2. the View, which observes the state and generatesoutput to the users.
3. the Controller, which translatesuser input into operations on the model.
button对应的MVC是ButtonModel,ButtonUI和ButtonListener
ButtonModel管理所有的状态和对状态的操作,比如按钮被选中,按下,松开等(这个状态是程序内部的记录,用户无法观察到)。而让ButtonModel对状态进行操作的是ButtonListener,ButtonListener接受用户的操作(选中,点击按钮等)并反馈给ButtonModel。此外,当ButtonModel状态改变,让用户视觉上知道ButtonModel改变的是ButtonUI,即用户会看到按钮被选中,按下等样子
以用户选中某个按钮为例:
当一个用户选中按钮,而用户也看到了按钮被按下,对于button所发生的过程如下:ButtonListener接收到按钮被选中,它将信息反馈给ButtonModel,从而使ButtonModel从原先未被选中变为被选中状态。此过程ButtonListener起到了Controller的角色。ButtonModel的状态以变为选中状态,而用户需要视觉上看到按钮被选中,所以ButtonUI负责将选来未被选中的视觉效果转为被选中的视觉效果。整一个MVC框架则被封装在button这个组件中