Here I begin my design patterns video tutorial. I have talked a great deal about OOP design principles in the past. I’ve shown you how to turn requirements into a UML diagram. Then I covered how to turn a UML diagram into a class.
This tutorial will start off by revisiting OOP concepts. I’ll also provide answers to the most common questions I receive.
By the end, you’ll be able to use design patterns to solve common software design problems.
If you like videos like this, please tell Google by clicking here
Sharing is nice
Code from the Video
Animal.java
public class Animal {
private String name;
private double height;
private int weight;
private String favFood;
private double speed;
private String sound;
public void setName(String newName){ name = newName; }
public String getName(){ return name; }
public void setHeight(double newHeight){ height = newHeight; }
public double getHeight(){ return height; }
public void setWeight(int newWeight){
if (newWeight > 0){
weight = newWeight;
} else {
System.out.println("Weight must be bigger than 0");
}
}
public double getWeight(){ return weight; }
public void setFavFood(String newFavFood){ favFood = newFavFood; }
public String getFavFood(){ return favFood; }
public void setSpeed(double newSpeed){ speed = newSpeed; }
public double getSpeed(){ return speed; }
public void setSound(String newSound){ sound = newSound; }
public String getSound(){ return sound; }
// A private method can only be accessed by other public methods
// that are in the same class
private void bePrivate(){
System.out.println("I'm a private method");
}
public static void main(String[] args){
Animal dog = new Animal();
dog.setName("Grover");
System.out.println(dog.getName());
}
}
Dog.java
public class Dog extends Animal{
public void digHole(){
System.out.println("Dug a hole");
}
public void changeVar(int randNum){
randNum = 12;
System.out.println("randNum in method value: " + randNum);
}
/* This private method can only be accessed through using other
* methods in the class */
private void bePrivate(){
System.out.println("In a private method");
}
public void accessPrivate(){
bePrivate();
}
// The constructor initializes all objects
public Dog(){
// Executes the parents constructor
// Every class has a constructor whether you make it or not
super();
// Sets bark for all Dog objects by default
setSound("Bark");
}
}
Cat.java
public class Cat extends Animal{
// The constructor initializes all objects
public Cat(){
// Executes the parents constructor
// Every class has a constructor whether you make it or not
super();
// Sets bark for all Dog objects by default
setSound("Meow");
}
// If you want to make sure a method isn't overridden mark it as Final
final void attack(){
// Do stuff that can never change
}
// A field marked with final can't be changed
public static final double FAVNUMBER = 3.14;
// A class labeled as final can't be extended
}
WorkWithAnimals.java
public class WorkWithAnimals{
int justANum = 10;
public static void main(String[] args){
Dog fido = new Dog();
fido.setName("Fido");
System.out.println(fido.getName());
fido.digHole();
fido.setWeight(-1);
// Everything is pass by value
// The original is not effected by changes in methods
int randNum = 10;
fido.changeVar(randNum);
System.out.println("randNum after method call: " + randNum);
// Objects are passed by reference to the original object
// Changes in methods do effect the object
changeObjectName(fido);
System.out.println("Dog name after method call: " + fido.getName());
System.out.println("Animal Sound: " + fido.getSound());
// Create a Dog and Cat object with the super class
// but the Dog and Cat reference type
Animal doggy = new Dog();
Animal kitty = new Cat();
System.out.println("Doggy says: " + doggy.getSound());
System.out.println("Kitty says: " + kitty.getSound() + "\n");
// Now you can make arrays of Animals and everything just works
Animal[] animals = new Animal[4];
animals[0] = doggy;
animals[1] = kitty;
System.out.println("Doggy says: " +animals[0].getSound());
System.out.println("Kitty says: " +animals[1].getSound() + "\n");
// Sends Animal objects for processing in a method
speakAnimal(doggy);
// Polymorphism allows you to write methods that don't need to
// change if new subclasses are created.
// You can't reference methods, or fields that aren't in Animal
// if you do, you'll have to cast to the required object
((Dog) doggy).digHole();
// You can't use non-static variables or methods in a static function
// System.out.println(justANum);
// sayHello();
// You can't call a private method even if you define it in
// the subclass
// fido.bePrivate();
// You can execute a private method by using another public
// method in the class
fido.accessPrivate();
// Creating a Giraffe from an abstract class
Giraffe giraffe = new Giraffe();
giraffe.setName("Frank");
System.out.println(giraffe.getName());
}
// Any methods that are in a class and not tied to an object must
// be labeled static. Every object created by this class will
// share just one static method
public static void changeObjectName(Dog fido){
fido.setName("Marcus");
}
// Receives Animal objects and makes them speak
public static void speakAnimal(Animal randAnimal){
System.out.println("Animal says: " + randAnimal.getSound());
}
// This is a non-static method used to demonstrate that you can't
// call a non-static method inside a static method
public void sayHello(){
System.out.println("Hello");
}
}
woww man so finally you started with design patterns, please covers as much design patterns as you can.
Thanks.
I will definitely do that. I sort of covered design patterns during the last 10 videos of my Java video tutorial. I did it in an interactive manor. This time I’ll do it in a much more straight forward way. Thanks for watching
Thanks, and i did’t got an email notification for you this comment.
Hello Derek,
I’m 14 and I am very inspired by your videos. They are extremely informative and funny and I think they compare to the professional one’s on Lynda.com. I would like to emulate your awesome approach and try to make some videos myself teaching web design and programming. Could you please make a quick tutorial explaining how to plan, record, edit, and deliver your amazing videos. You don’t have to go over everything but just some basic techniques. Again, awesome videos! Thanks, Sid.
Hi Sid, Thank you for all of the kind words
I pretty much cover what I do in Become a YouTube Partner.
The only thing that changed since then is that I now record screencasts with Camtasia 2. Apple broke the Quicktime screen capture tool after the last OS upgrade. I also got a Panasonic HDC-TM90 camcorder. It is a fabulous camera for the money. The only thing bad about it is the audio recording. I checked and it looks like you can only find it used? I plan on getting a Zoom H1 Digital Recorder to solve my audio issues.
As per my style and why I’m different. YouTube tells us to make short videos that are from 3 to 5 minutes in length maximum to maximize earnings. That is why most tutorial people make videos much shorter than I. You are also told to stick with one type of video. I break these and other rules because I don’t particularly care about making money. I also change the way I make videos constantly.
Feel free to ask any questions. If you want to know how to vlog and make money like the pros, take a look at How to video blog. I go everything in detail there
Derek,
Thank you for the quick and kind reply. I just have one more quick question. Since you use Camtasia 2, do you still use iMovie to edit you video or does Camtasia have editing features for, say, editing out the pauses in the raw video? Much thanks, Sid.
You’re very welcome. I still use iMovie. Camtasias editing features are very poor. I like the cursor highlights and a few other things it brings though
Thank you So Much Derek.Finally your with Design Patterns.Your Tutorials are very Nice.Great Work Once Again
Thank you
I hope you find it useful
I very interested in your Java tutorials. I am starting learning design from here.
Thank you Derek.
You’re very welcome. I have about 80 tutorials so far for Java and many more are in the works. I’m glad you like them
can you please explain why you called super class constructor in the dog.java.
i am a new viewer and find your tutorials amazing!
Thank you as always!
Thank you
You execute super(); so that you can initialize anything that is needed for the super class of Dog named Animal. This is just to make sure anything that may be needed by Dog is set up properly by the Animal constructor.
This is kind of a quick review on OOP in Java. I cover the topic more thoroughly here Java OOP Tutorial. You can find all of my Java tutorials in one place on this page Java Video Tutorial. I hope that helps
Hi sir, super work sir, i have no words to appriciate u. Good work
Thank you for the kind words. I do my best and I’m glad you found the tutorials useful