Skip to content

Commit

Permalink
更新责任链模式
Browse files Browse the repository at this point in the history
  • Loading branch information
phcbest committed May 23, 2023
1 parent 64ead4b commit 8785800
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 24 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.phcbest.designpatterns.behaviorPattern.chainOfResponsibility

import android.content.Context

object ChainOfResponsibilityDemo {
fun doChainOfResponsibility(context: Context) {
val dialogUnit1 = DialogUnit1()
val dialogUnit2 = DialogUnit2()
val dialogUnit3 = DialogUnit3()
dialogUnit1.setNextUnit(dialogUnit2)
dialogUnit2.setNextUnit(dialogUnit3)
dialogUnit1.handlerRequest(context)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.phcbest.designpatterns.behaviorPattern.chainOfResponsibility

import android.app.AlertDialog
import android.content.Context

class DialogUnit1 : IChainOfResponsibilityHandler {

private var mNextHandler: IChainOfResponsibilityHandler? = null

override fun setNextUnit(handler: IChainOfResponsibilityHandler) {
mNextHandler = handler
}

override fun handlerRequest(context: Context) {
AlertDialog.Builder(context).setTitle("处理器1").setMessage("处理器1")
.setNegativeButton("处理") { dialog, which ->
mNextHandler?.handlerRequest(context)
}.setNeutralButton("取消处理") { dialog, which ->
return@setNeutralButton
}.show()
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.phcbest.designpatterns.behaviorPattern.chainOfResponsibility

import android.app.AlertDialog
import android.content.Context

class DialogUnit2 : IChainOfResponsibilityHandler {

private var mNextHandler: IChainOfResponsibilityHandler? = null

override fun setNextUnit(handler: IChainOfResponsibilityHandler) {
mNextHandler = handler
}

override fun handlerRequest(context: Context) {
AlertDialog.Builder(context).setTitle("处理器2").setMessage("处理器2")
.setNegativeButton("处理") { dialog, which ->
mNextHandler?.handlerRequest(context)
}.setNeutralButton("取消处理") { dialog, which ->
return@setNeutralButton
}.show()
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.phcbest.designpatterns.behaviorPattern.chainOfResponsibility

import android.app.AlertDialog
import android.content.Context

class DialogUnit3 : IChainOfResponsibilityHandler {

private var mNextHandler: IChainOfResponsibilityHandler? = null

override fun setNextUnit(handler: IChainOfResponsibilityHandler) {
mNextHandler = handler
}

override fun handlerRequest(context: Context) {
AlertDialog.Builder(context).setTitle("处理器3").setMessage("处理器3")
.setNegativeButton("处理") { dialog, which ->
mNextHandler?.handlerRequest(context)
}.setNeutralButton("取消处理") { dialog, which ->
return@setNeutralButton
}.show()
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.phcbest.designpatterns.behaviorPattern.chainOfResponsibility

import android.content.Context

interface IChainOfResponsibilityHandler {
fun setNextUnit(handler: IChainOfResponsibilityHandler)
fun handlerRequest(context: Context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.phcbest.designpatterns.behaviorPattern.chainOfResponsibility.ChainOfResponsibilityDemo
import com.phcbest.designpatterns.databinding.FragmentDashboardBinding

class DashboardFragment : Fragment() {

private var _binding: FragmentDashboardBinding? = null

// This property is only valid between onCreateView and
// onDestroyView.

private val binding get() = _binding!!

override fun onCreateView(
Expand All @@ -23,20 +22,25 @@ class DashboardFragment : Fragment() {
savedInstanceState: Bundle?
): View {
val dashboardViewModel =
ViewModelProvider(this).get(DashboardViewModel::class.java)
ViewModelProvider(this)[DashboardViewModel::class.java]

_binding = FragmentDashboardBinding.inflate(inflater, container, false)
val root: View = binding.root
_binding?.btnChainOfResponsibility?.setOnClickListener(this::onClickListener)

val textView: TextView = binding.textDashboard
dashboardViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}
return root
return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private fun onClickListener(view: View) {
when (view) {
binding.btnChainOfResponsibility -> {
ChainOfResponsibilityDemo.doChainOfResponsibility(requireContext())
}
else -> {}
}
}
}
29 changes: 15 additions & 14 deletions app/src/main/res/layout/fragment_dashboard.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.dashboard.DashboardFragment">

<TextView
android:id="@+id/text_dashboard"
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
android:layout_height="match_parent"
android:columnCount="4">


<com.google.android.material.button.MaterialButton
android:id="@+id/btn_chain_of_responsibility"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="责任链模式" />
</GridLayout>

</FrameLayout>

0 comments on commit 8785800

Please sign in to comment.