From 15f42dfee7d1040182c44aa0aa407a8d816e6562 Mon Sep 17 00:00:00 2001
From: Kanishk Chhabra <67221487+mrkc2303@users.noreply.github.com>
Date: Mon, 29 Nov 2021 21:01:56 +0530
Subject: [PATCH 1/4] Update 19.1 Overview of MVVM architecture.md
---
.../19. Architecture/19.1 Overview of MVVM architecture.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Android_Development_with_Kotlin/19. Architecture/19.1 Overview of MVVM architecture.md b/Android_Development_with_Kotlin/19. Architecture/19.1 Overview of MVVM architecture.md
index 48177e23e0..aa806a49c2 100644
--- a/Android_Development_with_Kotlin/19. Architecture/19.1 Overview of MVVM architecture.md
+++ b/Android_Development_with_Kotlin/19. Architecture/19.1 Overview of MVVM architecture.md
@@ -27,7 +27,7 @@ MVVM stands for Model, View, ViewModel.
It acts as a link between the Model and the View. It’s responsible for transforming the data from the Model. It provides data streams to the View. It also uses hooks or callbacks to update the View. It’ll ask for the data from the Model.
A ViewModel is always created in association with a scope (an fragment or an activity) and will be retained as long as the scope is alive. E.g. if it is an Activity, until it is finished.
-
+
In other words, this means that a ViewModel will not be destroyed if its owner is destroyed for a configuration change (e.g. rotation). The new instance of the owner will just re-connected to the existing ViewModel.
| ![View Model Lifecycle Scope](https://i0.wp.com/howtodoandroid.com/wp-content/uploads/2021/03/view-model-scope.png?w=522&ssl=1 "View Model Lifecycle Scope") |
From 5a044cc8152457beafc397cfc7425407190679d2 Mon Sep 17 00:00:00 2001
From: Kanishk Chhabra <67221487+mrkc2303@users.noreply.github.com>
Date: Mon, 29 Nov 2021 22:43:56 +0530
Subject: [PATCH 2/4] Create 19.04 Overview of MVC architecture.md
---
.../19.04 Overview of MVC architecture.md | 277 ++++++++++++++++++
1 file changed, 277 insertions(+)
create mode 100644 Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md
diff --git a/Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md b/Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md
new file mode 100644
index 0000000000..d2e5c11308
--- /dev/null
+++ b/Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md
@@ -0,0 +1,277 @@
+# Overview of MVC architecture in Android
+
+Developing an android application by applying a software architecture pattern is always preferred by the developers. An architecture pattern gives modularity to the project files and assures that all the codes get covered in Unit testing. It makes the task easy for developers to maintain the software and to expand the features of the application in the future. There are some architectures that are very popular among developers and one of them is the Model—View—Controller(MVC) Pattern. The MVC pattern suggests splitting the code into 3 components. While creating the class/file of the application, the developer must categorize it into one of the following three layers:
+
+1. Model: This component stores the application data. It has no knowledge about the interface. The model is responsible for handling the domain logic(real-world business rules) and communication with the database and network layers.
+
+1. View: It is the UI(User Interface) layer that holds components that are visible on the screen. Moreover, it provides the visualization of the data stored in the Model and offers interaction to the user.
+
+1. Controller: This component establishes the relationship between the View and the Model. It contains the core application logic and gets informed of the user’s behavior and updates the Model as per the need.
+
+![MVC BASIC]()
+
+In spite of applying MVC schema to give a modular design to the application, code layers do depend on each other. In this pattern, `View` and `Controller` both depend upon the Model. Multiple approaches are possible to apply the MVC pattern in the project:
+
+* Approach 1: Activities and fragments can perform the role of Controller and are responsible for updating the View.
+* Approach 2: Use activity or fragments as views and controller while Model will be a separate class that does not extend any Android class.
+
+
+In `MVC architecture`, application data is updated by the controller and View gets the data. Since the `Model` component is separated, it could be tested independently of the UI. Further, if the View layer respects the `single responsibility principle` then their role is just to update the Controller for every user event and just display data from the Model, without implementing any business logic. In this case, UI tests should be enough to cover the functionalities of the View.
+
+To understand the implementation of the MVC architecture pattern more clearly, here is a simple example of an android application. This application will have 3 buttons and each one of them displays the count that how many times the user has clicked that particular button. To develop this application the code has been separated in the following manner:
+
+* __Controller and View__ will be handled by the Activity. Whenever the user clicks the buttons, activity directs the Model to handle the further operations. The activity will act as an observer.
+
+* __The Model__ will be a separate class that contains the data to be displayed. The operations on the data will be performed by functions of this class and after updating the values of the data this Observable class notifies the Observer(Activity) about the change.
+
+## Steps to implement MVC architecture in Android
+
+### Step 1: Create a new project
+
+1. Click on File, then New => New Project.
+1. Choose Empty activity
+1. Select language as Java/Kotlin
+1. Select the minimum SDK as per your need.
+
+### Step 2: Modify String.xml file
+
+All the strings which are used in the activity are listed in this file.
+
+```
+
+ GfG | MVC Architecture
+ MVC Architecture Pattern
+ Button_1
+ Count:0
+
+```
+
+### Step 3: Working with the activity_main.xml file
+
+Open the activity_main.xml file and add 3 Buttons to it which will display the count values as per the number of times the user clicks it. Below is the code for designing a proper activity layout.
+
+```
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+```
+
+### Step 4: Creating the Model class
+
+Create a new class named Model to separate all data and its operations. This class will not know the existence of View Class.
+
+```
+import java.util.*
+import kotlin.collections.ArrayList
+
+class Model : Observable() {
+ // declaring a list of integer
+ val List: MutableList
+
+ // constructor to initialize the list
+ init {
+ // reserving the space for list elements
+ List = ArrayList(3)
+
+ // adding elements into the list
+ List.add(0)
+ List.add(0)
+ List.add(0)
+ }
+
+ // defining getter and setter functions
+ // function to return appropriate count
+ // value at correct index
+ @Throws(IndexOutOfBoundsException::class)
+ fun getValueAtIndex(the_index: Int): Int {
+ return List[the_index]
+ }
+
+ // function to make changes in the activity button's
+ // count value when user touch it
+ @Throws(IndexOutOfBoundsException::class)
+ fun setValueAtIndex(the_index: Int) {
+ List[the_index] = List[the_index] + 1
+ setChanged()
+ notifyObservers()
+ }
+}
+```
+
+### Step 5: Define functionalities of View and Controller in the MainActivity file
+
+This class will establish the relationship between View and Model. The data provided by the Model will be used by View and the appropriate changes will be made in the activity.
+
+```
+import android.os.Bundle
+import android.view.View
+import android.widget.Button
+import androidx.appcompat.app.AppCompatActivity
+import java.util.*
+
+class MainActivity : AppCompatActivity(), Observer, View.OnClickListener {
+
+ // creating object of Model class
+ var myModel: Model? = null
+
+ // creating object of Button class
+ var Button1: Button? = null
+ var Button2: Button? = null
+ var Button3: Button? = null
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ setContentView(R.layout.activity_main)
+
+ // creating relationship between the
+ // observable Model and the
+ // observer Activity
+ myModel = Model()
+ myModel!!.addObserver(this)
+
+ // assigning button IDs to the objects
+ Button1 = findViewById(R.id.button)
+ Button2 = findViewById(R.id.button2)
+ Button3 = findViewById(R.id.button3)
+
+
+ // transfer the control to Onclick() method
+ // when a button is clicked by passing
+ // argument "this"
+ Button1?.setOnClickListener(this)
+ Button2?.setOnClickListener(this)
+ Button3?.setOnClickListener(this)
+ }
+
+ // calling setValueAtIndex() method
+ // by passing appropriate arguments
+ // for different buttons
+ 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)
+ }
+ }
+
+ // function to update the view after
+ // the values are modified by the model
+ override fun update(arg0: Observable, arg1: Any?) {
+
+ // changing text of the buttons
+ // according to updated values
+ Button1!!.text = "Count: " + myModel!!.getValueAtIndex(0)
+ Button2!!.text = "Count: " + myModel!!.getValueAtIndex(1)
+ Button3!!.text = "Count: " + myModel!!.getValueAtIndex(2)
+ }
+}
+```
+
+
+## Advantages of MVC architecture pattern
+
+* MVC pattern increases the code testability and makes it easier to implement new features as it highly supports the separation of concerns.
+
+* Unit testing of Model and Controller is possible as they do not extend or use any Android class.
+
+* Functionalities of the View can be checked through UI tests if the View respect the single responsibility principle(update controller and display data from the model without implementing domain logic)
+
+
+## Disadvantages of MVC architecture pattern
+
+* Code layers depend on each other even if MVC is applied correctly.
+* No parameter to handle UI logic i.e., how to display the data.
+
+
+## Author
+
+- [Kanishk Chhabra](https://github.com/mrkc2303) For Documentation
+
From 9842bcd3cdaba0ffa69373f5cd11df2054a81a40 Mon Sep 17 00:00:00 2001
From: Kanishk Chhabra <67221487+mrkc2303@users.noreply.github.com>
Date: Mon, 29 Nov 2021 22:55:43 +0530
Subject: [PATCH 3/4] Update 19.04 Overview of MVC architecture.md
---
.../19. Architecture/19.04 Overview of MVC architecture.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md b/Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md
index d2e5c11308..a3f74d4a90 100644
--- a/Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md
+++ b/Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md
@@ -8,7 +8,7 @@ Developing an android application by applying a software architecture pattern is
1. Controller: This component establishes the relationship between the View and the Model. It contains the core application logic and gets informed of the user’s behavior and updates the Model as per the need.
-![MVC BASIC]()
+![MVC BASIC](https://user-images.githubusercontent.com/67221487/143914452-5ad75ae3-f85c-4022-a632-38934b20ca1e.png)
In spite of applying MVC schema to give a modular design to the application, code layers do depend on each other. In this pattern, `View` and `Controller` both depend upon the Model. Multiple approaches are possible to apply the MVC pattern in the project:
From 2c71c85f1e626df5e5856cd202ad4d017dffc12a Mon Sep 17 00:00:00 2001
From: Rahul Sain <50570677+rahulsain@users.noreply.github.com>
Date: Tue, 30 Nov 2021 10:16:39 +0530
Subject: [PATCH 4/4] Rename 19.04 Overview of MVC architecture.md to 19.4
Overview of MVC architecture.md
---
...f MVC architecture.md => 19.4 Overview of MVC architecture.md} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename Android_Development_with_Kotlin/19. Architecture/{19.04 Overview of MVC architecture.md => 19.4 Overview of MVC architecture.md} (100%)
diff --git a/Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md b/Android_Development_with_Kotlin/19. Architecture/19.4 Overview of MVC architecture.md
similarity index 100%
rename from Android_Development_with_Kotlin/19. Architecture/19.04 Overview of MVC architecture.md
rename to Android_Development_with_Kotlin/19. Architecture/19.4 Overview of MVC architecture.md