Occam’s Razor

December 16th, 2009

The simplicity is laying under this principle:

The explanation requiring the fewest assumptions is most likely to be correct.
 

William of Occam

Quotes

1 Comment

Fancy UITabBar Like Tweetie

December 9th, 2009

It’s been a while I haven’t written anything about iPhone :) Anyways, today I added a fancy UITabBar (like in #Tweetie 2) to one of my applications. I thought I would share the code since I saw lots of iPhone Dev folks are looking for this :)

Initially I started googling, then I ended up a StackOverflow entry says you have to Roll it :) Since my need was just placing an indicator on top of the UITabItem, I didn’t go for that way.

Here is how I did:

First, I created an UIImageView which has the indicator icon. Then I added this view to my UITabBar as subview.

UIImage *upArrow = [UIImage imageNamed:@"up-arrow.png"];
UIImageView *imageView=[[UIImageView alloc] initWithImage:upArrow];
imageView.tag = ICON_TAG;
[upArrow release];
[self.tabBarController.tabBar addSubview:imageView];

This wasn’t quite enough for my need since I needed indicator to be centered. So I did some math :)

int itemCount = [self.tabBarController.tabBar.items count];
CGFloat cellWidth = tabBarController.view.frame.size.width/itemCount;
CGFloat center = (cellWidth / 2) - (INDICATOR_WIDTH / 2);

Now I could easily use this center as my indicator center. However I noticed that this wasn’t enough either since I want it to move every time after I tap to the next UITabBarItem. So I did some refactoring and:

