How I use Factory Method Pattern on Android Development

Fahrizal Sentosa
3 min readJan 16, 2022

--

Image from https://wallpaperaccess.com/factory

As Software engineers, we often develop an app with a single view but there are variant behaviors. Before I know the design pattern, I solved it with the condition in every single function!

Solution using all conditions for every single function will be going to make the dirty code, hard to read, duplicate code, and should be hard to maintain. Even when I just want to add one little change, I often need to change the whole feature.

Fortunately, I have repented by reading some of the books

I want to share when I create the CRUD page using the factory method pattern. Please take a look at my example below

If we see from the developer's perspective, there are functions and varied behavior. We need to define the varied things and separate them!

For example, when the page state is New, Input Field of ID, and Name is blank. But when the state is Edit or Delete, input field is filled. These behaviors vary.

Let’s list the behavior that varies

  1. Page properties (enable/disable, text value of EditText and Button) is vary
  2. Filled value of id and name is vary
  3. Button action is vary

Instead of using conditions on each function (see the code below)

fun getPageProperties(state : String) {    pageLiveData.value = when(state){
ViewState.EDIT ->
EmployeeDetailPageState(ViewState.EDIT, "Edit")
ViewState.DELETE ->
EmployeeDetailPageState(ViewState.DELETE, "delete")
else -> EmployeeDetailPageState(ViewState.NEW, "Create New")
}
}
fun getEmployee(state : String) {
when (state) {
ViewState.EDIT -> getEmployee()
ViewState.DELETE -> getEmployee()
else -> getBlankEmployee()
}
}
fun performActionButton(state : String) {
when (state) {
ViewState.EDIT -> editEmployeeDetail()
ViewState.DELETE -> deleteEmployeeDetail()
else -> createNewEmployeeDetail()
}
}

I think it is better to separate the condition and make them loosely coupled. Also, ensure the changes do not duplicate the code.

Factory Method Pattern implementation

The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

  1. Create an interface for varying the behavior
interface EmployeeDetail {

fun getPageState(): EmployeeDetailPageState
fun getEmployee(): Employee fun performAction()
}

2. Create classes with implementing an interface

Create new classes NewEmployeeDetail, EditEmployeeDetail, DeleteEmployeeDetail with implement EmployeeDetail like the diagram below

3. Create factory class

class EmployeeDetailFactory {

fun create(state: String): EmployeeDetail {
return when (state) {
ViewState.EDIT -> EditEmployeeDetail()
ViewState.DELETE -> DeleteEmployeeDetail()
else -> NewEmployeeDetail()
}
}
}

The condition is still needed here, but just once. Function create invoked when Activity/Fragment got state from bundle.

I will show you an example when implementing on setup page:

Explanation:

  1. Object employeeDetail is created by employeeDetailFactory. The creation depends on state. Yes, we still need the condition here, but to be noted that this is only once. After the state is chosen, all functions applied the state, and no need condition on every single function.
  2. When invoking function getPageProperties, there are 3 implementations. In this way, we succeed in decoupling the code from viewModel and getPageProperties behavior, also decoupling with various implementations. Imagine there’s a change on Edit behavior, then don’t need to impact to viewModel, New behavior, and Delete behavior.

The Factory Method Pattern is useful if you’ve only got one concrete creator because you are decoupling the implementation of the behavior page from its use. If you add additional action or change a behavior page implementation, it will not affect your Creator.

Source Code: https://github.com/fahrizal89/factory_method_pattern_android

Book:
https://www.amazon.com/Head-First-Design-Patterns-Object-Oriented/dp/149207800X/ref=sr_1_1?keywords=head+first+design+patterns&qid=1642360695&sprefix=headfirst+desi%2Caps%2C299&sr=8-1

--

--