Sunday, June 19, 2011

It's abt Brain F**K


It was a Boring Saturday again :P
So jus started exploring Brain F**K an awesome weird programming language 
It has got just 8 alphabets + - < > , . [ ] that's it :) 
This is like Maagi of programming languages :D

. - Prints the current byte
+ - Increments the byte pointed to
- - Decrements the byte pointed to
< - Moves to previous byte
> - Moves to the next byte
[ - Executes the instructions until ]
] - Executes the instructions from [ until the current byte carries 0
, - getchar()
U wanna know the C representation of Brain F**K char array[30000]; char *pointer; That's it :) It has a global array of size 30000 with a pointer pointing to it :) So I'm curious and tried this one
+++++ +++++ [>+>+++++++ >+++++++>++ ++++++<<<<-]>>>+++.<<. >>+++.+++.>++++++.<< -.<.>>>+++.<.>---- .<<<.>>>>+[[-] ,.------- --- ]
So now UR curious? Jus Click the link to Execute this 1. Copy paste the code 2. Type some Name r the One u like in the input Eg. xxx'\n' ('\n' - For clarity it is actually Enter in ur keyboard.) Don't forget to hit an enter at the end of the name else the code ll stuck in infinite loop 3. And Execute it :) 4. Don't laugh you are a culprit :P :P Who is that guy r girl? :D I'm done :) U also wanna explore? U have one link ahead Brain F**K

Friday, June 17, 2011

Crisps abt Design Patterns

Problems are hard solve more than that, our solution should not make our appln disobey OCP (Open to extensions and Closed to Modifications) but what if they have a quality solution pre-existing :)
That's what design patterns are :P They are generic reusable solutions for commonly occuring problems in S/W Design
Design patterns are of various categories if u wanna know them in detail follow the link below 
 Design Patterns  :)
I was just going thro some of the most commonly used Design Patterns in PHP and I wish to give them all a small introduction

Singleton:  
A common scenario for this is a LOG BOOK.

We might be recording various events at various times. So, whenever we need to record 
something we ll be declaring an object for the log book class and will be WRITING using some member functions of it. 
But what if we want a "GLOBAL LOG BOOK" for entire appln? Then choose singleton patterns. 
This pattern allows instantiation of object for a class only once. 
For Further instantiations, reference of the previously created object ll be returned. 
(Now Logbook is updated not created and written everytime)
One more appln is single player(only object of the whole app) gaming 

Google can find U PHP implementation of Singleton easily but i'll plot one for u here
class LogBook
{
  private static $LogBookObject;
  private function __construct(){...}
  public static function createInstance()
  {
     if(self::$LogBookObject==null)
     {
        self::$LogBookObject=new LogBook();
     }
     return self::$LogBookObject;
  }

  public function updateLog($msg){...}
}
$LogBookObject = LogBook::createInstance();
$LogBookObject->updateLog("This Works");
Factory: Huh!!! Its like ordering food to a waiter in a hotel :P Here waiter is an abstract object but the concrete object is the chef :) U might order Veg/Non-Veg the waiter decides to whom the order should be placed. The main advantage of it is the customer never knows even the hotel changes the chef :D Let us design a hotel appln in the sameway :)
class VegChef
{
  public function __construct() {}
  public function makeVegFood($foodName){}
}
class NonvegChef
{
  public function __construct() {}
  public function makeNonvegFood($foodName) {}
}
class Waiter
{
  private function __construct(){}
  public static placeOrder($foodType)
  {
    if($foodType=="Nonveg")
        return new NonVegChef();
    else if($foodType=="Veg")
       return new VegChef();
  }
}

$orderFood=Waiter::placeOrder("Veg");

$orderFood->makeVegFood("Gobi");
Strategy: This is like a music player that chooses songs based on our mood :P Here our behaviour like happy, romantic, sad are implemented as different classes hence any changes in happy never mess up with sad :D User ll also be able to change his mind with no intervention Lets go for implementation now
class ImHappy
{
  public function playSongs(){Algo to choose songs}
}
class ImSad
{
  public function playSongs(){Algo to choose songs}
}
class IfeelRomantic
{
  public function playSongs(){Algo to choose songs}
}
class musicPlayer
{
  private $Choice;
  public function setMyChoice($Choice)
  {
    $this->Choice=$Choice;
  } 
  public function Play()
  { 
   $this->Choice.playSongs()
  }
}

$user=new musicPlayer();
$user->setMyChoice(new ImHappy());
$user->Play();
// Ha I feel Romantic :P
$user->setMyChoice(new IfeelRomantic());
$user->Play();
There are two more Observer and Chain of command out of five I read :) Will soon comeup with nice illustrations for them :)

Thursday, June 16, 2011

all* abt PHP OO :P *conditions apply :D

Hi Folks spent a day with PHP in action Book
An awesome book for developers :)
Wanna share some useful tips out of it
0. When ISA (Inheritance)? When HASA (Composition)?
Only way to decide it is relationship if it is going to be 1:1 go for ISA 1:Many go for HASA :) Cool but true :)

1. Predict what would happen
$object1 = new base();
$object2 = $object1;
in PHP 4 a photocopy of present appearance of the object1 is copied to object2, any changes in object1 later ll not affect the latter :D
in PHP 5 it means shallow copy :) jus the reference is returned to object2 so changes on former or latter ll reflect on latter or former too :D
wanna do the PHP 5 way in PHP 4 then do
$object2 = &$object1; Explicit way :)
But this has the difference with the PHP5 way, in this jus the symbol table is adjusted (Clever no) :)

2. Can abstract functions be private?
Don't scold me if u know the meaning of abstract class :D
The answer is NOOOOOOOOO obviously :) Because abstract methods are need to be implemented by the class inherits it :) so it can't be private
So, what abt protected? It can be :D try it


3. FACT: A class with abstract method should be declared abstract :)
4. What would happen if the class inheriting an abstract class doesn't implement all the abstract methods?
Nothing the compiler will knock ur screen saying that declare the class that derives abstract :) else implement the rest :)


5. FACT: Interface is an abstract class with all methods declared abstract :)
6. Then why do we have both?
A class can inherit only one (abstract) class but can implement many interfaces :) This is how they implement multiple inheritance in java :)
Interfaces are pure abstract classes :P that is, all methods declared are abstract and public by default. So, no method defn can be there. But in abstract class there can be non-abstract methods(implemented with in the class itself) and private members are allowed too :)


7. FACT: Class is a virtual home for an object. The home will exist even if there is no habitants :D So U can loot static members and methods
8. FACT: Diff between die() and exception thrown with no catch is, stack trace is printed in the latter
9. FACT: Class constants won't accept complex expressions :) even string concatenation
10. FACT: PHP is a (statically-incomplete) dynamically typed language :D PHP 5 alows typehints :) function add(integer $a,integer $b) is valid, in the book they have specified that compiler ll generate a warning but im getting a fatal error :D
11. FACT: Method overloading is done through __CALL and by the way this is not same as function overloading :) this is a life saver at the verge of death of execution because of undefined methods :)
12. FACT: In the same way we have __autoload for classes :)

Read some design prinicples like
OCP (Open to extension and Closed to modification)
SRP (Single Responsiblity Policy) - Don't be good at many things :D
DIP (Dependency Inversion Principle) - Program for interface not for implementation :) Believe in abstraction :D

Monday, June 13, 2011

Google Facebook Fun

Jus a funny fact to start with

Jus Search for "Google" @ http://www.google/com and "Facebook" in the same :)

Search results found for Google 10,490,000,000

But for facebook 13,410,000,000

:D