Skip to content

Prototype pattern re-factor #584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
panksy2k opened this issue May 29, 2017 · 10 comments · Fixed by #1970
Closed

Prototype pattern re-factor #584

panksy2k opened this issue May 29, 2017 · 10 comments · Fixed by #1970

Comments

@panksy2k
Copy link

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

@ghost
Copy link

ghost commented Jun 4, 2017

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.

@jwheeler31
Copy link

@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.

LiHao-MS pushed a commit to LiHao-MS/java-design-patterns that referenced this issue May 31, 2020
Pull request description
It.s seems the equal() method in the abstract class isn't designed properly and I make some change in these abstract classes and their subclasses.
for issue: iluwatar#584
@iluwatar
Copy link
Owner

iluwatar commented Jul 6, 2020

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

@ohbus
Copy link
Contributor

ohbus commented Jan 6, 2021

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

@ohbus
Copy link
Contributor

ohbus commented May 8, 2021

Sure thing @barathgdkrish.

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

@joshivrush
Copy link

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

@ghost
Copy link

ghost commented Sep 5, 2021

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.

@ThexXTURBOXx
Copy link

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

@iluwatar
Copy link
Owner

iluwatar commented Oct 5, 2021

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

yonatankarp added a commit to yonatankarp/java-design-patterns that referenced this issue Mar 2, 2022
yonatankarp added a commit to yonatankarp/java-design-patterns that referenced this issue Mar 2, 2022
…watar#584)

This commit  refactors the Prototype pattern by making it Cloneable and thus inheriting the clone() method to its subclasses which removes code duplications.
yonatankarp added a commit to yonatankarp/java-design-patterns that referenced this issue Apr 19, 2022
…watar#584)

This commit  refactors the Prototype pattern by making it Cloneable and thus inheriting the clone() method to its subclasses which removes code duplications.
yonatankarp added a commit to yonatankarp/java-design-patterns that referenced this issue Apr 20, 2022
…watar#584)

This commit  refactors the Prototype pattern by making it Cloneable and thus inheriting the clone() method to its subclasses which removes code duplications.
yonatankarp added a commit to yonatankarp/java-design-patterns that referenced this issue Apr 20, 2022
…watar#584)

This commit  refactors the Prototype pattern by making it Cloneable and thus inheriting the clone() method to its subclasses which removes code duplications.
yonatankarp added a commit to yonatankarp/java-design-patterns that referenced this issue Apr 20, 2022
…watar#584)

This commit  refactors the Prototype pattern by making it Cloneable and thus inheriting the clone() method to its subclasses which removes code duplications.
@yonatankarp
Copy link
Contributor

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?

iluwatar pushed a commit that referenced this issue Sep 8, 2022
Closes #584) (#1970)

This commit  refactors the Prototype pattern by making it Cloneable and thus inheriting the clone() method to its subclasses which removes code duplications.
@iluwatar iluwatar added this to the 1.26.0 milestone Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment