Object Oriented Design 10

Object Oriented Design 10In this tutorial, I cover GRASP, or General Responsibility Assignment Software Patterns. GRASP defines rules you can use to decide which objects should have which responsibilities in your Object design.

Here I cover the following rules: Creator, Expert, Coupling, Controller and High Cohesion. The tutorial includes a presentation, code examples, sequence diagrams and class diagrams to make everything understandable and interesting. The code used follows the video.

If you like videos like this, it helps to tell Google [googleplusone]

If you know anyone interested in Object Oriented Design, feel free to share

Code From the Video

Bike.java

public class Bike {
	private Wheel theWheel;
	private Frame theFrame;

	Bike(Wheel newWheel, Frame newFrame) {
		theWheel = newWheel;
		theFrame = newFrame;
	}
	
	Bike(int wheelWid, int frameLen){
		
		theWheel = new Wheel(wheelWid);
		theFrame = new Frame(frameLen);
		
	}
	
	public static void main(String[] args){
		
		// Without creator
		
		Wheel wheel = new Wheel(24);
		
		Frame frame = new Frame(52);
		
		Bike bike = new Bike(wheel, frame);
		
		// With creator
		
		Bike bike2 = new Bike(24, 52);
		
	}

}

Wheel.java

class Wheel {
	private int width;

	Wheel(int newWidth) {
		width = newWidth;
	}
	
}

Frame.java

class Frame {
	private int length;

	Frame(int newLength) {
		length = newLength;
	}
}

ListAnimals.java

public class ListAnimals {

	private Monkey theMonkey;
	private Lion theLion;

	ListAnimals(Monkey monkey, Lion lion) {
		theMonkey = monkey;
		theLion = lion;
	}

	public void displayAnimals() {
		
		System.out.println(theMonkey);
		System.out.println(theLion);
		
	}
	
	public static void main(String[] args){
		
		Monkey monkey = new Monkey("Max");
		
		Lion lion = new Lion("Tom");
		
		ListAnimals listAnimals = new ListAnimals(monkey, lion);
		
		listAnimals.displayAnimals();
		
	}
	
}

Monkey.java

public class Monkey {

	private String name;
	
	Monkey(String newName){
		
		name = newName;
		
	}
	
	public String toString(){
		
		return "Monkey named " + name;
		
	}
	
}

Lion.java

public class Lion {
	
	private String name;
	
	Lion(String newName){
		
		name = newName;
		
	}
	
	public String toString(){
		
		return "Lion named " + name;
		
	}

}

Animal.java

public class Animal {

	private String name;
	
	Animal(String newName){
		
		name = newName;
		
	}
	
	public String toString(){
		
		return "Animal named " + name;
		
	}
	
}

Monkey2.java

public class Monkey2 extends Animal {

	private String name;
	
	Monkey2(String newName){
		
		super(newName);
		name = newName;
		
	}
	
	
	public String toString(){
		
		return "Monkey named " + name;
		
	}
	
}

Lion2.java

public class Lion2 extends Animal {

	private String name;
	
	Lion2(String newName){
		
		super(newName);
		name = newName;
		
	}
	
	
	public String toString(){
		
		return "Lion named " + name;
		
	}
	
}

ListAnimals2.java

import java.util.ArrayList;

public class ListAnimals2 {

	private ArrayList<Animal> theAnimals = new ArrayList<Animal>();
	
	public void addAnimalToList(Animal newAnimal){
		
		theAnimals.add(newAnimal);
		
	}

	public void displayAnimals() {
		
		for(Animal animal : theAnimals){
			
			System.out.println(animal);
			
		}
		
	}
	
	public static void main(String[] args){
		
		Animal monkey = new Monkey2("Max");
		
		Animal lion = new Lion2("Tom");
		
		Animal lion2 = new Lion2("Carl");
		
		ListAnimals2 listAnimals = new ListAnimals2();
		
		listAnimals.addAnimalToList(monkey);
		
		listAnimals.addAnimalToList(lion);
		
		listAnimals.addAnimalToList(lion2);
		
		listAnimals.displayAnimals();
		
	}
	
}

2 Responses to “Object Oriented Design 10”

  1. Lucas says:

    Hi Derek,

    Best wishes for 2013 for you and your family! Lots of prosperity, health, smile and good ideas for tutorials.

Leave a Reply

Your email address will not be published.

Google+