Notice

YouTube.com/BESTTravelers - Visit and SUBSCRIBE to our travel related YouTube Channel named "BEST Travelers"

Sunday, July 31, 2011

Dependency Injection Pattern using PHP

Behavioral Pattern – It is a technique that indicates to a part of a program which other parts it can use, i.e. to supply an external dependency, or reference, to a software component.

In technical terms, it is a design pattern that separates behavior from dependency resolution, thus decoupling highly dependent components.

// User.php
<?php
include_once('Database.php');
/**
 * Establishes a database connection and utilizes a singleton pattern
 * implementation to ensure only one instance exists during execution
 */
class User
{
    public function dbTask($db)
    {       
        $query = "SELECT name, age FROM student ";                       
        $result = $db->getQueryResult($query );
       
        while($thisrow = mysql_fetch_row($result))
        {
              $i=0;
              while ($i < mysql_num_fields($result))
              {
                $field_name = mysql_fetch_field($result, $i);
                echo $thisrow[$i] . " ";  //Display all the fields on one line
                $i++;
              }
            echo "<br>";  //put a break after each database entry
        }
    }
}
?>

// Calling.php
<?php
include_once('noUser.php');
// User and db is loosely coupled and dependency injected here between them
echo "Dependency Injection is applied"."<br><br>";
$user = new User();
$db = Database::getInstance();
$user->dbTask($db);
?>
Source: From lecture notes of Design and Development Open Multi-tier Application 

Registry Design Pattern example using PHP

It's a bhavioral Pattern – It’s generally considered “good form” to avoid the use of global variables, objects are usually passed from one code segment to another as parameters.

The problem with passing instances globally is that objects sometimes end up as “tramp data,” passed into one function only to be passed again to another function which truly needs the object. To make writing, reading, and consuming code simpler, it’s best to minimize the number of different objects and consolidate knowledge of how to get to a numerous of other widely-used objects into a single, well-known object.

How can you get references to objects through a single, well-known, object? The Registry design pattern is like an “object phone book”—a directory—that stores and retrieves references to objects.

The Registry pattern can be useful, for example, if, for the bulk of your application, you use the same database connection, but need to connect to an alternate database to perform a small set of tasks every now and then. If your DB class is implemented as a Singleton, this is impossible (unless you implement two separate classes, that is)—but a Registry makes it very easy:

<?php

class Profile {    
  protected $_address1;
  protected $_address2;
  protected $_city;
  protected $_state;
  protected $_zip;
  protected $_phone;
  protected $_fax;
   
  public function __construct() {
       
  }
   
