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 [googleplusone]
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 🙂
Hi,
Your videos really helped me.Where I can find the slides which you used in the tutorial?
Thanks and Regards,
Abhishek
Hi Abhishek,
Thank you 🙂 Sorry, but I don’t have the slides. I deleted them after making the tutorial. Sorry about that
Hi Derek,
Can you provide the link for design patterns where I can find all the tutorials at one place (Links on the right hand side) just like how you have done it for Android Tutorials.
Thanks,
Siva
Hi Siva,
Here is my Design Pattern Video Tutorial. I also have a Refactoring Video Tutorial. Everything including the links to the articles are on one page.
I hoep you find them useful 🙂 – Derek
Derek,
I can’t thank you enough. Your design pattern and refactoring tutorials have helped me out a lot. I studied electrical engineering, so I never heard of design patterns until I stumbled across your page. I had a job interview for a software engineering position and I was able to answer every technical question thrown at me on design patterns because of your A++ tutorials. Continue the great work!!!
Jean
Jean,
I’m very happy that I could help 🙂 This web site was put together to help people get jobs, so I’m happy that it has done that for some people. You’re very welcome.
Hi, and thank you for the tutorials!
@line#81 I think you meant ” Animal giraffe = new Animal(); “
Hello Derek ,
very useful tutorials , simply ,nicely ,professionally explained , just i want a litle help about the OOP concepts and i would like to share with us any exercices to practice and well understand that OO wOrLd , thanks a lot
Hello,
Thank you 🙂 I’m glad they helped. I think you’ll find many examples on how you can utilize OOP concepts in this tutorial as well as in my object oriented design and refactoring tutorials. I’ll be making many more Java tutorials soon.
Thanks a lot !
You’re very welcome 🙂
hello Derek ,
it’s good to comment again , to tell you how is very useful to follow your tutorials .
i want just to ask if there is a tutorial about j2ee patterns ?
thanks again 🙂
Thank you 🙂 I’ll cover java enterprise after Android. Sorry about the wait.
Hi Derek,
First a big thank you for the great training content you provide free of cost. It really makes the Internet a better place as a whole.
I was wondering if you could post or tell me a more structured way of approaching all this material. My end goal is to be an Android programmer/engineer. I have basic knowledge of Java and data structures but haven’t really dived deep into OO with hands-on large projects yet. I’m confused regarding which of your course material to tackle first – UML, Object Oriented Design or Design Patterns? And then the Android Studio tutorials? Or in some other order?
It would be really helpful if you could provide a structure to learning your various courses with Android as the destination.
Thank You once again!!
You’re very welcome 🙂 I very much enjoy making these videos. You could go in a few directions if your only goal is to make Android apps. If you feel you need more knowledge in Java just jump into my Android for Beginners tutorial. By the end you’ll be able to make advanced Java Android videos. You’ll have fun doing it as well.
If you prefer a technical route and are good with Java then try this order UML, Object Oriented Design, Refactoring and then either my Eclipse or Android Studio tutorial.
I am a fully blind computer science student and this is just the resource I needed 🙂 I can follow along with you in the code because you are very verbal in reading out what you are doing, which is a huge plus for me. I needed a resource that takes you from a quick refresher of OO concepts into how they are applied in the various design patterns that are out there, and it seems I have found just that 🙂 keep rockin’ 🙂
Thanks a lot from the waterlogged Netherlands 😉
Thank you very much for telling me that I was able to help 🙂 I greatly appreciate that and I wish you all the best.
Great work ……….spreading knowledge is a divine thing keep it up Derek…………
Thank you 🙂 Many more tutorials are coming. I’m glad I can help.
Thank you 🙂 You really made a great and awesome tutorials
You’re very welcome 🙂 Thank you