<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: AblerSong</title>
    <description>The latest articles on Forem by AblerSong (@ablersong).</description>
    <link>https://forem.com/ablersong</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1125461%2Fcb1e1bbb-8980-4882-bad5-80a6297d7add.png</url>
      <title>Forem: AblerSong</title>
      <link>https://forem.com/ablersong</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ablersong"/>
    <language>en</language>
    <item>
      <title>MVM for h5,iOS,Android;Page Object Model, also known as POM</title>
      <dc:creator>AblerSong</dc:creator>
      <pubDate>Sat, 12 Aug 2023 03:06:43 +0000</pubDate>
      <link>https://forem.com/ablersong/mvm-for-h5iosandroidpage-object-model-also-known-as-pom-55m1</link>
      <guid>https://forem.com/ablersong/mvm-for-h5iosandroidpage-object-model-also-known-as-pom-55m1</guid>
      <description>&lt;h1&gt;
  
  
  MVMDemo
&lt;/h1&gt;

&lt;p&gt;h5,iOS,Android, software architecture, MVM (Mediator, View, ViewModel)&lt;/p&gt;

&lt;p&gt;The design was originally conceptualized following the principles of MVM (Mediator, View, ViewModel). Upon completion, I conducted an online search to identify similar approaches. Eventually, &lt;code&gt;Page Object Model, also known as POM, is a design pattern in Selenium that creates an object repository for storing all web elements.&lt;/code&gt; However, In iOS, Android, and H5, I have not yet found design pattern related to this aspect.&lt;/p&gt;

&lt;h3&gt;
  
  
  I. Introduction to MVM
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;At its core, it functions as an intermediary between the backend and the UI. It sends requests to the backend for interface data, parses the data, and produces a data model suitable for the UI's requirements, encompassing both &lt;code&gt;data&lt;/code&gt; and &lt;code&gt;function&lt;/code&gt; components. The frontend then directly renders this model, freeing UI developers from the need to address the underlying logic. Simultaneously, logic developers are liberated from focusing on UI concerns.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;start&lt;/code&gt; in &lt;a href="https://github.com/AblerSong/MVMDemo" rel="noopener noreferrer"&gt;demo&lt;/a&gt;, simple code, beginner developers can easily understand&lt;br&gt;
implementation:&lt;a href="https://github.com/AblerSong/MVMDemo" rel="noopener noreferrer"&gt;https://github.com/AblerSong/MVMDemo&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  II. Design Approach
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a Page &lt;code&gt;(iOS: ViewController; Android: Activity or fragment; Vue:.vue)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Divide the page UI into different components, with each component corresponding to a &lt;code&gt;ViewModel&lt;/code&gt;. UI text is defined as a variable within the &lt;code&gt;ViewModel&lt;/code&gt;, while UI click events are defined as closures.&lt;/li&gt;
&lt;li&gt;Establish a &lt;code&gt;Mediator&lt;/code&gt; that, upon completion of the API request, initializes all &lt;code&gt;ViewModel&lt;/code&gt; variables and closures based on the data returned from the backend. Refer to &lt;a href="//#IV.%20Code%20Implementation"&gt;IV. Code Implementation&lt;/a&gt; for specific details.&lt;/li&gt;
&lt;li&gt;Provide the &lt;code&gt;Mediator&lt;/code&gt; to UI developers, who can directly bind and render the UI based on the data contained within the &lt;code&gt;Mediator&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  III. Specific Implementation and Detailed Requirements (Reference Solution)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;##### Split each page into separate &lt;code&gt;PageMediator&lt;/code&gt; instances, following the division of pages.&lt;/li&gt;
&lt;li&gt;##### Divide a page into different components based on "rows", where each component corresponds to a specific &lt;code&gt;ViewModel&lt;/code&gt;, all managed through the &lt;code&gt;PageMediator&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;##### Utilize a &lt;code&gt;MediatorManager Singleton&lt;/code&gt; for each &lt;code&gt;PageMediator&lt;/code&gt; (in the H5 demo, can use &lt;code&gt;Vuex&lt;/code&gt;). This approach ensures data is keep alive, whereas UI elements are not keep alive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;MediatorManager Singleton&lt;/code&gt; manages &lt;code&gt;PageMediators&lt;/code&gt;, and each &lt;code&gt;PageMediator&lt;/code&gt; manages &lt;code&gt;ViewModels&lt;/code&gt;&lt;/strong&gt; as outlined below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MediatorManager.getSingleton().mediator = new Mediator
MediatorManager.getSingleton().mediator = null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;h5&gt;
  
  
  If a page contains numerous components, the &lt;code&gt;PageMediator&lt;/code&gt; could become complex. In such scenarios, you could utilize appropriate &lt;code&gt;design patterns&lt;/code&gt; to refactor the &lt;code&gt;PageMediator&lt;/code&gt;. The specific approach to splitting depends on individual preferences and architectural capabilities.
