Advertisement
yo2man

Refactoring code

Jun 20th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. //Refactoring code Part 1
  2. //Refactoring: cleaning up and making the code more efficient
  3.  
  4. Step 1.
  5. // Right now we have all of our code in this FunFactsActivity class and
  6. // its working fine this way but what if we wanted to add, say,
  7. // a new screen that also randomly selected one of our facts.
  8. // We could copy all of this code and add it to the new screen's activity, but
  9. // then we'd have the same code doing the same thing in two different places.
  10. // Then if we change this code for some reason,
  11. // we'd have to remember to make the same change in the code for the other activity.
  12. // This violates a core principle of software development,
  13.  
  14. // the you don't repeat yourself principle.
  15.  
  16. // A better solution is to move the code that is repeated to a more usable component.
  17. // In this case that could be a new object that can be used in multiple activities.*
  18.  
  19. //Also only give each class/activity a single responsibility #SingleResponsibilityPrinciple
  20.  
  21. //-----------------------------------------------------------------------------------------------------------------------------------------
  22. //FunFactsActivity.java
  23.  
  24. Step 2.
  25. // Now we want to use the new method that we just created (in FactBook.java) in our new FactBook object.
  26.  
  27. // But first we need to instantiate a FactBook object.
  28. // We will do it similarly to how we created a new onClick listener up here.
  29. // Or, a new random object in the FactBook class.
  30.  
  31. // We're gonna use this new FactBook object to do the work of getting an answer for us when the button is tapped.
  32. // Now we could create it here in the onClick method. But if we do that then a FactBook object
  33. // will be created every time we tap on a button.
  34. // That seems inefficient doesn't it?
  35.  
  36. // What if our object were big and complex with thousands of answers to choose from?
  37. // That means we'd be wasting processing time in recreating the Factbook for each tab.
  38.  
  39. //Instead, let's do it one time and make it available throughout the life of our app.
  40. // This way, we only have the overhead of creating a FactBook object one time.
  41.  
  42. private FactBook mFactBook = new FactBook();  // the little 'm' stands for member variable //member variable = things about an object
  43.  
  44. public class FunFactsActivity extends Activity {
  45. @Override
  46.     public void onCreate(Bundle savedInstanceState) {
  47.         super.onCreate(savedInstanceState);
  48.         setContentView(R.layout.activity_fun_facts);
  49.  
  50.         final TextView factLabel = (TextView) findViewById(R.id.factTextView);
  51.         Button showFactButton = (Button) findViewById(R.id.showFactButton);
  52.         View.OnClickListener listenerName = new View.onClickListener(){
  53.                
  54.                 @Override
  55.                 public void onClick(View view) {
  56.  
  57.         //from private FactBook
  58.         String fact = mFactBook.getFact();
  59.  
  60.                 factLabel.setText(fact); //
  61.                                }
  62.         };
  63.                
  64.         showFactButton.setOnClickListener(listenerName);
  65.     }
  66. }
  67.  
  68. //Delete: on and on (which we haven't shown/been focused on) because we don't need them and its good to clean up the code eventhough they are harmless
  69.  
  70.  
  71. //-----------------------------------------------------------------------------------------------------------------------------------------
  72. //FactBook.java
  73.  
  74. public class FactBook {
  75.     //Member variable (properties about the object)
  76.     public String getFact() {  //Public = can be accessed by anyone // String tells us it will return a String data type when done.
  77.        
  78.     *// Let's make a new object that separates this part out for us.
  79.     // Not only will it make our code a little cleaner, but
  80.     // it will also prevent us from repeating ourselves in code, and
  81.     // it will give us a little more practice with objects too.
  82.         String[] facts = {                                  
  83.                         "Ants stretch when they wake up in the morning.",                                
  84.                         "Ostriches can run faster than horses.",                                        
  85.                         "Olympic gold medals are actually made mostly of silver.",                      
  86.                         "You are born with 300 bones; by the time you are an adult you will have 206.",  
  87.                         "It takes about 8 minutes for light from the Sun to reach Earth.",              
  88.                         "Some bamboo plants can grow almost a meter in just one day.",                  
  89.                         "The state of Florida is bigger than England.",                                  
  90.                         "Some penguins can leap 2-3 meters out of the water.",                          
  91.                         "On average, it takes 66 days to form a new habit.",                            
  92.                         "Mammoths still walked the earth when the Great Pyramid was being built." };    
  93.              
  94.         String fact = "";
  95.  
  96.                 Random randomGenerator = new Random ();
  97.                 int randomNumber = randomGenerator.nextInt(facts.length);  
  98.  
  99.                 fact = facts[randomNumber];  
  100.        
  101.         return fact; //return = I am done with this method, here is the result.
  102.     }
  103. }
  104. //-----------------------------------------------------------------------------------------------------------------------------------------
  105. https://teamtreehouse.com/library/build-a-simple-android-app-new/improving-our-code/simple-refactoring-creating-a-class
  106.  
  107. https://teamtreehouse.com/library/build-a-simple-android-app-new/improving-our-code/simple-refactoring-using-a-class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement