bytebitebight

Just byte me

WTF is an Interface?

A lot of people are confused  on WTF an interface or what is its usability.

An interface is basically a reference type that has abstract (or only has names but no implementation) members that must be implemented by the inheriting class. In simpler words you just make and interface and all its members (methods) must have names, accessor, return/parameters but you don’t write any code for it AND the first concrete class that implements it must write the method’s content for it if it is not implemented already.

WTF is it good for? Ok imagine you and a team is working on a very large project so each member makes his/her own component. Even if they are able to implement and follow a naming convention chances are there will still be some problems in creating a generic ode that can handle or the coding. Here is an example

public class noInterface {

              /* rest of code omitted for clarity */

             public  printThis(Object toBePrinted) {

                        toBePrinted.Print();

            }

}

Problem solved? Not really since you cannot call the Print method from an object. You can go about this another way such overload the printThis method but if you have to create all the methods for each type you pass this beats the reuse concept of OOP. Another possibility is to use reflection on the object but this is not the way since this is against the KISS approach.  A smart ass answer would be “why not find a language that allows reflection etc etc etc” well I must admit there are languages out there that complies with this but still there is no way of knowing whether the object passed has the print method without checking for it explicitly

WTF are we supposed to do? use an interface. Here is what I mean

create an interface e.g

 

public interface printInterface {

 

    void print();

}

create classes that implement that interface e.g.

public class Test1 implements printInterface{

    public void print() {

        System.out.println(“test1”);

    }

}

//

public class Test2 implements printInterface {

 public void print() {

        System.out.println(“test2”);

    }

}

and an example run is

public class Main { 

public static void main(String[] args) {

        Test1 xxx = new Test1();

        Test2 xxxx = new Test2();

       /* an alternative will be 

       printInterface  xxx = new Test1() ; // this is allowed since Test1 implemented printInterface

       printInterface  xxxx = new Test2() ; // this is allowed since Test2 implemented printInterface

       end of alternative

       */

 

        Main yyy = new Main();

        yyy.doPrintInterface(xxx);

        yyy.doPrintInterface(xxxx);

    }

    public void doPrintInterface(printInterface z){

        z.print();

    }

}

Now if the object passed to the doPrinterInterface method  did not implement the printinterface interface then a compile time error will be detected, you can pass any object to the method and be assured that it will have the print method.

Why not use an abstract call instead? There are many reasons but one of them is that several languages don’t allow multiple inheritance from classes but allows it from interfaces. Yes you can implement multiple interfaces in Java and C#.

Note: I used java

for a C# example kindly see http://www.codeguru.com/csharp/csharp/cs_syntax/interfaces/article.php/c7563

 

 

Advertisements

September 6, 2008 Posted by | DumbAss's Guide, Java Bits | , , | Leave a comment

WTF is a software Framework?

Let me start this by saying that I am no expert in the subject and have no pretension as such. I am just a regular Jose (Joe) writing my understanding of the subject from a dumb down point of view.

What is a Framework

A framework is basically a frame that a developer will add functionality to solve a certain problem. A framework contains the little bits that need not be handled by “plumbing code” such as page navigation, database insertion and the like depending on the framework being used.

Unlike a library where the functions are called by the application, in the case of a framework it is the application that is called by the framework. A good example of a framework is Ruby on Rails aka RoR aka Rails. Rails will handle most database transactions and view transactions leaving the developer to concentrate on the business processes.

IoC

To further distinguish a library, which usually requires the usage or inclusion or importation of certain package or header, from a framework there is this term the is called Inversion of Control or IoC which means that the control structure is reversed. To simply put it in a library the developer writes code to control the functionality of the library in a framework the developer writes code that will be controlled and used by the framework.

Frameworks Frameworks Frameworks

There are so many frameworks out there both “free” and non-free that can be utilized to solve a particular problem. There are frameworks that tackle only a a certain area such as display or database interaction while others are complete packages. It would be up to the developer to select which tool would be most fitting.

Advantage of a Framework

The advantage of a framework is that generally the developer is insulated from the tedious coding of some plumbing code such as page navigation, session handling or data handling and manipulation. The developer can now concentrate on business rules and the like.

Disadvantage of a framework

Two words “learning curve”

August 26, 2008 Posted by | DumbAss's Guide | , | Leave a comment