MVC/ MVP / MVVM 架构学习之井字游戏_2_MVP实现
程序员文章站
2022-07-05 10:53:26
MVC 实现参考前一篇:https://blog.csdn.net/whjk20/article/details/107213015其中,布局文件和 Model 的实现不变, 使用Presenter 代替 Controller,把Activity 抽离出来成为View, 因此仅写变化的部分目录1. Presenter (与Model 和 View 交互)2. View (对View的操作抽象为借口 TicTacToeView, 供Presenter 调用 )1. Presenter (....
MVC 实现参考前一篇: https://blog.csdn.net/whjk20/article/details/107213015
其中,布局文件和 Model 的实现不变, 使用Presenter 代替 Controller,把Activity 抽离出来成为View, 因此仅写变化的部分
目录
1. Presenter (与Model 和 View 交互)
2. View (对View的操作抽象为借口 TicTacToeView, 供Presenter 调用 )
1. Presenter (与Model 和 View 交互)
interface IPresenter {
fun onCreate()
fun onPause()
fun onResume()
fun onDestroy()
}
// Presenter 如何持有View的引用 -> 创建的时候传入
class TicTacToePresenter : IPresenter {
private var tictactoeView: TicTacToeView
private var model: Board? = null
override fun onCreate() {
model = Board()
}
constructor(tictactoeView: TicTacToeView) {
this.tictactoeView = tictactoeView
}
override fun onPause() {
}
override fun onResume() {
}
override fun onDestroy() {
}
fun onButtonSelected(row: Int, col: Int) {
var playerThatMoved = model?.mark(row, col);
playerThatMoved?.let {tictactoeView.setButtonText(row, col, it.toString()) }
model?.winner?.let { tictactoeView.showWinner(it.toString()) }
}
fun onResetSelected() {
tictactoeView.clearWinnerDisplay()
tictactoeView.clearButtons()
model!!.restart()
}
}
2. View (对View的操作抽象为借口 TicTacToeView, 供Presenter 调用 )
interface TicTacToeView {
fun showWinner(winningPlayerDisplayLabel: String?)
fun clearWinnerDisplay()
fun clearButtons()
fun setButtonText(row: Int, col: Int, text: String?)
}
// 操作VIEW的抽象成接口,供presenter调用; 不用管model
// Activity 充当了VIEW, 只管初始化和更新UI操作
class TicTacToeMVPActivity : AppCompatActivity(), TicTacToeView {
companion object {
const val TAG = "TicTacToeMVPActivity"
}
private var presenter = TicTacToePresenter(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tictactoe_mvc)
presenter.onCreate()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.reset_menu, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.design_reset -> {
//交给presenter处理
presenter.onResetSelected()
return true
}
}
return super.onOptionsItemSelected(item)
}
fun onCellClicked(view: View) {
// 读取VIEW操作
var clickedButton = view as Button
var tag = clickedButton.tag.toString()
var clickedRow = Integer.valueOf(tag.substring(0, 1))
var clickedCol = Integer.valueOf(tag.substring(1, 2))
Log.d(TAG, "onCellClicked - clickedRow = $clickedRow, clickedCol=$clickedCol")
//交给presenter处理
presenter.onButtonSelected(clickedRow, clickedCol)
}
//VIEW的操作
override fun showWinner(winningPlayerDisplayLabel: String?) {
winnerPlayerViewGroup.visibility = View.VISIBLE
winnerPlayerLabel.text = winningPlayerDisplayLabel
}
override fun clearWinnerDisplay() {
winnerPlayerViewGroup.visibility = View.GONE
winnerPlayerLabel.text = EMPTY_TEXT
}
override fun clearButtons() {
for (index in 0 until buttonGrid.childCount) {
(buttonGrid.getChildAt(index) as Button).text = EMPTY_TEXT
}
}
override fun setButtonText(row: Int, col: Int, text: String?) {
val btn = buttonGrid.findViewWithTag("" + row + col) as Button
btn?.text = text
}
}
本文地址:https://blog.csdn.net/whjk20/article/details/107213294