Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android Design Patterns and Best Practice

You're reading from   Android Design Patterns and Best Practice Create reliable, robust, and efficient Android apps with industry-standard design patterns

Arrow left icon
Product type Paperback
Published in Dec 2016
Publisher Packt
ISBN-13 9781786467218
Length 370 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Kyle Mew Kyle Mew
Author Profile Icon Kyle Mew
Kyle Mew
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Design Patterns FREE CHAPTER 2. Creational Patterns 3. Material Patterns 4. Layout Patterns 5. Structural Patterns 6. Activating Patterns 7. Combining Patterns 8. Composing Patterns 9. Observing Patterns 10. Behavioral Patterns 11. Wearable Patterns 12. Social Patterns 13. Distribution Patterns

The factory pattern

The factory pattern is one of the most widely used creational patterns. As its name suggests, it makes things, or more precisely, it creates objects. Its usefulness lies in the way it uses a common interface to separate logic from use. The best way to see how this works is simply to build one now. Open the project we began a page or two previously, or start a new one. Minimum and target SDK levels are not important for this exercise.

Tip

Selecting an API level of 21 or higher allows Android Studio to employ a technology known as hot-swapping. This avoids having to completely rebuild a project each time it is run and vastly speeds up the testing of an app. Even if you intend to finally target a lower platform, the time hot-swapping saves makes it well worth your while lowering this target once the app is as good as developed.

We are going to build a very simple example app that generates objects to represent the different types of bread our sandwich builder app might offer. To emphasize the pattern, we will keep it simple and have our objects return nothing more sophisticated than a string:

  1. Locate the MainActivity.java file in the project view.
  2. Right-click it and create a New | Java Class of KindInterface called Bread:

    The factory pattern

  3. Complete the interface as follows:
        public interface Bread { 
     
            String name(); 
            String calories(); 
        } 
    
  4. Create concrete classes of Bread, like so:
        public class Baguette implements Bread { 
     
            @Override 
            public String name() { 
                return "Baguette"; 
            } 
     
            @Override 
            public String calories() { 
                return " : 65 kcal"; 
            } 
          } 
     
          public class Roll implements Bread { 
     
            @Override 
            public String name() { 
                return "Roll"; 
            } 
      
            @Override 
            public String calories() { 
                return " : 75 kcal"; 
            } 
          } 
     
          public class Brioche implements Bread { 
     
            @Override 
            public String name() { 
                return "Brioche"; 
            } 
     
            @Override 
            public String calories() { 
                return " : 85 kcal"; 
            } 
        } 
    
  5. Next, create a new class called BreadFactory that looks like this:
    public class BreadFactory { 
     
        public Bread getBread(String breadType) { 
     
            if (breadType == "BRI") { 
                return new Brioche(); 
     
            } else if (breadType == "BAG") { 
                return new Baguette(); 
     
            } else if (breadType == "ROL") { 
                return new Roll(); 
            } 
     
            return null; 
        } 
    } 
    

UML diagrams

The key to understanding design patterns lies in understanding their structure and how component parts relate to each other. One of the best ways to view a pattern is pictorially, and the Unified Modeling Language (UML) class diagrams are a great way to accomplish this.

Consider the pattern we just created expressed diagrammatically, like so:

UML diagrams

With our pattern in place, all that is required is to see it in action. For this demonstration, we will make use of the TextView in our layout that the template generated for us and the onCreate() method that is called every time our main activity is started:

  1. Open the activity_main.xml file in Text mode.
  2. Add an id to the text view, like so:
    <TextView 
        android:id="@+id/text_view" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
    
  3. Open the MainActivity.java file and edit the onCreate() method to match the following code:
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
     
        TextView textView = (TextView) findViewById(R.id.text_view); 
     
        BreadFactory breadFactory = new BreadFactory(); 
        Bread bread = breadFactory.getBread("BAG"); 
     
        textView.setText(new StringBuilder() 
                .append(bread.name()) 
                .toString()); 
    } 
    

    Tip

    Depending on how you have Android Studio set up, you may have to import the TextView widget: import android.widget.TextView;. Usually, the editor will prompt you and import the widget with a simple press of Alt + Enter.

You can now test the pattern on an emulator or real device:

UML diagrams

This may appear at first glance as an incredibly long-winded way to achieve a very simple goal, but therein lies the beauty of patterns. The added layers of abstraction allow us to modify our classes without having to edit our activity and vice versa. This usefulness will become more apparent as we develop more complex objects and encounter situations that require more than a single factory.

The example we created here is too simple to really require any testing, but now is as good a time as any to explore how we test Android apps on both real and virtual devices, as well as how we can monitor performance and use debugging tools to test output without having to add unnecessary screen components.

You have been reading a chapter from
Android Design Patterns and Best Practice
Published in: Dec 2016
Publisher: Packt
ISBN-13: 9781786467218
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image