• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Prototype Design Pattern

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,

While I was reading about the Prototype pattern, I couldn't understand how it can reduce the number of subclasses, and how it provides a way to add new classes at runtime. Could anyone clerify that for me? may be by examples???

Thanks a lot.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only sensible example I found was musical notes - whole note and half note. In the example system these two things have many attributes in common but different durations and graphics. One could make HalfNote and WholeNote classes that extend MusicalNote and write

With Prototype one could eliminate WholeNote and HalfNote classes, make MusicalNote concrete, and clone existing instances:

Does that help?
[ May 09, 2006: Message edited by: Stan James ]
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For true prototyping in the music example, you could just have one prototype Object:



This may not seem very useful in Java, but it is used very effectively in other languages which support copy-and-reconfigure as a single operation akin to a costructor. Some of these languages have no concept of a class at all, just various objects with dynamically attached methods and data.

In real world Java applications I have used it to model user interactions which follow something like this approach. I remember part of a user interface which had a form of rich text editing. Setting the global font and stuff changed a "global" prototype object, and entering text took a clone of the current global format object and attached it to the block of text. Any change to text style after it was entered only modified the format clone attached to the text block.

Anything with a copy-and-edit interaction cycle might potentially benefit from this pattern.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Carver:
For true prototyping in the music example, you could just have one prototype Object:



In which way is Stan's example *not* "true prototyping"?
 
Hatem Mousselly Sergieh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anything with a copy-and-edit interaction cycle might potentially benefit from this pattern.



I think this phrase is a very good conclusion that helps understanding the main purpose of this pattern.

Thank you all for your replies.

Hatem
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
In which way is Stan's example *not* "true prototyping"?



OK, perhaps my phrasing was a bit strong. :roll:

My intention was to point out that there was not really a need for two separate base classes in the given example. If music may be commonly understood as a sequence of [/i]note[/i]s, then it can be implemented using just those two abstractions. In a system using prototyping there seems little point in introducing new types (and thus reducing flexibility) just for the sake of having a deeper class hierarchy.

It's a philosophical thing, similar to claiming that a solution using static methods and data-only classes is not really Object-Oriented, even though it be implemented in an O-O language.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Carver:

My intention was to point out that there was not really a need for two separate base classes in the given example.



Ah, but I think that is what Stan was saying, too. I think he imagined the instances in the Prototype example to be defined as something like



That's how I know the canonical GoF Prototype pattern.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ayup. Well, maybe 2 & 4 instead of .5 and 1. That's kind of neat that you could take any user-customized instance and think of it as a new type and clone more of them. I can imagine some kind of interactive application where the user defines a new Thing and then makes lots of them.

Any idea where in Java it would be cheaper to clone() than to create new? I'd guess most clone methods do a new anyhow, right? Maybe if clone could copy a lot of properties that would otherwise come from a database.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
Any idea where in Java it would be cheaper to clone() than to create new?



I have no idea, but that's not the intent of the pattern, anyway, is it?

The point of using clone for the prototype pattern is that the client doesn't need to know about what parameters he needs to pass to which constructor.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Re "cheaper to clone" ... does show up in the motivation to use Prototype in some write-ups. That doesn't make it right of course. Reduction in the number of classes is usually there, too, but not always well explained.
 
I don't like that guy. The tiny ad agrees with me.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic