Prototype Design Pattern in Java

Поділитися
Вставка
  • Опубліковано 16 лют 2017
  • Recommended Book :
    Head First Design Pattern : amzn.to/2pY5xbR
    Prototype Design Pattern in Java
    This video contains both theory and practical session.
    Prototype design pattern belongs to Creational design pattern which belongs to Design Patterns in java.
    Prototype pattern refers to creating duplicate object while keeping performance in mind.
    Trainer: Navin Reddy
    Check out our website: www.telusko.com
    Follow Telusko on Twitter: / navinreddy20
    Follow on Facebook:
    Telusko : / teluskolearnings
    Navin Reddy : / navintelusko
    Follow Navin Reddy on Instagram: / navinreddy20
    Subscribe to our other channel:
    Navin Reddy : / @navinreddy
    Telusko Hindi :
    / @teluskohindi
    Subscribe to the channel and learn Programming in easy way.
    Java Tutorial for Beginners : goo.gl/p10QfB
    Scala Tutorials for Java Developers : goo.gl/8H1aE5
    C Tutorial Playlist : goo.gl/8v92pu
    Android Tutorial for Beginners Playlist : goo.gl/MzlIUJ
    XML Tutorial : goo.gl/Eo79do
    Design Patterns in Java : goo.gl/Kd2MWE
    Java Servlet : goo.gl/R5nHp8
    Hibernate Tutorial :goo.gl/N5ONYg
    Spring MVC Tutorial : goo.gl/9ubbG2
    OpenShift Tutorial for Beginners : goo.gl/s58BQH
    Spring Framework with Maven : goo.gl/MaEluO
    Sql Tutorial for Beginners : goo.gl/x3PrTg
    String Handling in Java : goo.gl/zUdPwa
    Array in Java : goo.gl/uXTaUy
    Socket Programming in Java : goo.gl/jlMEbg
    Exception Handling in Java : goo.gl/N4NbAW
    Regards,
    Navin Reddy
  • Наука та технологія

