Code: Review of Java Fundamentals: Object-oriented Design from PluralSight.com

Review of Java Fundamentals: Object-oriented Design

When I picked up the Java programming language a few years back, I focused on understanding the syntax, API and tooling of the language. Yet one area that I quickly glazed over was object oriented design patterns. Over the years, I’ve been itching to research this topic since I’ve heard that good architecture and the right design patterns could increase the maintainability of my source code.
For that reason, I decided last week that I would level up my design skills and take a course over Java object oriented design patterns on pluralsight.com.

The pluralsight course covered the popular SOLID principles mentioned in most Java OO design textbooks but with the authors spin on things.

After taking the course over a 3 day time span, I feel like it was time well spent! I had a lot of misconceptions about object oriented programming that were debunked in the first hour of the modules. Here are a few things I absorbed from the course.

1. The object with the most data should do the heavy lifting

2. Ask for help from objects, not their data

We can illustrate the first two points with a simple use case.
In this example we want to find the number of books with less than 100 pages in a shelf.

Ok Code

// ES6
const MAX_PAGES_FOR_SHORT_BOOK = 100
const numOfShortBooks = shelf.getBooks().filter(book => book.getTotalPages() < MAX_PAGES_FOR_SHORT_BOOK).length


Figure: Code 1.1

The problem here is that the shelf object is exposing too much information about itself, which could make refactoring hard.
In the course, the author states that objects should communicate and accomplish tasks using public methods, while keeping their inner data structures private and locked down. Avoid getter and setters of internal data like the plague. Not doing so will produce more run-time exceptions because of the unexpected way a external class is using your code.
Check this out.

Improved Code

// ES6
const MAX_PAGES_FOR_SHORT_BOOK = 100
const numOfShortBooks = shelf.countBooksByTotalPages(MAX_PAGES_FOR_SHORT_BOOK)


Figure: Code 1.2

As you can see, the method calls were wrapped inside .countBooksByTotalPages() to hide the complexity. Less code, less bugs.

3. Prefer interfaces over concrete classes.

In OO design, your objects should care about getting the job done regardless of the class. That's why you should prefer interacting with interfaces rather than classes. Interfaces will give you a greater number of objects that can fulfill a role because it's based on abilities. However, class must have the correct super class in it's class hierarchy to meet a requirement.

// in groovy language
interface Talkative {
  void speak()
}
class Person {
  speak(){
    println("Where's my money?")
  }
}
class Dog {
  speak(){
    println("woof! woof!")
  }
}

def saySomething(Talkative thing){
  thing.speak()
}

// :-( Doesn't work with Dog
def personSaySomething(Person person){
  person.speak()
}

saySomething(new Person()) // prints where's my money!
saySomething(new Dog()) // prints woof! woof!

personSaySomething(new Person()) // prints where's my money!
personSaySomething(new Dog()) // Error! Dog isn't of class Person


Figure: Code 1.3

In the above code, you can see that the saySomething() method is more flexible then personSaySomething().

Review: 5/5 - Recommended
Pros:
+ Short and concise
+ Covers the popular SOLID principles
+ Straight forward and complete examples

Cons:
- It not about designing objects. For that you'll have to find a course over Object-oriented analysis and design (OOAD)

- More info:
Java Fundamentals: Object-oriented Design

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Subscription Review: New Scientist


Throughout 2011 I have been loyal subscription holder to the scientific magazine New Scientist.
I love it how each week I’m presented with the latest scientific discoveries that blow my mind.

So, what is New Scientist?

New Scientist reports on the very latest science and technology news, putting discoveries and advances in the context of everyday life. New Scientist relates the advancements of human knowledge to the broader impacts on society and culture, making it essential reading for people who ask why. – From: Amazon.com

Here are my top 5 hottest stories from 2011

  1. Fallible DNA evidence can mean prison or freedom

    New Scientist reveals that much of the DNA analysis now conducted in crime labs can suffer from worrying subjectivity and bias.

  2. Neutrino watch: Speed claim baffles CERN theoryfest

    OPERA … announced that neutrinos traveling from CERN had apparently moved faster than light.

  3. Move over, Einstein: Machines will take it from here

    Schmidt recorded this movement using a motion tracking camera which fed numbers into his computer. What he was looking for was an equation describing the motion of the pendulums.

    Note: I wrote an article over this here

  4. How online games are solving uncomputable problems
  5. Better than human? What’s next for Jeopardy! computer

Pros:

  • Well thought out and highly organized magazine.
  • Scientific Job Board
  • Amazing Scientific News
  • Articles are easy for ordinary person to understand.
  • Offers short lessons over complex topics
  • “The Last Word” – answers to scientific questions about everyday phenomena.

Cons:

  • Topics can sometimes become repetitive
  • No references for most articles.
  • It’s only news, so you can’t apply want you learn.

Price: ~$99 annually (on line discounts may vary)
You can check out “New Scientist” at www.newscientist.com/

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Download Videos From Your Browser’s Cache

I watch tons of videos on-line in any given week from sites like youtube.com, dailymotion, and newgrounds.
From time to time I stumble across videos that I would love to keep in my video collections. The way that I save the videos is by saving the browser cache folder and searching for the video within there.
To simplify this process I use VideoCacheView from Nirsoft.net.
VideoCacheView is freeware and easy to use. All you do is download and extract the zip file, then launch the program.

Example
Let’s say that I want to download “TFS Abridged Parody Episode 24” from youtube.com.

TFS Abridged Parody Episode 24 Video
TFS Abridged Parody Episode 24 Video
. All I would have to do are two step to get the file.
Step 1: Open the link and have the video load to the completion.
Step 2: After that launch VideoCacheView.exe and right click on the temporary file to save the video.
That’s it.

Video Cache View Right click to save
Video Cache View Right click to save

Once last thing. Most of the videos are in the flv file format. To solve this problem, you can use “iWisoft Free Video Converter” to convert from one file format to another, like wmv or avi.

Video Cache View: Download here

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube