Sunday, 17 February 2013

Next generation Computer programming Techniques - Mocking your Objects


One topic which i have mentioned before was unit tests.  Implementing unit tests is something I did almost 4 years in to my professional job as a developer.  This unique straightforward act made a remarkable change on the program code which i was creating.  Concrete calculations like “the amount of issues in the program code” had proven that the idea of unit tests had done the job of making far better program code.  Now to expound on the subject of unit tests, today I wish to talk about mocking.

Just what is Mocking?


The term mocking can be used to specify the act of lessening the coupling between your Objects (such as during unit testing).  This idea allows us to take every one of the collaborating Objects and change them (quite simply) in to shell-like constructs that will eliminate coupling.  To illustrate, let’s say we have a UserService object that allows us to save and delete users.  We should test our save and delete methods and ensure that they are always doing what we anticipate them to be doing... but in order to run a test on the UserService object, we have to instantiate and initialize a UserDao object (since this DAO object would be conducting the save/delete functions).

This causes a complication because at this point we are not “unit” testing, we are broadening the scope of our unit test to incorporate 2 objects.  This defeats the entire purpose of a unit test, so we ought to do something about this travesty!

Hello Mocking.


It is possible to ‘mock’ the DAO object which will allow your UserService object to connect to an empty shell of the DAO.  The UserService will be able to call all of the functions, and you can even get fancy and supply the actual return values from the called methods.  This enables you to effectively create the precise use-cases you intend to test inside the UserService object.

Therefore, all in all, you will have the ability to accurately test A single unit of your code and you will still be able to run all of your tests with incredible speed.  The mocking frameworks Personally i have tried up to now i would suggest include:

- Mockito
- Powermock

To learn how to use these kinds of frameworks and other lessons on computer programming, you can check out the library of Java Video Tutorials available online.  I myself am an experienced Java developer and have produced a large number of video tutorials that explain this and many more extremely useful topics in computer programming at javavideotutorials.net.

Sunday, 15 July 2012

The 3 main Principles of Object Oriented Programming - How to Program with Java


Object Oriented Programming (or OOP) may be classified by three main principles.  

1) Encapsulation
2) Inheritance
3) Polymorphism

All these seem like intimidating words but are really fairly simple concepts to comprehend.  If you wish to learn how to program with java, you will need to know these concepts.  So let's have a look at our first main principle of OOP, encapsulation.  Encapsulation just means that we wish to limit the access that other pieces of code have to our particular object.  So, for example, if you have a Person object, and this Person object has a first and last name as attributes.  In the event that another piece of code tries to change your Person object's first name to be say "Frank3", you could have a look at what the first name is trying to be set to, and strip out any digits so we're just left with "Frank".  Without having encapsulation, we wouldn't be able to stop "silly programmers" from changing the values of our variables to something that wouldn't make sense, or even worse, break the application.  Make sense?

The second principle of OOP, and a very important concept if you wish to learn how to program with Java, is Inheritance.  This principle refers to a super class (or parent class) and a sub-class (or child class) and the idea that a child class gains all the attributes of its parent.  You can think of it in terms of a real world situation, just like a real parent and child.  A child has a tendency to inherit certain characteristics from his or her parents, like say, eye colour or hair colour.  Let's think about another example in terms of programming, say we have super class "Vehicle" and sub-classes "Car" and "Motorcycle".  A "Vehicle" has wheels, and therefore through inheritence so would a "Car" and a "Motorcycle", but a "Car" has doors, and a "Motorcycle" does not.  So it wouldn't be accurate to say that a "Vehicle" has doors, as that statement would be incorrect.  So you can see how we could determine all the aspects that are common between a "Car" and a "Motorcycle" and define them inside of the "Vehicle" super class.

The 3rd principle of OOP is Polymorphism.  This principle sounds the most intimidating, but I can explain it in simple terms.  Polymorphism means that an object (i.e. Animal) can take on several forms while your program is running.  Let's imagine you have created an Animal class and defined the method "Speak".  You then asked three of your friends to make types of animals and have them implement the "Speak" method.  You won't know what type of animals your friends create, nor how their Animals will speak, until you actually hear those animals speak.  This is very much like how Java handles this problem.  It's known as dynamic method binding, which simply means, Java won't know how the specific Animal speaks until runtime.  So perhaps your friends have created a Dog, Cat and Snake.  These are three kinds of Animals, and they each speak differently.  When Java asks the Dog to speak, it says "woof".  Whenever Java asks the Cat to speak, it says "meow".  Whenever Java asks the snake to speak, it hisses.  There's the advantage of polymorphism, all we did was define an Animal interface with a Speak method, and we can create a whole bunch of types of animals that speak in their own distinctive way.