#define INDICATOR_WIDTH 10 //u can define st like this
-(void) addIndicatorTo:(int) index {
    int itemCount = [self.tabBarController.tabBar.items count];
    CGFloat cellWidth=tabBarController.view.frame.size.width/itemCount;
    CGFloat center = (index * cellWidth) + (cellWidth / 2) 
                            - (INDICATOR_WIDTH / 2);
    UIImage *upArrow = [UIImage imageNamed:@"up-arrow.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:upArrow];
    imageView.tag = ICON_TAG;
    [upArrow release];
    [self.tabBarController.tabBar addSubview:imageView];
}
// just listen the item index change
- (void)tabBarController:(UITabBarController *)tbController 
        didSelectViewController:(UIViewController *)viewController {
    [self addIndicatorTo:tbController.selectedIndex];
}

So far so good. Wait a second, this adds new images every time I tap to the next UITabBarItem :S Anyways, so I refactored my code a little more:

#define INDICATOR_WIDTH 10, u can define st like this
#define INDICATOR_WIDTH 9, u can define st like this
#define ICON_TAG 11, u can define st like this
-(UIImageView *) createNewIndicator {
    UIImage *upArrow = [UIImage imageNamed:@"up-arrow.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:upArrow];
    imageView.tag = ICON_TAG;
    [upArrow release];
    return imageView;
}
-(UIImageView *) findIndicator {
    // return existing one
    for (UIView *view in self.tabBarController.tabBar.subviews) {
        if (view.tag == ICON_TAG) {
            return (UIImageView *) view;
        }
    }
    // if not return a new one
    return [self createNewIndicator];
}
-(void) addIndicatorTo:(int) index {
    int itemCount = [self.tabBarController.tabBar.items count];
    CGFloat cellWidth = tabBarController.view.frame.size.width / itemCount;
    CGFloat center = (index * cellWidth) 
                + (cellWidth / 2) - (INDICATOR_WIDTH / 2);
    UIImageView *imageView = [self findIndicator];
    imageView.frame = CGRectMake(
                        indicatorPositionX, 
                        3 - INDICATOR_HEIGHT, 
                        INDICATOR_WIDTH, 
                        INDICATOR_HEIGHT
                      );
    [self.tabBarController.tabBar addSubview:imageView];
}

This is basically it. So I did some animation too, here is the result:

If you guys need, I can upload the working source code to somewhere.. Hope this guides a bit.

iPhone

14 Comments

Single, Dynamic, Multiple and Double Dispatching

December 7th, 2009

Today I was reading couple articles about programming languages and I noticed that there is still some confusion about dispatching. So I’ll try to explain as much as I can. Everbody is welcome to correct!

What is Dispatching?

This is also just another OOP term. It’s not valid outside of the OOP context. I’m not gonna dive into linguistic details. Basically, it’s an invocation of a function on a specific type as in object.function(with params). For more details you can #wiki it. There are 3 types of dispatching mechanism:

  • Single/Dynamic Dispatching
  • Multiple Dispatching
  • Double Dispatching

You might also find 2 other topics related to this subject, but I’m not gonna cover them today. You can quickly #wiki it: Monkey Patching and Duck Typing. Anyways, so let’s get back to our subject:

Single/Dynamic Dispatching

Although there is a subtle difference between them, I covered these 2 topics under the same title. So basically, when you invoke a function on a type, it’s called single dispatch as in:

    class Messenger {
        public void send(String message) { }
    }
Messenger messenger = new Messenger(); messenger.send("Hello World");

If there is no polymorphic structure, compiler knows this at compile time. There can be multiple send functions under different types (classes). Compiler binds them to the correct ones in the compile time:

    class Messenger {
        public void send(String message) { }
    }
class SMSGateway { public void send(String sms) { } }
Messenger messenger = new Messenger(); messenger.send("Hello World");
SMSGateway gateway = new SMSGateway(); gateway.send("Cya in 10!");

In above example, compiler bound send(message) to Messenger and send(sms) to SMSGateway types. These are all single dispatch examples. No dynamic dispatching needed so far. However, if you have a polymorphic structure as in:

    interface Messenger {
        void send(String message);
    }
class TextMessenger implements Messenger { public void send(String message) { System.out.println("TEXT: " + message); } }
class XMLMessenger implements Messenger { public void send(String message) { System.out.println("" + message + ""); } }
class Demo { public void useMessenger(Messenger messenger) { messenger.send("Which messenger am I using?"); } }

As you may quickly notice, this requires dynamic dispatching (run-time decision) since compiler cannot decide the actual type of the messenger which Demo.useMessenger uses. Lots of known design patterns are already based on this mechanism (Strategy, Bridge, etc..) Again I’m not going go into details, you can find more information about this on #wiki: Single/Dynamic Dispatch and #c2wiki: Single Dispatch / Dynamic Dispatch

Multiple Dispatching

Roughly it is the idea behind overloading in most programming languages. Your type has so many functions with the same name, but they all have different parameters.

    class Messenger {
        public void send(String message) { }
        public void send(String message, Person to) { }
        public void send(String message, String subject, Person to) { }
    }

You can figure out how it would work. However multiple dispatching is not just about this. I guess overloading example is misleading too. Anyways, let’s consider following example:

    interface Messenger {
        void send(String message);
    }
    class TextMessenger implements Messenger {
        public void send(String message) {
            System.out.println("TEXT: " + message);
        }
    }
class XMLMessenger implements Messenger { public void send(String message) { System.out.println("" + message + ""); } } class Demo { public void useMessenger(Messenger messenger) { messenger.send("Which messenger am I using?"); } public void useMessenger(TextMessenger messenger) { messenger.send("I'm TextMessenger for sure, but?"); } }

So what do you think will happen when I try to invoke Demo.useMessenger(TextMessenger). If your language doesn’t support true multiple dispatch mechanism, you’ll always end up invoking useMessenger(Messenger) function no matter what you provide. With true multiple dispatching, you’ll invoke useMessenger(TextMessenger) when you provide TextMessenger to the function. More information, again use #wiki Multiple Dispatch or #c2wiki: Multiple Dispatch BTW, #Java doesn’t support true multiple dispatching, so don’t cause any bugs while creating overloaded methods.

Double Dispatching

Well this one is pretty cool. Some define this as true dispatching :) Anyways, to me all of them are true :) In real world, all relations are actually two-way. It’s not only you’re telling somebody about something, he/she is also listening to you. Double dispatching is like this: two-way. Visitor pattern is actually good way of explaining this topic. Let’s look at the following example:

    // messenger
    interface Messenger {
        void send(Message message);
    }
    class TextMessenger implements Messenger {
        public void send(Message message) {
            // some custom operations
            message.print(this);
        }
    }
    class XMLMessenger implements Messenger {
        public void send(Message message) {
            // some other custom operations
            message.print(this);
        }
    }
    // and message
    class Message {
        public void print(TextMessenger messenger) {
            // do something
        }
        public void print(XMLMessenger messenger) {
            // do some other thing
        }
    }

As you can easily see, objects are related to each-other both ways. Message is deciding to do something based on other types.

Hope this clarifies things a bit more.

Java

5 Comments