Introduction to Factory Pattern on Android
Factory Pattern is one of the most popular creational pattern out there. I myself have used it in several projects, one of such is an open source cross platform app written in Dart Joker App. But you can check out CoffeeMaker if you just want to see the codes and don't care about explanations.
Factory Pattern, as the name implies uses factory methods to handle the problem of having to create multiple objects without specifying the exact class of object that will be created. In Android(Kotlin/Java), this is achieved by implementing a common interface in classes and providing access to the class objects through a Factory method. The best way to see how this works is to build a project that put all I have just explained into action. Open Android Studio and Start a new project.We're going to build a mini sample app that generate an object to represent different variants of Coffee. To keep things simple, I will be focusing on the pattern alone. The app will return name and recipes of each coffee variant in string.
How to Build Factory Pattern
- I presumed that you've created a new project in your Android Studio, now edit the MainActivity.java
- Right-click it and create a New Kotlin File/Class of interface called Coffee.
- Edit the interface and create two methods namely name and recipes inside it.
interface Coffee {
fun name(): String
fun recipe(): String
}
- Create concrete classes that implements the Coffee interface
class CaffeLatte : Coffee {
override fun name(): String ="CaffeLatte"
override fun recipe(): String ="Expresso"
}
class Americano : Coffee {
override fun recipes(): String = "Expresso, Hot water"
override fun name(): String = "Caffè Americano"
}
- Now, create a new class called CoffeeFactory that look like the code snippet below:
object CoffeeFactory {
enum class Type{
LATTE, AMERICANO
}
fun getCoffee(type: Type): Coffee{
if (type == CoffeeFactory.Type.LATTE){
return CafeLatte()
}else if(type == CoffeeFactory.Type.AMERICANO){
return Americano()
}
throw IllegalArgumentException("Can't handle your command ${type.name}")
}
}
We're done with the Pattern, now lets display the coffee recipes and name on the screen.
Modify your activity_main.xml layout. Copy/Paste below code in there:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:padding="10dp"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Hello World!"
android:paddingBottom="5dp"
android:id="@+id/coffee_name"
android:textAppearance="?android:textAppearanceMedium"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:paddingBottom="10dp"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/coffee_recipe"
android:textAppearance="?android:textAppearanceMedium"
app:layout_constraintTop_toBottomOf="@id/coffee_name"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Type"
android:id="@+id/coffee_btn"
android:textAppearance="?android:textAppearanceMedium"
app:layout_constraintTop_toBottomOf="@id/coffee_recipe"/>
</android.support.constraint.ConstraintLayout>
Edit the MainActivity.java file. Reference the TextViews from the layout and set the texts. The button will change the text when it's clicked
class MainActivity : AppCompatActivity() {
private var isChanged: Boolean =false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Set button click
coffee_btn.setOnClickListener(this::changeCoffee)
}
/**
* Change coffee recipe and name when button is clicked
*/
fun changeCoffee(view: View){
var factory: Coffee
if (isChanged){
factory = CoffeeFactory.getCoffee(CoffeeFactory.Type.AMERICANO)
isChanged = false
}else{
factory = CoffeeFactory.getCoffee(CoffeeFactory.Type.LATTE)
isChanged = true
}
changeCoffeeType(factory.recipes(), factory.name())
}
/**
* Method that changes the coffee type
*/
private fun changeCoffeeType(recipe: String, name: String){
coffee_recipe.text = recipe
coffee_name.text =name
}
}
Yay! we're done. Now you can run your app on real device or emulator.
It would be convenient that Android allowed you to pay with your phone almost anywhere your heart desires. This is especially true for online casinos and online casino games, where it is necessary for gamblers and casino players. For example, https://casinohex.org/canada/casinos/pay-by-phone/ shows online casino sites where you can pay by phone.