Wednesday, January 29, 2014

WeakReference example

WeakReference is a great mechanism.
Usual references are called Strong References.
For example,
string referenceToString = new string("The string!");

Garbage Collector collects and destroys the object if there is no any Strong reference to it.

string referenceToString = null;
If there is no strong reference to "The string!" then it will be collected by garbage collector and destroyed.

WeakReference is a wrapper on top of a Strong reference with one core distinction "WeakReference does not protect from garbage collection".

string referenceToString = new string("The string!");
var wr = new WeakReference(referenceToString);
referenceToString = null;

// some execution time left
// here wr can point to: 
// 1) NULLL if it was garbage collected or
// 2) object "The string!" if it was not garbage collected

var wrTarget = wr.Target as string; // return internal object of weak reference

Usually weak references are used wrong. Take the dialog from my old interview:
Interviewer: What is WeakReference?
Me: I did not know.
Interviewer: (answers the definition)
Me: But where it can be applied?
Interviewer: Mmmm... for example, in a cache. You can have very heavy resources, and resource can be recreated after WeakReference internal object will be garbage collected.


It is very hard to find good example of proper usage for WeakReference. Why on the earth cache mechanism should depend on Garbage Collection time? There are lots of customisable and convenient solutions for caching.

One of the great example was found for Android development (Java).
UI thread can be locked if the image will be loaded not from memory (disk, net). This can be solved by creating asynchronous task that will load the image. The problem arises if user goes out from current page of the application or Android unloads invisible part of the page. Then the image container on the page (ImageView) will not be needed at all when async operation finishes. This can be smartly solved with WeakReference:

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public BitmapWorkerTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }
    // Method for getting bitmap is removed for code clearness

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}
Now image container can be garbage collected.

Monday, January 13, 2014

Prettify does not work with Blogger

The solution is a tricky one:
<script>
$(window.blogger.ui()).on('viewitem', function (event, post, element) {
    prettyPrint();
});
</script>

Source: http://stackoverflow.com/a/14659603/304371


Thursday, January 9, 2014

Software patterns problem/ Alexander patterns

Software patterns were provided by the Gang of Four (Erich Gamma, ...) in the book Design Patterns: Elements of ReusableObject-Oriented Software in 1994. The main concept Pattern is provided as reusable behaviour that developers can use and share among each other. The pattern concept is got from the book A Pattern Language written by Christopher Alexander (architect).

Developers communicates with people very poor. And the Gang of Four took evil inside their book by interchanging initial Alexander pattern concept. The Pattern by Alexander is not only a reusable instrument for specialists, but also is a part of common language that clients can understand and which resolve clients problem. 

For example, The pattern 107 Wings of Light explains the benefits of having natural light coming in from many directions. A room that has light coming in from only one direction feels flat and dead. Sunlight coming into a room from 2 or more directions gives the room dimension that triggers a positive emotional response.

How it can be applied to software development? Developers could talk to clients in common language and describe reusable features that can be applied:
  • Load data on scroll pattern (instead of loading full data)
  • Social network sign-on pattern (instead of only registration process)
  • etc

Eric Evans makes us closer to the clients in his book Domain Driven Design by introducing Ubiquitous language and Domain models.


If clients could describe what they want with such common patterns would it work in your projects?