For more information about how you can program with Java, there is a wealth of straightforward java tutorials presented on the howtoprogramwithjava.com web site.

Tuesday, 10 July 2012

Java Tutorial - How to Program with Java

If you are considering becomming a developer, but you do not know where to begin the process, there's a cutting edge weblog in town!  Trevor Page is the publisher of this new website and he is a skilled programmer with a talent for teaching folks.  He has chosen to establish a site where he will write-up innovative subject matter on how to become a developer using the Java technology.  This Java technology is a great and popular programming language that has helped Trevor become a big success in his career.  In this website, Trevor will cover all the significant subject areas with regard to coding using Java as well as teach you how to use the tools important to commence your job as a developer.

Trevor's first installment on this innovative weblog is discovering the 5 basic concepts of any programming language.  These concepts are typically used throughout most of the programming languages today, and they are critical to comprehend when you start your voyage as a new programmer.  These five key points are as follows:

1) Variables
2) Control Structures
3) Data Structures
4) Syntax
5) Tools

In his site, he will describe exactly why these principles are extremely vitally important to have an understanding of when you embark on becomming a developer.  Trevor delivers numerous examples using real life code and attempts to connect these examples to everyday life.  He additionally informs you what the common errors are in coding that you ought to avoid, and will point you in the right direction.  So if you are an aspiring developer, visit www.howtoprogramwithjava.com and begin your mission to becoming a genuine computer programmer as soon as possible!


Another different part of this page is that Trevor will not just be speaking about matters that could interest a beginner, but he'll additionally post about the road blocks that he has experienced in his current programming.  These tips and hints are generally centered around helping any kind of designer that is employed today and will most likely save people hours in browsing through yahoo and google for a solution.  These thorough java tutorials will make certain that no stone is left un-turned and that the solutions can be very easily integrated in anyone's code base, seeing that there is nothing more annoying than some developer who is convinced that anyone will comprehend just what they mean by a one phrase answer.  These are things that Trevor has noticed all over the net, and wants to cease with this innovative web-site.

Finally, the original part of Trevor's efforts, is that he will be posting videos that should show at length ways to become a developer, right from setting up the mandatory tools to debugging your code line by line.  These videos will help make it feasible for one to walk in off the street and recognize what exactly the steps are to getting the job done.  Now, suppose you have questions?  Just ask your question at the end of any of the articles, and Trevor is going to answer them quickly and also to the very best of his knowledge, simply because above all, Trevor loves to share his education and takes delight in aiding others achieve success!  So hop on over to www.howtoprogramwithjava.com and say hi in any blog's comment today!

Java Tutorial - What is Object Oriented Programming in Java?

Java is termed an Object Oriented 'language'.  So, exactly what does Object Oriented necessarily mean?  It means that the foundations of every program constructed in Java may very well be looked upon in terms of Objects.  A wonderful example of this concept may be to have a look at a few sample business requirements for any product.  Let's say that we are tasked with developing a product which can manage an important public library system.  This software will have to account for each of the branches of the libraries, each of the materials that are contained in the branches, as well as all of the users that wish to borrow books in a library's branch.

The very first thing we could actually do is simply examin these specs and determine each of the words which are nouns.  For the record, a noun is known as a person, place or thing.  As a result, after we assess these requirements we discern these nouns:

1) Library
2) Book
3) Branch
4) Customer

All these phrases would symbolize Objects in Java.  This really is, in essence, Object Oriented programming (otherwise know as O-O programming).  What we could easily now go about doing, is just arrange these four Objects right onto a page of old fashioned paper, and begin to distinguish what type of components each one of these Objects possesss  What do I mean by the components?  Well, in O-O programming this happens to be recognized as pinpointing the "has a" relationships.  For instance, a Branch "has an" address, any Book "has a" title, a Customer "has a" full name.  We will map out all of the noteworthy components that every of these Objects contain, and build ourselves a very good beginning point for the design of a particular Java product.

Object Oriented programming enables coders to think in terms of real life "things" or Objects, and simply solve issues with all those Objects.  It is critical to remember that Java is actually not the sole O-O programming language in existence, as it was initially created almost 50 years ago and lots of modern programming languages take advantage of Object Oriented principles.  Some of these languages include C++, C#, Objective-C, Python, Ruby, and Visual Basic.

There are lots more principles that happen to be rudimentary in O-O programming languages which include inheritance, polymorphism as well as encapsulation.  So if you would like to try understanding much more Object Oriented programming as it pertains to the Java language, there are lots of great Java tutorial internet resources existing these days.  Software engineer Trevor Page serves up his knowledge relating to the Java programming language and how a novice developer can easily learn howtoprogramwithjava.com.