Tuesday, June 26, 2012


Suppose we have an object, how to throw from the current level of activity the other an activity



I do not know XD


Joke, Internet search, found a design pattern called Singleton really super

Briefly explain

Suppose there is a category, set inside the Activity
But you want this category to keep, and then to an Activity inside out
You how to do it? This time you will think of a static class, but you How can we guarantee
You were born out of the category, will not be abused it?
This time, Singleton is a good way.
It allows you to only have one object.
Suppose you have a class called Singleton long,
class Singleton{ public Singleton(){} } 


You want to create objects in this category
You may do so
 Singleton single = new Singleton(); 


But if you let the others have no way of freedom to establish this category of objects
You can do
 class Singleton{ private Singleton(){} } 


So who can access?
Of course, only be able to access the Hello!
So the next you can write,
 class Singleton{ private static Singleton single; private Singleton(){} public static Singleton getInstance(){ single = new Singleton(); } return single; } 


As a result, just call Singleton.getInstance () can produce an object.

But that there is not right, if everyone has been the call A.getInstance ();
Not or to create a bunch of objects, so add a few lines limit
  1. class Singleton {
  2. private static Singleton single;
  3. private Singleton () {}
  4. public static Singleton getInstance () {
  5. if (single == null) {
  6. single = new Singleton ();
  7. }
  8. return single;
  9. }
  10. }

As a result, can only have one object, but this time, but there is a problem,
Suppose there are two threads to access this category getInstance this method,
If we go straight order becomes this:

A thread went to the fifth line to
B thread went to the fifth line to the
A thread in the sixth line of the establishment of an object.
B thread in the sixth line of the establishment of an object.

Violation of the principle of Singleton: the establishment of an object.

So we just want the getInstance () into a synchronized method, you can solve the problem of the above out.
 class Singleton{ private static Singleton single; private Singleton(){} public static synchronized Singleton getInstance(){ if(single == null){ single = new Singleton(); } return single; } } 


ya, resolved.

Not.

synchronized is a waste of performance, if a large number of calls this method, your computer will be a group of energy-consuming thread to torture your computer, and does not make sense after the fact, if the first complete new, simply do not need to getInstance do sync.

Therefore, we developed an idea that if we start a program on your object new?
 class Singleton{ private static Singleton single = new Singleton(); private Singleton(){} public static Singleton getInstance(){ return single; } } 


In this way, though a small L azy Initialization (delay in the feces of initialization: the need to use an object when it is established),
If your category is not to eat a lot of resources, in fact, so it is good enough.

We returned to the original topic: how to make an object in a lot of activity shuttle it?

Suppose you write the category of a cat
 class Cat{ private static Cat candy = new Cat(); private Cat(){} public static Cat getMyCat(){ return candy; } //其他方法} 

And then the A activity
 public class A extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Cat candy = Cat.getMyCat(); //作一些設定Intent i = new Intent(); i.setClass(A.this, B.class); startActivity(i);//跳到B頁面} } 


And then B activity display
 public class B extends Activity{ public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Cat candy = Cat.getMyCat(); //取得一些資訊//display } } 


This said that no matter where you can access the Cat class objects,
Also equivalent to the spread of an object to another one the Activity.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © .Hacking Cracking Tricks And Tutorials, Paid Scripts, Latest Exploits, 0Day Vulnerability, - Skyblue - Powered by Blogger - Designed by Johanes Djogan -