본문 바로가기

안드로이드 자바 코틀린 기초 지식/안드로이드 예제 및 코드

MVC 패턴

MVC 관련 내용: https://growing-software-engineer.tistory.com/25

 

예제 추가 :

1. MvcModel class

import java.util.*

class MvcModel : Observable() {
    val List: MutableList<Int>

    init {
        List = ArrayList(3)
        List.add(0)
        List.add(0)
        List.add(0)
    }

    //Getter Setter 메소드
    @Throws(IndexOutOfBoundsException::class)
    fun getValueAtIndex(the_index: Int): Int {
        return List[the_index]
    }

    @Throws(java.lang.IndexOutOfBoundsException::class)
    fun setValueAtIndex(the_index: Int) {
        List[the_index] = List[the_index] + 1
        setChanged()
        notifyObservers()
    }
}

2. MvcPatternFragment.fragment

import MvcModel
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import my.android_study.R
import my.android_study.databinding.FragmentMvcPatternBinding
import java.util.*

class MvcPatternFragment : Fragment(), Observer, View.OnClickListener  {

    private lateinit var binding: FragmentMvcPatternBinding

    private var myModel: MvcModel? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentMvcPatternBinding.inflate(inflater, container, false)

        myModel = MvcModel()
        myModel!!.addObserver(this);

        binding.button.setOnClickListener(this);
        binding.button2.setOnClickListener(this);
        binding.button3.setOnClickListener(this);

        val view = binding.root
        return view
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.button ->  myModel?.setValueAtIndex(0)
            R.id.button2 -> myModel?.setValueAtIndex(1)
            R.id.button3 -> myModel?.setValueAtIndex(2)
        }
    }

    @Deprecated("Deprecated in Java")
    override fun update(arg0: Observable, arg1: Any?) {
        binding.button.text = "Count:${myModel!!.getValueAtIndex(0)}"
        binding.button2.text = "Count:${myModel!!.getValueAtIndex(1)}"
        binding.button3.text = "Count:${myModel!!.getValueAtIndex(2)}"
    }
}

3. fragment_mvc_pattern.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".fragments_code_examples.mvc_pattern.MvcPatternFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#168BC34A"
            tools:context=".MainActivity">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

            <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="40dp"
                    android:layout_marginBottom="60dp"
                    android:text="MVC Architecture Pattern"
                    android:textAlignment="center"
                    android:textColor="@android:color/holo_green_dark"
                    android:textSize="30sp"
                    android:textStyle="bold"/>

            <Button
                    android:id="@+id/button"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="30dp"
                    android:layout_marginEnd="20dp"
                    android:layout_marginBottom="20dp"
                    android:background="#4CAF50"
                    android:text="Count:0"
                    android:textColor="@android:color/background_light"
                    android:textSize="24sp"
                    android:textStyle="bold"/>

            <!-- Second Button of the activity. -->
            <Button
                    android:id="@+id/button2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="50dp"
                    android:layout_marginEnd="20dp"
                    android:layout_marginBottom="20dp"
                    android:background="#4CAF50"
                    android:text="Count:0"
                    android:textColor="@android:color/background_light"
                    android:textSize="24sp"
                    android:textStyle="bold"/>

            <!-- Third Button of the activity. -->
            <Button
                    android:id="@+id/button3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="50dp"
                    android:layout_marginEnd="20dp"
                    android:layout_marginBottom="20dp"
                    android:background="#4CAF50"
                    android:text="Count:0"
                    android:textColor="@android:color/background_light"
                    android:textSize="24sp"
                    android:textStyle="bold"/>
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

'안드로이드 자바 코틀린 기초 지식 > 안드로이드 예제 및 코드' 카테고리의 다른 글

MVVM pattern  (0) 2023.03.18
MVP 패턴  (0) 2023.03.10