&lt;/h5&gt;&lt;/li&gt;
&lt;li&gt;&lt;h5&gt;
  
  
  &lt;strong&gt;It's crucial to carefully consider &lt;code&gt;public&lt;/code&gt; &lt;code&gt;variables&lt;/code&gt; and &lt;code&gt;methods&lt;/code&gt; within &lt;code&gt;PageMediator&lt;/code&gt;.&lt;/strong&gt; As long as the exposed API for UI is sound, subsequent logic refactoring won't impact the UI, and altering the UI will have minimal effect on the logic.
&lt;/h5&gt;&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;
  
  
  In practical development, other modules should also be encapsulated to facilitate future use of &lt;code&gt;Unit Test&lt;/code&gt; as a replacement for &lt;code&gt;UI Test&lt;/code&gt;. For instance, modules like Router, Toast, and Network. For example, for &lt;code&gt;Toast&lt;/code&gt;:
&lt;/h5&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ToastViewModel {
    // use ReactiveX replace setter
    set toast(value) {
        Toast(value)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  IV. Code Implementation
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vtdtjuq538tff9fch7g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vtdtjuq538tff9fch7g.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following code demonstrates that Vue uses bind, iOS uses tableView, and Android uses Adapter for rendering. Data binding is performed within each page's component, while all the logic is centralized in the Mediator.&lt;/p&gt;

&lt;p&gt;VUE&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mediator {
  username_text = "username"
  password_text = "password"
  _username_str = ""
  _password_str = ""
  login_btn_disabled = true

  constructor() {
    this.init()
  }
  init() {}

  set username_str(value) {
    this._username_str = value
    this.update_login_btn_disabled()
  }
  get username_str() {
    return this._username_str
  }

  set password_str(value) {
    this._password_str = value
    this.update_login_btn_disabled()
  }
  get password_str() {
    return this._password_str
  }

  update_login_btn_disabled() {
    this.login_btn_disabled = !(this.username_str?.length &amp;amp;&amp;amp; this.password_str?.length)
  }

  onSubmit() {
    if (this.username_str == "admin" &amp;amp;&amp;amp; this.password_str == "123456") {
      router.back()
    } else {
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Android&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ButtonViewModel (
    val buttonState: BehaviorSubject&amp;lt;Boolean&amp;gt; = BehaviorSubject.createDefault(false),
    var buttonText: BehaviorSubject&amp;lt;String&amp;gt; = BehaviorSubject.createDefault(""),
) {
    var clickItem = {}
}

class InputViewModel (
    val text: BehaviorSubject&amp;lt;String&amp;gt; = BehaviorSubject.createDefault(""),
    val value: BehaviorSubject&amp;lt;String&amp;gt; = BehaviorSubject.createDefault("")
) {}

class Mediator : BaseMediator () {
    val usernameViewModel: InputViewModel = InputViewModel()
    val passwordViewModel: InputViewModel = InputViewModel()
    val buttonViewModel: ButtonViewModel = ButtonViewModel()

    init {
        usernameViewModel.text.onNext("username")
        passwordViewModel.text.onNext("password")

        val isNotEmpty: (String, String) -&amp;gt; Boolean = { name: String, age: String -&amp;gt;
            name.isNotEmpty() &amp;amp;&amp;amp; age.isNotEmpty()
        }
        val d1 = Observable.combineLatest(usernameViewModel.value, passwordViewModel.value, isNotEmpty).subscribe {
            buttonViewModel.buttonState.onNext(it)
        }

        buttonViewModel.clickItem = {
            val username = usernameViewModel.value.value
            val password = passwordViewModel.value.value
            if (username == "admin" &amp;amp;&amp;amp; password == "123456") {
                routerSubject.onNext(R.layout.activity_main)
            } else {
                ToastManager.toastSubject.onNext("input error")
            }
        }

        compositeDisposable.add(d1)
    }

    val dataList by lazy { initList() }

    private fun initList(): List&amp;lt;Map&amp;lt;String, Any&amp;gt;&amp;gt; {
        val m1 = mapOf(Pair("viewType", R.layout.input_item), Pair("viewHolder", InputViewHolder::class.java), Pair("viewModel", usernameViewModel))
        val m2 = mapOf(Pair("viewType", R.layout.input_item), Pair("viewHolder", InputViewHolder::class.java), Pair("viewModel", passwordViewModel))
        val m3 = mapOf(Pair("viewType", R.layout.button_item), Pair("viewHolder", ButtonViewHolder::class.java), Pair("viewModel", buttonViewModel))

        return listOf&amp;lt;Map&amp;lt;String, Any&amp;gt;&amp;gt;(m1, m2, m3)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;iOS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ButtonCellViewModel: BaseViewModel {
    let login_btn_disabled = BehaviorRelay(value: false)
    var onSubmit = {}
}

class TextFieldCellViewModel: BaseViewModel {
    let text = BehaviorRelay(value: "")
    let value = BehaviorRelay(value: "")
}

class Mediator: BaseMediator {
    let usernameViewModel = TextFieldCellViewModel()
    let passwordViewModel = TextFieldCellViewModel()
    let buttonCellViewModel = ButtonCellViewModel()

    lazy var list: [[[String : Any]]] = {
        let arr: [[[String : Any]]] = [
            [
                ["model":usernameViewModel,"reuseIdentifier":textFieldCellReuseIdentifier],
                ["model":passwordViewModel,"reuseIdentifier":textFieldCellReuseIdentifier],
            ],
            [
                ["model":buttonCellViewModel,"reuseIdentifier":buttonCellReuseIdentifier]
            ]
        ]
        return arr
    }()

    override init() {
        super.init()

        initPasswordViewModel()
        initUsernameViewModel()
        initButtonCellViewModel()
    }

    func initUsernameViewModel() {
        usernameViewModel.text.accept("username")
    }
    func initPasswordViewModel() {
        passwordViewModel.text.accept("password")
    }
    func initButtonCellViewModel() {
        let combineLatest = Observable.combineLatest(usernameViewModel.value, passwordViewModel.value)

        combineLatest.map { (username: String, password: String) -&amp;gt; Bool in
            return username.count &amp;gt; 0 &amp;amp;&amp;amp; password.count &amp;gt; 0
        }.bind(to: buttonCellViewModel.login_btn_disabled).disposed(by: disposeBag)


        buttonCellViewModel.onSubmit = {
            combineLatest.subscribe( onNext: { (username: String, password: String) in
                if username == "Admin", password == "123456" {
                    RouterBehaviorSubject.onNext(RouterModel(type: .pop))
                } else {
                    ToastBehaviorSubject.onNext("input error")
                }
            }).dispose()
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  V. Advantages and Disadvantages
&lt;/h3&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compared to frameworks like VIPER, MVI, etc., the core idea is simpler and easier to grasp.&lt;/li&gt;
&lt;li&gt;Separation of UI and logic facilitates task decomposition and combination, enhancing code reusability.&lt;/li&gt;
&lt;li&gt;Properly decomposed, the &lt;code&gt;Mediator&lt;/code&gt; can be &lt;code&gt;unit test&lt;/code&gt; in place of &lt;code&gt;UI test&lt;/code&gt;; &lt;strong&gt;this facilitates straightforward white-box automated testing&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Thanks to the presence of the &lt;code&gt;MediatorManager Singleton&lt;/code&gt;, data remains alive, while UI doesn't; data is centralized, enabling easy management.&lt;/li&gt;
&lt;li&gt;Uniform business code structure allows developers to quickly take over others' code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Developers not paying attention can easily cause memory leaks, which may be challenging to locate.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  VI. Conclusion
&lt;/h3&gt;

&lt;p&gt;From a practical development perspective, this framework is exceptionally well-suited for H5 applications. For example, in the demo (Vue), breaking down components into &lt;code&gt;.vue&lt;/code&gt;, &lt;code&gt;.scss&lt;/code&gt;, and &lt;code&gt;.js&lt;/code&gt; files greatly enhances code reusability, particularly due to the independent nature of CSS files.&lt;/p&gt;

&lt;p&gt;For H5, iOS, and Android, utilizing the &lt;code&gt;Mediator&lt;/code&gt; for &lt;code&gt;Unit test&lt;/code&gt; in place of &lt;code&gt;UI test&lt;/code&gt; can significantly reduce errors and enhance testing efficiency.&lt;/p&gt;

&lt;p&gt;Personally, I am highly impressed with this approach. It substantially boosts the efficiency of automated testing, making UI test much less cumbersome compared to the convenience of Unit test.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>ios</category>
      <category>android</category>
      <category>designpatterns</category>
    </item>
  </channel>
</rss>
