Wednesday, July 21, 2010

Delegate

Usually if we want to add new behavior in an existing object, we do inheritance. For example if we have class Apple below:



If we want the Apple class to have a new behavior such as growAppleInDesert(), we have some options:
1. Put the new method in interface or parent class, IFruit. This will apply to all subclasses, however.
2. Put the new method in Apple class. This can be done; however, if our Apple class is a simple UI, adding the action growAppleInDesert() here will break layering pattern.
3. Subclass Apple to create new class, say DesertApple. This is what OO design pattern suggested.
4. Create new class, say AppleGrowAction which contains growAppleInDesert() , growAppleInMeadow(), etc. Our current Apple object can then refer to the new AppleGrowAction class to call its methods. This is known as delegate.
5. Using "Category" feature of Objective-C. This will be explained later.

Another example is if we want to create a new component from an existing one. We have some options:
1. We can inherit the existing component; however, if the component has so many methods, we will end up having those methods in our class.
2. Put the component as delegate, our new component will only need to call methods in existing component.

In Cocoa Framework, delegate usually implemented as protocol. For example if we have a control UITextField, we will need to implement UITextFieldDelegate protocol in order to handle text field event.



In Java it is common to do (3) or (4), but, in Objective-C, (4) is widely used. It is called delegation because we delegate some tasks to another object.

Category

Category is an Objective-C feature that allows us to extend the capability of a given class. This feature works even if we do not have access to the source code of the class we are extending. When we extend a given class through a category, the extension is inherited by all its subclasses. Of course, the additional methods defined by the category are only seen by our program.

To illustrate this feature, let us extend the NSObject class by adding an instance method to it:

Tuesday, July 20, 2010

Java reference, Objective-C retain count

Java

How Java's garbage collector knows that an object must be destroyed? An object is a candidate for garbage collector if they lose all its references. For example Object B below. It is referred by variable B1 and B2. Once both B1 and B2 lose reference to it, none knows that it exists, then it becomes candidate for garbage collector.



Objective-C

Objective-C on the other hand, uses different approach. Each object holds a variable named retainCount. It is set to 1 the first time the object created. It is incremented anytime the object retained or copied. It is decremented anytime it is released. When the retainCount equals zero, the object is destroyed.

Those differences happen because they use different approach in memory management. Java depends on its garbage collection algorithm to release unused memory, while Objective-C gives programmers more responsibility to release their objects. If you allocate memory, it is your responsibility to release it.

However, since XCode 4.2 Apple has put new feature called ARC (Automatic Reference Counting) to ease developers’ job dealing with allocating and releasing memory. It is sort of AOP implementation in Objective-C for me.

Using ARC, we don’t need to release memory we allocated before, in other words we don’t need to deal with retain-release code anymore. The compiler takes care the code of retaining-relasing objects. At compile time, the compiler crosscuts your code and adds some line of retain-release code. Using ARC we now only have to deal with our logic.

Java Environment



The picture above tells us a big picture of Java. Some codes written in Ada, Cobol, and C can be compiled into java byte code. The .class java byte code then translated by JVM into native machine language.

So, what is JVM? Lets see java installation directory:
1. JDK = java programs (java.exe, javac.exe, jar.exe, etc) + JRE
2. JRE = collection of Java API (lang, util, text, io, net, math, nio)
3. JVM = instance of JRE

Another term is JIT which is a part of JVM that compiles java byte codes that have similar functionality at the same compiling time so it will reduce compile time.

Java Heap Space

Heap is a part of memory used by JVM to allocate objects. There are three types of Java heap space:
1. Young Generation: Space to save short-live objects, they are method-scope objects or loop-scope objects.
2. Old/Tenured Generation: Space to save long-live objects, they are class-scope and application-scope objects.
3. Permanent Generation (permgen): Space to save class definition and its metadata.

There are some arguments that can be used to configure heap space:
1. -Xmx256M : Maximum heap space value is 256 MB.
2. -Xms128M: Initial heap space value is 128 MB. It will grow until xmx value is reached.
3. -Xmn64M: Heap space allocated for young generation.

There is nothing mentioned about default java heap space. It varies between machine, but wikipedia says it is 128M.

Saturday, July 17, 2010

Java Block (Static, Non static, Constructor)

Hanya untuk pengingat saja, daripada terus googling

public class TestBlock {

{
System.out.println("This is object block...");
}

public TestBlock() {
System.out.println("This is constructor...");
}

static {
System.out.println("This is static block...");
}

public static void main(String args[]) {
TestBlock obj1 = new TestBlock();
TestBlock obj2 = new TestBlock();
}

}

Hasil :

This is static block...
This is object block...
This is constructor...
This is object block...
This is constructor...

Dari hasilnya, ketahuan urutan eksekusi dan jumlah eksekusi.
 

©2009 Stay the Same | by TNB