The Factory pattern handles instantiations of objects and abstracts it away from the calling class using a factory method. This is commonly achieved by implementing a specific interface or extending an abstract class in the objects and accessing them through a static method.
The project would return the types of coffee. Additionally, the sample codes in this tutorial are written in Kotlin, but you can apply the same concepts to Android apps built with Java.
3. Edit the interface, and create two functions that return string. Name the function `name` and `recipes` respectively.
4. Create two classes that implement the Coffee interface.
How To Set Up A Factory Pattern For Your Project
Create a new project in your Android Studio or whatever IDE you're using. We're going to be building a mini sample app that generates different types of Coffee drinks - who doesn't like coffee!. Let's move on. To keep things simple, I will be focusing on the pattern alone.The project would return the types of coffee. Additionally, the sample codes in this tutorial are written in Kotlin, but you can apply the same concepts to Android apps built with Java.
- I assumed you've created a new project in your IDE. Now edit the `MainActivity.java` that was generated when you created your project.
- Create a new kotlin interface called Coffee.
3. Edit the interface, and create two functions that return string. Name the function `name` and `recipes` respectively.
interface Coffee {
fun name(): String
fun recipe(): String
}
4. Create two classes that implement 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"
}
5. Create another kotlin class and call it `CoffeeFactory`. Copy/paste below code snippet into your newly created factory class
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 creating the Factory Pattern, now lets put the pattern into use.
- Modify your
activity_main.xmllayout. 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>
- Modify the
MainActivity.javafile. Set up the views properly. The button will be responsible for changing the text when we click it, using the factory method.
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. If you have any questions regarding this tutorial, please let me know through the comment section.Sign up here with your email


ConversionConversion EmoticonEmoticon