  public function address1($address = NULL) {
    if(isset($address)) {
      if(!is_string($address)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_address1 = $address;
    } else {
      return $this->_address1;
    }       
  }
   
  public function address2($address = NULL) {
    if(isset($address)) {
      if(!is_string($address)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_address2 = $address;
    } else {
      return $this->_address2;
    }
  }
   
  public function city($city = NULL) {
    if(isset($city)) {
      if(!is_string($city)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_city = $city;
    } else {
      return $this->_city;
    }
  }
   
  public function state($state = NULL) {
    if(isset($state)) {
      if(!is_string($state)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_state = $state;
    } else {
      return $this->_state;
    }
  }
   
  public function zip($zip = NULL) {
    if(isset($zip)) {
      if(!is_string($zip)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_zip = $zip;
    } else {
      return $this->_zip;
    }
  }
   
  public function phone($phone = NULL) {
    if(isset($phone)) {
      if(!is_string($phone)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_phone = $phone;
    } else {
      return $this->_phone;
    }
  }    
  public function fax($fax = NULL) {
    if(isset($fax)) {
      if(!is_string($fax)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_fax = $fax;
    } else {
      return $this->_fax;
    }
  }
   
}
?> 
Source: From lecture notes of Design and Development Open Multi-tier Application 

About me - Observation of a person who has 30 years of experience

I have worked 2 years at United Group International (UGI Ltd.) as a Project Manager and Manager-In-Charge later. There I have got chance to work with a man who has more then 30 years of experience.He was General Manager at Bangladesh Oil Gas & Mineral Corporation(Petrobangla). And he has extra ordinary managing skill. I was lucky that I have learned management from such a person.

I have received following email on Mar 18,2010 after 9 months of leaving UGI in return of one of my email. I was very happy to receive such a comment from the people like him.
"Dear Asfak,
I am really glad to read this news of your good placement. I always adore you for your good heart. Also I am sure you will be more controlled now than before with your earned experiences.

I really love you as you are a good man which is more precious than a good engineer and indeed a rare quality in our society. 

With best wishes,

Syed Shamsul Alam
Consultant, Arneeb Enterprise
Executive Director, United Group International
Ex. General Manager, Bangladesh Oil Gas & Mineral Corporation(Petrobangla)"

Thursday, July 28, 2011

One of my colleague's point of view about me

Here are some quote's about me from one of my colleague.
  • You are smarter than the people who hired you at least I know that. I am a true witness of that.
  • Whatever it is - You are the best in the organization.
  • You are best. Because you have this attitude to be the perfectionist.
  • You are very choosy in terms of people you hang around with.
  • You always like unusual things.
  • You like the word 'typical'.
According to my colleague, here is my 10 characteristics, 
  1. Active - You are very active in office, but I bet you are damn lazy at home. (Which is right)
  2. Mystery - You like mysterious type of thing.You like to uncover the mystery. (Which is right)
  3. Winter - You like winter season and love to sleep in the winter morning. (Which is wrong. Because I have not special affection for it)
  4. Intelligence - You like intelligent people rather than beautiful people. (Which is right)
  5. Traveling -You love traveling lot. (Which is right)
  6. [Skipped this point by mistake]
  7. Sunlight & Sky - You like strong sunlight and clear blue sky. (Which is wrong. I like blue sky. But not too much.)
  8. White - You like white color. (Which is wrong)
  9. [Skipped this point by mistake]
  10. Girls - You like smart, outspoken and intelligent girls. (Which is wrong. I like good human being who has innocent heart)

Tuesday, July 26, 2011

5 characteristics of a successful object-oriented software project

5 characteristics of a successful object-oriented software project are,
  1. A ruthless focus on the development of a system that provides a well understood collection of essential minimal characteristics.
  2. The existence of a culture that is centered on results, encourages communication, and yet is not afraid to fail.
  3. The effective use of object oriented modeling
  4. The existence of a strong architectural vision.
  5. The application of a well-managed iterative and incremental development life cycle.
Source: Software Project Management by Barry W. Boehm

Monday, July 25, 2011

Tour in Keo-Kra-Dong,Bandarban. Highest hill in Bangladesh

In 2005 some of my friends decided to visit Keo-Kra-Dong. They told me to join with them. I was very interested to visit Keo-Kra-Dong. I like this kind of adventure tour lot. I love to visit new places. So I agreed to visit Keo-Kra-Dong.
Picture: Going to Ruma Bazar, Bandarban, Bangladesh.
We decided to go  Keo-Kra-Dong on December 2005. Because all of my friends were busy with their study or jobs. So it was not possible to go their in the middle of the year. Thus we waited 6 months. Actually I was not so involve in all necessary planning and arrangement because of my busy time. My friends Jasim Bhai, Nipu and Rajib was closely involve in all kind of arrangement. We were 5 friends all together - Rajib, Topu, Emon, Jashim Vai, Nipu and me.

Picture: Going to Boga Lake, Bandarban,Bangladesh.
Keo-Kra-Dong is the most beautiful place that I had ever seen up to that time. I can’t explain how beautiful the place is. It is very beautiful place. The tour was full of adventure. It was too hard to walk in a hilly way. Actually none will understand it without walking in hilly path.

Picture: This is Boga Lake,Bandarban,Bangladesh.
At the first day of tracking, we started walking at 8am from Ruma Bazar and finished walking at 5pm.All day we walked on the hilly path and walking in hilly way is 20 times harder compare walking in a normal road.There were life risks also. One of my friend was Emon. He shocked see the hilly way and became very nervous and wanted to come back to Dhaka. But we overcome the critical situation.After 5pm we reach a village near to Boga Lake. And we surprised to see the beauty of Boga Lake. The lake was very beautiful. Next day we visited Keo-Kra-Dong after half day tracking in hilly way. We was so excited that we felt like we have won the world cup. That was a wonderful experience for all of us. It’s true that I will never forget this journey in my life.

Picture: Going to Keo-Kara-Dong,Bandarban,Bangladesh.
Adventure:
  • Waking in hilly way was very hard and painful. But i have enjoyed at the end.
  • We have seen some villages during going to Boga Lake and Keo-Kra-Dong.
  • We drank water from road side places when we finished our water.
  • It was a great advantage during arriving to the top of Keo-Kra-Dong.
Picture: Reached to Keo-Kra-Dong,Bandarban,Bangladesh.
 Memorable Moments:
  • Emon was very nervous to see the hilly way. So he wanted to come back to Dhaka. Night before the first day of tracking, he was very nervous.
  • We thought that we will walk in the hilly way with our bags which was full of many shirt and pants. But later we have realized that we have to take only urgent items. Otherwise it will be very hard to walk in the hilly way.
  • Jasim’s bhai thread us by saying - “You all don’t know how to walk in Hilly path”.
  • Nipu wore bad shoes. Thus he has faced lots of problem during walking in hilly way.
  • Nipu and Rajib’s has taken drugs for first time in Boga lake.
  • Rajibs has become ill.
  • Jasim bhai and Rajib ate meat of pig which is not allow for Muslim and Hinduism .
  • I had very bad health condition during returning back from Keo-Kra-Dong.
  • Emon’s used too much bad word to our guide because of unending path at time of returning to Ruma Bazar.
Picture: Keo-Kra-Dong,Bandarban, Bangladesh

    Thursday, July 14, 2011

    Adaptor Design Pattern example using PHP

    Adapters are used to enable objects with different interfaces to communicate with each other.Convert the interface of a class into another interface clients expect. It is useful when we upgrade system

    <?php
    //SimpleBook.php copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
     
    class SimpleBook
    {
         private $author;
         private $title;
         function __construct($author_in, $title_in) {
            $this->author = $author_in;
            $this->title  = $title_in;
        }
        function getAuthor() {return $this->author;}
        function getTitle() {return $this->title;}
    }
    ?>

    <?php
    //BookAdapter.php copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
    include_once('SimpleBook.php');

    class BookAdapter
    {
        private $book;
        function __construct(SimpleBook $book_in) {
          $this->book = $book_in;
        }
        function getAuthorAndTitle() {
          return $this->book->getTitle() . ' by ' . $this->book->getAuthor();
        }
    }
    ?>

    <?php
    //testAdapter.php copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
    include_once('SimpleBook.php');
    include_once('BookAdapter.php');

    define('BR', '<'.'BR'.'>');
    echo 'BEGIN TESTING ADAPTER PATTERN'.BR;
    echo BR;

    $book = new SimpleBook("Gamma, Helm, Johnson, and Vlissides","Design Patterns");
    $bookAdapter = new BookAdapter($book);
    echo 'Author and Title: '.$bookAdapter->getAuthorAndTitle();
    echo BR.BR;
    echo 'END TESTING ADAPTER PATTERN'.BR;
    ?>

    Source: From lecture notes of Design and Development Open Multi-tier Application 

    Singleton Design Pattern example using Java

    Creational Pattern- Ensure a class only has one instance, and provide a global point of access to it.Sometimes we want just a single instance of a class to exist in the system. For example, we want just one window manager. And we want to ensure that additional instances of the class cannot be created.
    • Normally when you have a class and you create many instances of it then you get many objects.
    • But in the case of singleton design pattern we get one instantiated object for many calls of getInstance() method instead of new.
    • Example of Singleton Design pattern is implemented in the following database class. Note that it private constructor. Only getInstance() method is used to create single instance of the class.
    public class Database
    {
        private static Database singleObject;
        private int record;
        private String name;

        private Database(String n) // Note private
        {
            name = n;
            record = 0;
        }
        public static Database getInstance(String n) //instantiation
        {
            if (singleObject == null){
                singleObject = new Database(n);
            }
            return singleObject;
        }
        public void editRecord(String operation)
        {
            System.out.println("Performing a " + operation + " operation on record " + record + " in database " + name);
        }
        public String getName()
        {
            return name;
        }
    }
    public class TestSingleton
    {
        public static void main(String args[])
        {
            Database database;
            database = Database.getInstance("products");
            System.out.println("This is the " + database.getName() + " databse.");
            database = Database.getInstance("employees");
            System.out.println("This is the " + database.getName() + " databse.");
        }
    }

    Source:  From lecture notes of Design and Development Open Multi-tier Application 

    Tuesday, July 12, 2011

    Observer Design Pattern example using PHP

    The observer pattern is a software design pattern in which an object (called the subject object) maintains a list of its dependents (called observers) and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.
    • The Observer pattern defines an one-to-many dependency between a subject object and any number of observer objects so that when the subject object changes state, all its observer objects are notified and updated automatically.
    • The Observer pattern essentially allows an unlimited number of objects to observe or listen to events in the observed object (or subject) by registering themselves. After observers are registered to an event, the subject will notify them when the event is fired.
    • The subject handles this by storing an observer collection and iterating through it when the event occurs in order to notify each observer.
    • Observer Pattern registers observers with a subject.
    • You might have multiple observers. Subject must keep a list of registered observers and when event occurs it fires (provides notification) all registered observers.
    • Unregister also possible when we do not need any observer.

    Example using Observer Design Pattern:
    <?php
    interface IObserver {
        function onChanged($sender, $args);
    }

    interface IObservable {
        function addObserver($observer);
    }

    class UserList implements IObservable {
        private $_observers = array();

        public function addCustomer($name) {
            foreach( $this->_observers as $obs )          
                $obs->onChanged( $this, $name );
            }
       
        public function addObserver( $observer ) {
            $this->_observers []= $observer;
        }
    }

    class UserListLogger implements IObserver {       
       
        public function onChanged( $sender, $args ) {
            echo( "'$args' notifies  UserListLogger <br>" );           
        }
    }

    class AuditLogger implements IObserver {
       
        public function onChanged( $sender, $args ) {
            echo( "'$args' notifies  AuditLogger <br>" );       
        }
    }

    $ul = new UserList();
    $ul->addObserver(new UserListLogger() );
    $ul->addObserver(new AuditLogger() );

    $ul->addCustomer("Jack" );
    $ul->addCustomer("John" );
    ?>

    Source:  From lecture notes of Design and Development Open Multi-tier Application

    Friday, July 8, 2011

    Software economics and cost models

    Most software cost models can be abstracted into a function of 5 parameters. They are,
    1. Size - It's indicate number of source instructions or the number of function points required to develop the functionality.
    2. Process - It used to produce the end product, in particular the ability of the process to avoid rework, bureaucratic delays and communications overhead.
    3. Personnel - Indicate the capabilities of software engineering personnel.
    4. Environment - Indicate the tools and techniques available to develop the software.
    5. Quality - It's indicate quality of the software including its features, performance, reliability, and adaptability.
    Effort = (Personnel)(Environment)(Quality)(Size^Process)
    Source: Software Project Management by Barry W. Boehm

    Characteristics of the conventional software development process

    It's useful to summarize the characteristics of the conventional software development process as it has typically been applied, which is not necessarily as it was intended.Our projects destined for trouble frequently exhibit the following symptoms,
    • Protracted integration and late breakage
    • Late risk resolution
    • Requirements driven functional decomposition
    • Adversarial stakeholder relationships
    • Focus on documents and reviews meetings.

    Source: Software Project Management by Barry W. Boehm

      Thursday, July 7, 2011

      Software Development: 5 necessary improvements for Waterfall Model to work

      Waterfall Model is not suitable for software development now a days. But if we do following improvements, then we will be able to develop successful software using this approach,
      1. Complete program design before analysis and coding begin.
      2. Maintain current and complete documentation.
      3. Do the job twice if possible.
      4. Plan, control and monitor testing.
      5. Involve the customer.
      Source: Software Project Management by Barry W. Boehm, TRW Inc.
      Figure: Waterfall Model

      Wednesday, July 6, 2011

      Skills of a Software Project Manager

      Software project managers need many leadership qualities in order to enhance team effectiveness.The following are some crucial skills of successful project managers,
      1. Hiring Skill
      2. Customer Interface Skill
      3. Decision Making Skill
      4. Team Building Skill
      5. Selling Skill
      The other required skills are,
      • Human Management Skill (It's a part of Team Building Skill)
      • Communication Skill (It's a part of Team Building Skill)
      • Technical Skill
      • Problem Solving Skill 
      Source: Software Project Management by Barry W. Boehm.

      Software Development: 80% of the contribution comes from 20% of the contributors

      • 80% of the engineering is consumed by 20% of the requirements
      • 80% of the software cost is consumed by 20% of the components.
      • 80% of the errors are caused by 20% of the modules.
      • 80% of the software scrap and network is caused by 20% of the errors.
      • 80% of the resources are consumed by 20% of the components.
      • 80% of the engineering is accomplished by 20% of the tools.
      • 80% of the progress is made by 20% of the people.
      Source: INDUSTRIAL SOFTWARE METRICS: A TOP-TEN LIST by Barry W. Boehm, TRW Inc.

      Software Project Management: Top 10 list of Industrial Software Metric

      To manage software project, we should know Top-Ten list of software metric relationships, in terms of their value in industrial situations. Here they are, in rough priority order:
      1. Finding and fixing a software problem after delivery is 100 times more expensive than finding and fixing it during the requirements and early design phases.
      2. You can compress a software development schedule up to 25% of nominal, but no more.
      3. For every dollar you spend on software development you will spend two dollars on software maintenance.
      4. Software development and maintenance costs are primarily a function of the number of source instructions in the product.
      5. Variations between people account for the biggest differences in software productivity.
      6. The overall ratio of computer software to hardware costs has gone from 15:85 in 1955 to 85:15 in 1985, and it is still growing.
      7. Only about 15% of software product development effort is devoted to programming.
      8. Software systems and software products each typically cost 3 times as much per instruction to fully develop as does an individual software program. Software system products cost 9 times as much.
      9. Walkthroughs catch 60% of the errors.
      10. Many software phenomena follow a Pareto distribution: 80% of the contribution comes from 20% of the contributors.
      Source: INDUSTRIAL SOFTWARE METRICS: A TOP-TEN LIST by Barry W. Boehm, TRW Inc.