Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Design Patterns in Mobile Applications

Design Patterns in Mobile Applications

The goal of these slides are to demonstrate use cases for design patterns and help solidify your understanding with simple code examples.

If you are a web or mobile developer you may not be aware that you use some of these patterns every day. This presentation will demonstrate how to apply design patterns to the iOS, Android, and Rails frameworks and give you a deeper understanding of when and how to use these patterns.

https://github.com/metova/design-patterns-sample

David Thacker

October 03, 2015
Tweet

Other Decks in Programming

Transcript

  1. NASHVILLE • ATLANTA • WASHINGTON D.C. • SEATTLE • LITTLE

    ROCK Metova, Inc. is a leading emerging tech company creating world-class apps along with strategy, design, development and marketing for customers ranging from Fortune 500 companies to startups. OUR CULTURE Quick • Agile • Effective COMMERCIAL FEDERAL EDUCATION 38 states 1500 districts
  2. 106+ employees! Franklin, TN and Conway, AR! Inc. 5000 list

    of fastest-growing companies every year since 2011! Android, iOS, Windows Phone, Blackberry mobile development! Ruby on Rails, Java, and .NET website/API development! Awesome technologists
  3. If you are new to mobile development 
 To provide

    best practices to start with" If you already have experience with mobile development
 To give a better understanding of the code you use" If you are aware of design patterns 
 To strengthen your understanding with some concrete examples
 GOALS
  4. Adapter Pattern Adapter allows a class to share useful components"

    Adapter allows a class to keep its existing implementation
  5. Android BaseAdapter public class ListAdapter extends BaseAdapter {
 
 


    
 public ListAdapter() {
 
 }
 
 @Override
 public int getCount() {
 
 }
 
 @Override
 public Restaurant getItem(int position) {
 
 }
 
 @Override
 public long getItemId(int position) {
 
 } 
 
 @Override
 public View getView(int position, View convertView, ViewGroup parent){
 
 }
 }
  6. Android BaseAdapter public class ListAdapter extends BaseAdapter {
 
 private

    ArrayList<Restaurant> mRestaurantList;
 
 public ListAdapter(ArrayList<Restaurant> restaurantList) {
 mRestaurantList = restaurantList;
 }
 
 @Override
 public int getCount() {
 return mRestaurantList.size();
 }
 
 @Override
 public Restaurant getItem(int position) {
 return mRestaurantList.get(position);
 }
 
 @Override
 public long getItemId(int position) {
 return getItem(position).getGenerated_id();
 } 
 
 @Override
 public View getView(int position, View convertView, ViewGroup parent){
 // create and return a new view
 }
 }
  7. Android BaseAdapter Provides a standard protocol for app developers to

    send data to a list view." Can easily be used with any type of data collection" Gives benefits like view recycling so the app developer doesn’t have to worry about implementation
  8. Flyweight Pattern Flyweight allows for sharing of common data "

    Flyweight increases performance by using small shared objects" Objects should only have to be created once" Useful if it is easy for the client to provide attributes
 

  9. Flyweight Pattern public class LinkFlyweight{
 
 public final Sword sword;


    public final Shield shield;
 public Tunic tunic;
 public int hitPoints;
 
 public LinkFlyweight(int color, int hitPoints) {
 this.sword = new Sword();
 this.shield = new Shield();
 this.tunic = new Tunic(color);
 this.hitPoints = hitPoints;
 }
 
 } Separate intrinsic (immutable) and extrinsic (mutable)" Flyweight object provides intrinsic, client provides extrinsic
  10. Android ViewHolder Pattern public class ViewHolder {
 
 public final

    ImageView image;
 public final TextView name;
 public final TextView location;
 public final ImageView stars;
 public final ImageView type;
 
 public ViewHolder(View view) { // the findViewById() call is expensive
 image = (ImageView) view.findViewById(R.id.image);
 name = (TextView) view.findViewById(R.id.name);
 location = (TextView) view.findViewById(R.id.location);
 stars = (ImageView) view.findViewById(R.id.stars);
 type = (ImageView) view.findViewById(R.id.type);
 
 view.setTag(this);
 }
 
 }
  11. Android ViewHolder Pattern @Override
 public View getView(int position, View convertView,

    ViewGroup parent) {
 Context context = parent.getContext();
 Restaurant restaurant = getItem(position); 
 ViewHolder holder; " //..
 
 return convertView;
 }
  12. Android ViewHolder Pattern @Override
 public View getView(int position, View convertView,

    ViewGroup parent) {
 Context context = parent.getContext();
 Restaurant restaurant = getItem(position); 
 ViewHolder holder; " // Only create each ViewHolder once
 if (convertView == null) {
 convertView = LayoutInflater.from(context).inflate( R.layout.restaurant_item, parent); 
 holder = new ViewHolder(convertView);
 } else {
 holder = (ViewHolder) convertView.getTag();
 }
 
 //..
 
 return convertView;
 }
  13. Android ViewHolder Pattern @Override
 public View getView(int position, View convertView,

    ViewGroup parent) {
 Context context = parent.getContext();
 Restaurant restaurant = getItem(position); 
 ViewHolder holder; " // Only create each ViewHolder once
 if (convertView == null) {
 convertView = LayoutInflater.from(context).inflate( R.layout.restaurant_item, parent); 
 holder = new ViewHolder(convertView);
 } else {
 holder = (ViewHolder) convertView.getTag();
 }
 
 if (restaurant instanceof Burger) {
 holder.type.setImageResource(R.drawable.burger);
 } else {
 holder.type.setImageResource(R.drawable.chicken);
 }
 
 holder.name.setText(restaurant.getName());
 holder.location.setText(restaurant.getCity());
 
 return convertView;
 }
  14. Android ViewHolder Pattern Avoids having to perform a lookup for

    each view’s view hierarchy" Increases the performance of the ListView and creates smoother scrolling" The ViewHolder is similar but not an exact implementation of the Flyweight pattern
  15. OBSERVER PATTERN Observer patterns allow objects to be notified of

    certain events" The iOS SDK provides a basic publish/subscribe system called NSNotificationCenter
  16. DELEGATION PATTERN A delegate is an object that acts on

    behalf of, or in coordination with, another object when that object encounters an event Delegation involves two objects: the delegate, and the delegating object A common example of a delegating object is a UITableView