Need of Prototype design pattern?

When exactly do we go for the prototype design pattern ? What I understood from various resources is to use the pattern when we find instantiating a class using new Operator Or when we want the run-time initialization of object. So How exactly we could define an instantiation of class using new operator to be expensive? when the class definition contains too many fields and that too seldom going to change in future???

Is instantiating a chess board with all players set up for every new game will be too costlier to be instantiated using new operator?

Can someone through some light on same?


Indeed Prototype pattern isn't much related to OOP. ie its just a workaround to increase performance (Specially in Java). The idea is when you see direct creation of an object using new is costly, just clone an existing one rather than newly creation.

One question you might have here is, are they refers to the same object (same memory) there after or they are different? Indeed they are 2 independent objects (memories) there after. Only the creation of the second object is done with cloning the first one. Thereafter they are different entities.

When exactly do we go for the prototype design pattern ?

As suggested above, when you think direct creation takes much time. ie costly operations are there (ex: db access etc.). And you think that cloning will be cheaper and doesn't harm the business logic. ie cloned attributes at the creation won't be a problem.

---------Regarding Chess Board Question----------

Yes, indeed you can apply prototype pattern there.

  • At the start of your app, you can initialize a chess board instance.
  • Whenever the player wants to start a new game, you can use that instance to clone and make a new one.
  • There, because you are making one at the start of the app and keeping it in the memory, memory-usage will increase.
  • But since you are cloning at every new game, you are saving the processing time there.
  • So basically its a way to use additional memory instead of processing time to improve the performance.
  • 链接地址: http://www.djcxy.com/p/26932.html

    上一篇: 为什么C中需要volatile?

    下一篇: 需要Prototype设计模式?