java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

Prototype pattern re-factor

Open panksy2k opened this issue 7 years ago • 13 comments

Hi,

The Prototype implementations where clone() is Overridden (ElfBeast, ElfMage etc) can rather not have implementation of clone(). Instead their respective parent class (Mage, Beast) can have a general implementation of clone which creates a new instance by doing a shallow copy (Object.clone()) and then subsequently type-cast to the type of class (Mage.class or Beast.class etc) it is cloning for.

This will have advantage of:

  1. Removing verbosity among all implementations and dedicate the responsibility of cloning the object by their respective interfaces (abstract class).

  2. Avoid using 'new' more than once and using only whilst cloning the prototype object that is passed in HeroFactoryImpl.

Regards, panksy2k

panksy2k avatar May 29 '17 19:05 panksy2k

Hi @panksy2k , Say we move the clone implementation from ElfBeast to Beast by doing this.

public Beast clone() throws CloneNotSupportedException { return (Beast)( super.clone() ); }

Then if we call clone method on an object of ElfBeast, it will return an Object of type Beast, not ElfBeast. That is why, clone() implementation should be in ElfBeast class.

ghost avatar Jun 04 '17 19:06 ghost

@panksy2k @ghost, Adding clone() method (copy() in this case), fights inheritance/polymorphism. Any sort of instantiation of an abstract class should throw a compile-time error. The current implementation should stand as is (only a concrete class is instantiated). If Beast were to have any attributes to be initialized, it should be done via constructor inheritance.

jwheeler31 avatar Mar 13 '19 22:03 jwheeler31

Under construction by https://github.com/jasciiz

iluwatar avatar Jul 06 '20 16:07 iluwatar

@jasciiz can you please share any progress you have made for this issue?

ohbus avatar Jan 06 '21 09:01 ohbus

@ohbus : Can I pick up this issue? It is not assigned to anyone now.

barathgdkrish avatar May 08 '21 05:05 barathgdkrish

Sure thing @barathgdkrish.

Please mention a timeline for when can we expect a PR against this issue.

ohbus avatar May 08 '21 06:05 ohbus

Hi @ohbus , I have forked and cloned the repo. This is my first contribution, I'll study the ask and the code and will let you know the timeline by tomorrow EOD?

barathgdkrish avatar May 08 '21 07:05 barathgdkrish

Hey @ohbus, @barathgdkrish is still working on this or shall I start with this?

vrushofficial avatar Jul 28 '21 06:07 vrushofficial

Hi @vrushabhjoshi, I'm still working on this.


From: Vrushabh Joshi @.***> Sent: Wednesday, 28 July 2021, 12:26 To: iluwatar/java-design-patterns Cc: Barath G D Krishnan; Mention Subject: Re: [iluwatar/java-design-patterns] Prototype pattern re-factor (#584)

Hey @ohbushttps://github.com/ohbus, @barathgdkrishhttps://github.com/barathgdkrish is still working on this or shall I start with this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/iluwatar/java-design-patterns/issues/584#issuecomment-888061654, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AOOLDWMGXNS6NJDK334RS3LTZ6S23ANCNFSM4DNGJTXA.

barathgdkrish avatar Jul 28 '21 07:07 barathgdkrish

How about adding a default method in typed prototype abstract class itself?

@Slf4j
public abstract class Prototype<T> implements Cloneable {

  public T copy() {
    try {
      return (T) super.clone();
    } catch (CloneNotSupportedException e) {
      LOGGER.error("Failed to clone {}!", this.getClass().getName());
    }
    return null;
  }
}

Doing changes with respect to this class works fine and all test were passed. Not sure whether this is a good practice or not.

ghost avatar Sep 05 '21 16:09 ghost

Hi, if noone else is working on this, I could pick up this issue. Is that okay? :)

ThexXTURBOXx avatar Oct 01 '21 22:10 ThexXTURBOXx

Yes, since there is no sign of PR from @barathgdkrish you can go ahead @ThexXTURBOXx

iluwatar avatar Oct 05 '21 19:10 iluwatar

There's a pr for the fix, but it seems that SonarCloud isn't happy with the try-catch of the clone in terms of coverage. From checking online, it's considered anti-pattern to test this... Any suggestions?

yonatankarp avatar Apr 22 '22 11:04 yonatankarp