Business, Code, Tech

Moxy Strategy. Something You Have Never Heard Before

Android development is fun, exciting and filled with exciting possibilities. Moxy is a library which provides MVP components for Android. If you’re a developer or you’re hiring a developer, Moxy strategies may be a smart choice for your team.

But if you’re working with a dedicated development team, it’s best if you allow the team to determine which libraries and frameworks are best suited for your apps.

What is MVP Anyway?

Moxy brings MVP to Android, but what is MVP? MVP stands for:

  • Model
  • View
  • Presenter

You’ll mostly be using MVP for user interfaces. If you’ve heard of MVC before or model-view-controller, you’ll note that while similar, there is a difference between MVP and MVC.

What are MVP and MVC and what is the difference?

The MVP uses the presenter as a middleman. All the view actions, of interactions with the app user, will be passed to the presenter that will then pass anything necessary to the model, which doesn’t directly communicate with the view.

Instead, the model will fire events that are passed to the presenter and returned to the view.

When looking at all these elements, you may be confused as to what each element does. The presenter handles all the business logic, while the model contains all of the data that is displayed in the view.

A view handles what the end user sees.

An MVP is just another way to make logical sense between components when developing. Since all three of these elements are separate, you can make code more modular and easier to update, too.

What’s the Benefit of an MVP Pattern?

Android Architecture with Multi-Screen MVP (Part 2)

If you’re not used to using an MVP pattern, it can be difficult to know why using one of these patterns is beneficial. The simple answer is: logic. When all coding is divided into the MVP, it allows for coding to be put into logical parts.

You’re able to make changes in the presenter without impacting other code.

The lines of code are also cut down, so you don’t have to deal with files with thousands of lines of coding that can be tedious and time-consuming to read or update.

Everything else becomes easier, too:

  • Adding features
  • Testing

What are Moxy Strategies?

MVP for Android Part 2: Featuring Moxy

You know what Moxy and MVPs are and their benefits, but what are Moxy strategies? Strategies involve command queues that are used by the View.  The View allows commands when the View is in an active state, and improper strategy usage can lead to memory overflow or execution errors.

Moxy includes several strategies by default that enable you to manipulate the commands going to the view. The strategies include:

  • AddToEndStrategy
  • AddToEndSingleStrategy
  • SingleStateStrategy
  • SkipStrategy
  • OneExecuteStrategy

You can use each of these strategies to your advantage to force commands to do something outside the norm of the normal execution pattern. For example:

AddToEndStrategy

When you need to add a command to the end of a queue, this is the strategy to use. You’ll execute the queue in order, and add something to the end of the queue.

AddToEndSingleStrategy

If you need to delete all previous instances, you can use this strategy to execute a command at the end of a queue and then delete previous instances.

SingleStateStrategy

Sometimes, you’ll want to execute a command, clear out a queue and then add commands of your own. This is the strategy to use to achieve this.

SkipStrategy

A strategy that simply executes the command.

OneExecuteStrategy

When you need to execute a command ASAP, you would use this strategy.

Of course, all strategies are very case-specific. Most people will want to use the AddToEndStrategy because it is the strategy that is simplest and has the lowest risk of error or causing hiccups in your app’s functionality.

If you use a strategy improperly and without a lot of testing, logic errors can occur and unwanted side effects will also exist. Strategies allow you to break free from Moxy logic and essentially override or change queues, but there is always risks of errors if the strategy is not thoroughly tested.

There may be times when a built-in strategy is inefficient or doesn’t perform in the way you need it to perform. Moxy does allow you to create custom strategies that are powerful and will allow you to alter the ViewState command queue with greater precision.

If you like this, You'll love These.

You Might Also Like