Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Refactoring code Part 1
- //Refactoring: cleaning up and making the code more efficient
- Step 1.
- // Right now we have all of our code in this FunFactsActivity class and
- // its working fine this way but what if we wanted to add, say,
- // a new screen that also randomly selected one of our facts.
- // We could copy all of this code and add it to the new screen's activity, but
- // then we'd have the same code doing the same thing in two different places.
- // Then if we change this code for some reason,
- // we'd have to remember to make the same change in the code for the other activity.
- // This violates a core principle of software development,
- // the you don't repeat yourself principle.
- // A better solution is to move the code that is repeated to a more usable component.
- // In this case that could be a new object that can be used in multiple activities.*
- //Also only give each class/activity a single responsibility #SingleResponsibilityPrinciple
- //-----------------------------------------------------------------------------------------------------------------------------------------
- //FunFactsActivity.java
- Step 2.
- // Now we want to use the new method that we just created (in FactBook.java) in our new FactBook object.
- // But first we need to instantiate a FactBook object.
- // We will do it similarly to how we created a new onClick listener up here.
- // Or, a new random object in the FactBook class.
- // We're gonna use this new FactBook object to do the work of getting an answer for us when the button is tapped.
- // Now we could create it here in the onClick method. But if we do that then a FactBook object
- // will be created every time we tap on a button.
- // That seems inefficient doesn't it?
- // What if our object were big and complex with thousands of answers to choose from?
- // That means we'd be wasting processing time in recreating the Factbook for each tab.
- //Instead, let's do it one time and make it available throughout the life of our app.
- // This way, we only have the overhead of creating a FactBook object one time.
- private FactBook mFactBook = new FactBook(); // the little 'm' stands for member variable //member variable = things about an object
- public class FunFactsActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_fun_facts);
- final TextView factLabel = (TextView) findViewById(R.id.factTextView);
- Button showFactButton = (Button) findViewById(R.id.showFactButton);
- View.OnClickListener listenerName = new View.onClickListener(){
- @Override
- public void onClick(View view) {
- //from private FactBook
- String fact = mFactBook.getFact();
- factLabel.setText(fact); //
- }
- };
- showFactButton.setOnClickListener(listenerName);
- }
- }
- //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
- //-----------------------------------------------------------------------------------------------------------------------------------------
- //FactBook.java
- public class FactBook {
- //Member variable (properties about the object)
- public String getFact() { //Public = can be accessed by anyone // String tells us it will return a String data type when done.
- *// Let's make a new object that separates this part out for us.
- // Not only will it make our code a little cleaner, but
- // it will also prevent us from repeating ourselves in code, and
- // it will give us a little more practice with objects too.
- String[] facts = {
- "Ants stretch when they wake up in the morning.",
- "Ostriches can run faster than horses.",
- "Olympic gold medals are actually made mostly of silver.",
- "You are born with 300 bones; by the time you are an adult you will have 206.",
- "It takes about 8 minutes for light from the Sun to reach Earth.",
- "Some bamboo plants can grow almost a meter in just one day.",
- "The state of Florida is bigger than England.",
- "Some penguins can leap 2-3 meters out of the water.",
- "On average, it takes 66 days to form a new habit.",
- "Mammoths still walked the earth when the Great Pyramid was being built." };
- String fact = "";
- Random randomGenerator = new Random ();
- int randomNumber = randomGenerator.nextInt(facts.length);
- fact = facts[randomNumber];
- return fact; //return = I am done with this method, here is the result.
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------------------------
- https://teamtreehouse.com/library/build-a-simple-android-app-new/improving-our-code/simple-refactoring-creating-a-class
- 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