КОМЕНТАРІ • 117

  • @Ranjith_P
    @Ranjith_P 6 років тому +64

    I have been called a lot of things in my life , never been called a Alien ...

    • @mohan-ri6ze
      @mohan-ri6ze 3 роки тому +3

      He thinks he have subscribers from other planets also

    • @vimalbhatt5359
      @vimalbhatt5359 Рік тому

      He is from Pluto that's why we are aliens to him

  • @mishamovdivar
    @mishamovdivar 7 років тому +137

    Demo for shallow copy is wrong.
    You are removing third item from bs BEFORE you clone it, so, whether it's deep or shallow copy, third element wont be copied into bs1.
    Instead you should remove third element from bs1 and print bs and observe that third element was also removed from bs (which demos that bs and bs1 are referring to same memory (shallow copy))

  • @naveengamini3445
    @naveengamini3445 5 років тому +3

    You are aweome Navin.. I like your live programing skills with fastness and help us to learn the concept and also live coding, test the code and debug.. Awesome videos..Thanks

  • @sakshisharma7097
    @sakshisharma7097 7 років тому +2

    i love the way you make everything simple and cooler :)

  • @paranjisridhar5529
    @paranjisridhar5529 6 років тому +35

    There is a problem in your example. You are still doing shallow cloning of the books. By iterating thru the list of books and adding each book to the second book shop you are still adding references to the second book shop. You can see the effect by renaming one of the books in the first bookshop instead of removing it, the name will be changed in the second one too.
    Removing the book from the first bookshop removed it from the list(which is a different object in the two bookshops) but the book is still there because the second book shop refers to it in its list.

    • @maolyherrera4240
      @maolyherrera4240 5 років тому +13

      That's correct. To make it deep; in BookShop's clone() method, under line 43: for(Book b: getBooks()) it should be added: Book newBook = new Book(); newBook.setBid=b.getId(); newBook.setBname=b.getName(); shop.getBooks().add(newBook);

    • @adityajagtap4349
      @adityajagtap4349 4 роки тому +2

      @@maolyherrera4240 Better way is use clone() in Book

    • @it-family-
      @it-family- 3 роки тому +1

      You are wrong! It is exactly a deep copy already. The idea is that the cloned object contained a List of objects in which we have first field int id (primitive - no reference) and second field String (an immutable type of object). So, no need for those object to make a copy, we could reSet them both in cloned object with no impact to origin object.

    • @abhishekkorlekar7966
      @abhishekkorlekar7966 3 роки тому +2

      Yup are you right!!
      I renamed in the first object by: obj1.getBooks().get(0).name = "Some Name";
      This changed the name in another bookShop object as well

    • @little-by-little-one-trave1770
      @little-by-little-one-trave1770 2 роки тому

      @@maolyherrera4240 Minor improvement to your code we can implement clone(deep) in Book and inside for loop we can do --> Book newBook = b.clone();

  • @amitkhandey3582
    @amitkhandey3582 2 роки тому

    Hi Navin, I you have excellent ability to explain things in very simple way.

  • @sandhyaaa24
    @sandhyaaa24 7 років тому

    your way of teaching is improving day by day...I am watching your videos when u have other channel name naveen reddy

  • @liponrr9737
    @liponrr9737 7 років тому +3

    You are a awesome teacher

  • @buzzbummer2111
    @buzzbummer2111 5 років тому

    Awesome tutorial video, Thanks Sir

  • @vladimirjevremov5581
    @vladimirjevremov5581 7 років тому +2

    Great work, keep it up !!

  • @vinaysingh2971
    @vinaysingh2971 3 роки тому

    Very well thoughtfully explained

  • @vnm_8945
    @vnm_8945 5 років тому

    can you make more of this, really appreciating your videos!

  • @jasper5016
    @jasper5016 Рік тому +2

    I really love your explanations. Why didn't you cover all design patterns?

  • @arpitmehta9655
    @arpitmehta9655 7 років тому

    Thanks sir :) for assisting

  • @aashishgoyal1436
    @aashishgoyal1436 6 років тому

    thanks a lot naveen sir

  • @SunilKumar-pd9eh
    @SunilKumar-pd9eh 6 років тому +6

    when you were explaining about deep copy, I felt there was no use of overriding clone method, you can do the same with any normal method as you were not leveraging the advantage of overriding the clone method. You can simply say copy method and do the same stuff. It doesn't need to be clone really. Please correct me if I understood wrong.

  • @kathiravanjagadeesan
    @kathiravanjagadeesan 5 років тому

    Awesome work by another indian .

  • @ranilutube
    @ranilutube 6 років тому

    This is very useful..

  • @swatiyadav9842
    @swatiyadav9842 4 роки тому

    Nice explaination.

  • @myangelsworld6566
    @myangelsworld6566 6 місяців тому

    awesome explanation !!!! The only thing is playback speed is a little faster.

  • @mermerjamal6662
    @mermerjamal6662 Рік тому

    thanks for you great vedio

  • @srinivasjava1932
    @srinivasjava1932 4 роки тому

    Thank you Sir

  • @chandankumar-su7ng
    @chandankumar-su7ng 4 роки тому

    thank you sir

  • @munirali137
    @munirali137 4 роки тому

    Thank you

  • @rajivraghu9857
    @rajivraghu9857 7 років тому

    pls do all design pattern video. Thanks Navin

  • @rithikraj4316
    @rithikraj4316 4 дні тому +1

    So why don't we use copyConstructor ?? which sole purpose is to give me value of other object ? or copy constructor comes under prototype design pattern ?

  • @ujjawalgupta5408
    @ujjawalgupta5408 3 роки тому +1

    While doing Shallow copy, I changed the shop name in bs1 and after changing it I printed bs but still I got shop name as 'Novelty'. So, as you said it refers to the Object then shop name should also change. Can you please explain what happend here?

  • @tusharkale9578
    @tusharkale9578 Рік тому +1

    at 13.05 .. you are first removing object book-3 and then cloning so its affecting both BookShops.. not because of shallow copy

  • @PostMapping
    @PostMapping 2 роки тому

    Thanks!

  • @kumodsharma8141
    @kumodsharma8141 5 років тому

    Nice explained

    • @kbhardwaj1989
      @kbhardwaj1989 5 років тому

      Mr KMD, you watched this video very late. Naveen sir has shared this video 1 year back. Do follow his vidoes regularly. :)

  • @abdollahsobhy9743
    @abdollahsobhy9743 6 років тому

    Hi, Thanks for your effort. I just face a problem with the code. It gives me NullPointerException in the clone method at this line (bookShop.getBooks().add(b);). What is the problem?

  • @hardstuck170
    @hardstuck170 7 років тому +1

    questions:
    1- why no public/private indentifier before the List field, what's the meaning of no identifier?
    2- why do you fetch the list with the getter. I think you can do it directly since you are in the same class

    • @KushalMaharana
      @KushalMaharana 7 років тому +1

      1. public protected private default are not identifiers they are access modifiers. When you don't specify a access modifier compiler consider as a default modifier that means that list can be access in the same package only.
      2. Every object of type BookShop contains that list so it's not used for internal operations of BookShop class. Thats why sir have set a getter method to call it on object.
      Please correct me if I'm wrong!

  • @noahvo6673
    @noahvo6673 3 роки тому +1

    Im not sure the demo is correct. does this mean:
    one method they use the same memory for the database and have 2 pointer point to that memory. This cause change one object will lead to the other change too
    and the other, they use different memory so they are independent to each other

  • @thedragoncorpse6403
    @thedragoncorpse6403 4 роки тому

    please tell me why do we use clone method instead of just copying the reference from older to newer like
    Bookshop bs1=bs;

  • @pankajmenkudale
    @pankajmenkudale 5 років тому

    will you upload more video on design pattern ?

  • @venkatk7914
    @venkatk7914 5 років тому

    nice vedio

  • @louaykhammar7268
    @louaykhammar7268 2 роки тому

    thanks

  • @pratikchauhan6077
    @pratikchauhan6077 7 років тому

    sir can you make a video in jsp with hibernate then clone object
    please sir give me this type of tutorial

  • @kaoushikkumar8472
    @kaoushikkumar8472 4 роки тому

    Hi Navin,
    Could we have small demo for the same using Python code? It is desperately required by the users like me.

  • @yatri6329
    @yatri6329 2 роки тому

    What about immutable obj do we need copies of that. If not why?

  • @asheeshpratapsingh4948
    @asheeshpratapsingh4948 6 років тому

    is Use of prototype design pattern with factory Pattern good approach ?

  • @rajsaraogi
    @rajsaraogi Рік тому

    How implementation of Cloneable given permission to class to add clone method, can you clarify more on this.

  • @rakshamalviya3450
    @rakshamalviya3450 4 роки тому +1

    At the time of shallow cloning u r removing element and then cloning so any how third element will not reflect in the copy object whether it is shallow or deep cloning. You need to update ur video

  • @polojoy6824
    @polojoy6824 4 роки тому

    thank q

  • @manjusha9630
    @manjusha9630 4 роки тому

    plz do facade design pattern

  • @cristianflorentintarta885
    @cristianflorentintarta885 Рік тому

    Navin's videos should not have a dislike button / it's useless :)

  • @abhilash1755
    @abhilash1755 Рік тому

    If we are creating another object inside clone method is it really prototyping ?

  • @user-lx7gf8kn3n
    @user-lx7gf8kn3n 10 місяців тому

    some articles on google says there are 3 components in prototype design pattern, 1.prototype, 2,ConcreteProtoype and 3.Client in this video who is which one.

  • @xenon4602
    @xenon4602 4 роки тому +2

    his head shines brighter than my life

  • @JafarAliyev
    @JafarAliyev 8 місяців тому

    Mistake in 13:36 time. In order to demonstrate shallow cloning you had to remove an element after calling clone() method. Not before

  • @niteshagarwal4934
    @niteshagarwal4934 4 роки тому

    why we are using clone method.?we can create any custom method and do the same task.How does it is different from the clone?

  • @arindamsarkar3383
    @arindamsarkar3383 2 роки тому

    Inside your overridden clone() method you are using "BookShop shop=new BookShop()". Is it not creating a new object through new keywords instead of cloning?

  • @ramyashanthisalugu4758
    @ramyashanthisalugu4758 5 років тому

    Can't we just assign bs1 = bs and if we want a different bookshop name just set it as bs1.setShopName()

  • @sakuragi1111
    @sakuragi1111 3 роки тому

    Book added into the new book shop is still the reference.

  • @vigneshv1517
    @vigneshv1517 7 років тому +4

    prototype design pattern another good example is... one friend downloads a movie from internet and we people get that using pendrive. am i correct sir??

  • @ashokselvaraj8152
    @ashokselvaraj8152 4 роки тому

    Liked

  • @gouraharidas
    @gouraharidas Рік тому

    Are you not doing the swallow copy of the objects in the loop? You should have written list.add(b.clone())

  • @prathammodii
    @prathammodii Рік тому

    navin reddy bhagwan. jay ho kaka

  • @vyshnavramesh9305
    @vyshnavramesh9305 5 років тому +3

    guyz this is wrong..the shallow cloning part. Whats the use of an incorrect info tutorial?

  • @thereviewer9947
    @thereviewer9947 6 років тому

    Hi Navin! Your videos are good and you are doing a great job.
    But when it comes to this video, this is totally wrong example.
    You can verify this by commenting the custom cloning code and using the default .clone method!!

  • @sanjeen2503
    @sanjeen2503 Рік тому

    Hey Naveen, I think it would be better if you prepared the content beforehand, rather than just the outline. It is confusing when you move logging statements around on the fly.

  • @epicghost9222
    @epicghost9222 3 роки тому +1

    Deep cloning explaination or example given is wrong. please correct it in new video.
    What you explained is shallow copy both the time.

  • @murugadossarumugam7031
    @murugadossarumugam7031 7 років тому +1

    It cannot be cloning...you are again calling the database while cloning

  • @brijgarg6178
    @brijgarg6178 4 роки тому +1

    Demo of shallow copy is wrong. Please corrct the video. Otherwise people will learn it in a wrong way

  • @isoplayers
    @isoplayers 5 років тому

    the beeping in the background drove me nuts...

  • @madhuridhadi6492
    @madhuridhadi6492 6 років тому +3

    Hi Sir,
    Your explanation for shallow copy is not correct. You are saying that shallow copy does not create a new object instead assigns the reference of the cloned object which is not correct. Shallow copy do create a new object.

  • @dhirajpatil6431
    @dhirajpatil6431 6 років тому

    Not understand shallow and deep cloning difference... It's not clear by this example

  • @SantoshKumar-yg7rc
    @SantoshKumar-yg7rc 7 років тому +2

    explanation is simply confusing for even some one who knows the dp.

  • @shaswatkumar1234
    @shaswatkumar1234 7 років тому +2

    Its clear. but you are confusing me by taking the example of different type of cloning. I think it does not matter to go for 2nd type.

  • @sachinbhise4074
    @sachinbhise4074 4 роки тому

    instead of creation new object directly you are creation new object in clone method.

  • @sumitkawale9878
    @sumitkawale9878 3 роки тому

    I've new name now.
    let me introduce myself,
    I am Alien👽

  • @aslam3161
    @aslam3161 5 років тому +2

    wrong

  • @tusharshekhar2113
    @tusharshekhar2113 5 років тому

    This one was little confusing.

  • @ChandrachurMukherjee96
    @ChandrachurMukherjee96 5 років тому

    pretty clumsy.. LOL

  • @Ravikumar-gj6qw
    @Ravikumar-gj6qw 5 років тому

    Not explained properly

  • @arafathossain1803
    @arafathossain1803 3 роки тому

    So funny! BS :v

  • @krishnakrishna-yh9ly
    @krishnakrishna-yh9ly 3 роки тому

    aliens, why?

  • @xenon4602
    @xenon4602 4 роки тому

    BookShop bullshit = new BookShop();
    im i the only one who thought of this ?

  • @Charan1990ify
    @Charan1990ify 7 років тому +1

    This is worst !!

  • @wasimpatel2618
    @wasimpatel2618 11 місяців тому

    navin sir, if you havnt understood any concept completely ,please dont make videoson that.

  • @benoitgoldenberg777
    @benoitgoldenberg777 7 років тому +1

    Java is dead, long live Scala !!!

    • @sushilveerdhwaj
      @sushilveerdhwaj 7 років тому +1

      Everything is mutable except String in JAVA...sorry for your SCALA,,it will dead soon!!!

    • @chnrbk1
      @chnrbk1 6 років тому +1

      Never. billions of devices running in Java

    • @vishalaher8286
      @vishalaher8286 5 років тому

      i think they just do programming they don't know about what is going on

    • @tusharshekhar2113
      @tusharshekhar2113 5 років тому

      so its two years since you commented this and it is still number 1 programming language , though I use Scala a lot

  • @jackfrost8969
    @jackfrost8969 4 місяці тому

    This was a waste.

  • @hrs7305
    @hrs7305 2 роки тому

    You can implement deep copy with object clone method as well
    protected Object clone() throws CloneNotSupportedException {
    BookShop bs = new BookShop();
    bs.setName(this.getName());
    bs.books = (List)this.getBooks().clone(); // saves us from writing a for loop by deep copying a list
    return bs;
    }

  • @charankumar6942
    @charankumar6942 5 років тому

    You are aweome Navin.. I like your live programing skills with fastness and help us to learn the concept and also live coding, test the code and debug.. Awesome videos..Thanks

  • @epicghost9222
    @epicghost9222 3 роки тому

    Deep cloning explaination or example given is wrong. please correct it in new video.
    What you explained is shallow copy both the time.

  • @epicghost9222
    @epicghost9222 3 роки тому

    Deep cloning explaination or example given is wrong. please correct it in new video.
    What you explained is shallow copy both the time.

  • @epicghost9222
    @epicghost9222 3 роки тому

    Deep cloning explaination or example given is wrong. please correct it in new video.
    What you explained is shallow copy both the time.