Video: Tutorial over Object Oriented Design



Object Oriented Design by Derek Banas

> I had to share this rare find with you guys. It’s a FREE video series that explains the process for object oriented analysis and design, (OOAD). Designing classes in an object oriented system is one of the hardest part of software development. But Derek makes it look easy, even though his code isn’t nicest to read.

Lynda.com offers a course over OOAD but it requires a monthly subscription and is MUCH longer:
https://www.lynda.com/Java-tutorials/Welcome/96949/106053-4.html

Larry Battle

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

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

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

A quick comparison between == vs equals in Java

/**
@author Larry Battle <bateru.com/news>
@date April 19, 2012
@purpose To show a simple comparison between == and equals().
*/
 
public class SameObjectTest {
	public static boolean areAssertsEnabled(){
		boolean isEnabled = false;
		try{
			assert false: "Yes asserts are enabled.";
		}catch( AssertionError e ){
			isEnabled = true;
		}
		return isEnabled;
	}
	public static void main(String[] args ){
		// All asserts should be without an error.
		String message = "Test Completed with no errors.";
		int[] ints = new int[]{ 10, 10, 20 };
		String[] strings = new String[]{ new String( "Duck" ), new String( "Duck" ), new String( "Goose!" ) };
		Worker[] workers = new Worker[]{ new Worker( "Working" ), new Worker( "Working" ), new Worker( "Sleeping" ) };
 
		assert ints[0] == ints[1] : "10 is 10";
		assert ints[1] != ints[2] : "10 is not 20";
		// Primative data types can't use ints[i].equals( ints[j] ) because they don't have methods.
 
		// Strings are a little bit more tricky. Go here for more inforamtion. http://www.janeg.ca/scjp/lang/strLiteral.html
		assert strings[0] == strings[0]: "An equality check, ==, on objects compares the references, not the values.";
		assert strings[0] != strings[1]: "strings[0] and strings[1] do not have the same reference point. In otherwords, that don't have the same Class and hashCodes.";
		assert strings[0].equals( strings[1] ): "String equals methods is predefined to compare the value of the string objects.";
		assert !strings[0].equals( strings[2] ): "the string duck should not equal the string goose!";
 
		// You have to override the equals methods for user-defined objects.
		assert workers[0] != workers[1]: "workers[0] and workers[1] have two different hash values.";
		assert workers[0].equals( workers[1] ): "However workers[0] and workers[1] are equivalent to eachother, according to the equals method.";
		assert !workers[1].equals( workers[2] ): "But this is not the case for workers[1] and workers[2].";
 
		message = ( areAssertsEnabled() ) ? message : "Asserts are disabled! Please enable with the switch -ea, ex. java -ea ";
		System.out.println( message );
	}
}
class Worker{
	public String status;
	public Worker( String status ){
		this.status = ( status != null ) ? status : "Unknown";
	}
	public boolean equals( Object obj ){
		boolean areSame = false;
		Worker x;
		if( obj != null  ){
			if( obj == this ){
				areSame = true;
			}
			if( this.getClass() == obj.getClass() ){
				x = (Worker) obj;
				if( x != null && x.status.equals( this.status ) ){
					areSame = true;
				}
			}
		}
		return areSame;
	}
}

Larry Battle

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

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Use the contents of a jar file.

How to view the contents of a jar file.
Command

// From the cmd line
jar -tvf fileName.jar

Example Output

C:\BrowserGUI\dist>jar -tvf BrowserGUI.jar
     0 Sat Nov 26 21:46:28 PST 2011 META-INF/
   106 Sat Nov 26 21:46:26 PST 2011 META-INF/MANIFEST.MF
 39313 Sat Nov 26 21:46:26 PST 2011 GUIAboutTab.class
 39952 Sat Nov 26 21:46:26 PST 2011 GUIFrame.class
 45354 Sat Nov 26 21:46:26 PST 2011 GUITabbedPane.class
  5244 Sat Nov 26 21:46:26 PST 2011 GUImain.class
   691 Sat Nov 26 21:46:26 PST 2011 NewJFrame$1.class
   509 Sat Nov 26 21:46:26 PST 2011 NewJFrame$2.class
 15458 Sat Nov 26 21:46:26 PST 2011 NewJFrame.class

Check out the Java Package Tutorial for more infomation.

Larry Battle

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

More Posts - Website

Follow Me:
TwitterLinkedInYouTube