Code Refactoring 10

Get the Code:

In this part of the code refactoring tutorial I’m going to revisit the Builder Design Pattern for a couple of reasons. First, I receive a bunch of questions about it. Secondly, in the next tutorial I’ll show you how to build composites, which were covered in the last part of the code refactoring tutorial, with the builder pattern.

The builder pattern allows you to build complex objects in a series of steps. In the video and code above, we will build sandwiches.






30 responses to “Code Refactoring 10”

  1. Adriano Fonseca Avatar

    Hello Derek, congrats for a work really well done! Your channel or better our channel that you did for us, it is amazing!
    Just a question an implementation such as the following is a variation of the Builder Design pattern or is something else?
    A hug from Brazil 🙂

    public class Page {
    private final int pageNumber;
    private final String content;

    public int getPageNumber() {
    return pageNumber;

    public String getContent(){
    return content;

    public String toString() {
    return "Page [pageNumber=" + pageNumber + ", content=" + content + "]";

    public Page(Builder builder){

    public static class Builder{

    private int pageNumber;
    private String content;

    public Builder pageNumber(int nr){
    return this;

    public Builder content(String content){
    return this;

    public Page build(){
    return new Page(this);



  2. Brax1982 Avatar

    As with the previous video on Builder Pattern, I don't think this makes any sense. It's not good practice to let a class call the methods of another class which the latter is perfectly capable of calling by itself. At least not unless you want to call only a subset of its methods. Then again, that's not what happens in any of these videos. We always let the director/engineer/artist call all the methods inside the class, anyway.

  3. Lester Avatar

    Hi +Derek Banas I am confused about the Builder Design Pattern and the Template Method Design Pattern due to the samiliar Sandwich case. Does the Builder Design Pattern use for ONE complex object with a series of steps while the Template Method Design Pattern can use more than two similar objects with the similar series of steps?

  4. Stenodyon Avatar

    This is more of a Design Pattern tutorial rather than a Code Refactoring tutorial :/

  5. Derek Banas Avatar

    I'm working on it 🙂

  6. skazis87 Avatar

    please, please do JUnit. thanks.

  7. Derek Banas Avatar

    That is great! I may put them on my site with this article. If you want me to link to your site Ill do that

  8. syzer Avatar

    I have those files in PHP. PM me if u want them. PS they look almost exactly the same.

  9. Derek Banas Avatar

    That's funny 🙂

  10. Mladen Uzelac Avatar

    I get hungry by watching your videos. You talk only about food. 🙂

  11. Bangash Avatar

    I'll be waiting in anticipation for that time….

  12. Derek Banas Avatar

    Thank you 🙂 when I cover php again I'll do it with the same level of detail I'm now doing with java

  13. JC_Studio Avatar

    Hi Derek, with code refactoring goes to part 10, i'm just wondering if there's any chance you would cover C++ in the next series? : ) btw, great videos as always

  14. Derek Banas Avatar

    It makes sense to cover JUnit with refactoring. I'll cover it very soon. Thank you for reminding me 🙂

  15. Harpal singh Avatar

    I Derek, i am really enjoying your all videos… thank you very much…when you going to upload JUnit related tutorials….i am eagerly waiting… 🙂 ….

  16. Fredrik Westman Avatar

    and btw, you said it was going to be a opengl tutorial rigth ? and if, when do you estimate you will do it?

  17. Fredrik Westman Avatar

    🙂 you seen the movie cloud atlas ? (if you havnt i strongly recomend it, its more then a movie. Its a piece of art). Things like this makes me think in terms of that movie. How small actions and events can make such a big impact on the future. Like the butterfly effect. But this isnt a small thing, its huge. Information, education spreading, freely across the internet. I wish i could look in to the future and see the impact of these videos.

  18. Derek Banas Avatar

    That was part of my goal 🙂 I want to cover math and physics as well by the end of the year

  19. Fredrik Westman Avatar

    At this rate of upplodning when your kid has grown up, this is going to be a encyclopedia. Any time he/she askes about anything you can just say " yh, check this tutorial out" ^^

  20. Derek Banas Avatar

    Я не говорю на русском тоже. Именно поэтому у нас есть переводчики 🙂

  21. Derek Banas Avatar

    I'm glad you liked it. Next Ill cover how to make composites with a builder. I'm really enjoying these videos and will try to make one every day

  22. Truthiness231 Avatar

    Ahh, finally one of these refactoring tuts that I get to learn and improve from. This sort of job I'd typically use something closer to the factory method (a very general factory that ends up confusing as all hell to read and use), but I can see why the director+builder method is superior for the task.

    And thanks for RainoBoy for pointing out: congrats on the 500 videos milestone! I wish you the best in hitting the big 1000 mark, and I'll be sticking around to see it if/when it happens ^.^

  23. Derek Banas Avatar

    நன்றி. நான் நாளை மற்றொரு வீடியோ வேண்டும்

  24. 123japanuser Avatar

    Շնորհավոր 500 բեռնումները.

  25. 123japanuser Avatar

    ஹலோ பயிற்சியாளர்,

    நமக்கு ஞானத்தை மற்றொரு அளவை நன்றி.

    என் இரவு தங்க பெறவில்லை பொறுப்பு.

    வெண்கல ஒரு தீர்வு வேண்டும்.

    நான் என்னுடைய வேண்டும் கருதுகின்றன என்ன பெற நம்பிக்கை.


    இந்த சேனல் வணக்கங்களை நீங்கள் தீவிர ஆதரவாளர்.


  26. Derek Banas Avatar

    while(infiniti) system.out.println("You're Welcome")

  27. Warden Avatar


  28. Derek Banas Avatar

    Thank you 🙂 I'm trying to do 1 a day for as long as possible. I thought I had 498? Either way, I have deleted so many I'm well past 500 at this point.

  29. Raino Avatar

    Yay 500 videos 😀 Keep'em coming.. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *