mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-18 11:25:50 +00:00
This commit refactors the Prototype pattern by making it Cloneable and thus inheriting the clone() method to its subclasses which removes code duplications.
This commit is contained in:
committed by
GitHub
parent
c1c863843f
commit
5397f85d7d
+10
-13
@@ -22,7 +22,7 @@ used for creating new objects from prototype instances.
|
||||
|
||||
Real-world example
|
||||
|
||||
> Remember Dolly? The sheep that was cloned! Lets not get into the details but the key point here is
|
||||
> Remember Dolly? The sheep that was cloned! Let's not get into the details but the key point here is
|
||||
> that it is all about cloning.
|
||||
|
||||
In plain words
|
||||
@@ -45,8 +45,11 @@ interface with a method for cloning objects. In this example, `Prototype` interf
|
||||
this with its `copy` method.
|
||||
|
||||
```java
|
||||
public interface Prototype {
|
||||
Object copy();
|
||||
public abstract class Prototype<T> implements Cloneable {
|
||||
@SneakyThrows
|
||||
public T copy() {
|
||||
return (T) super.clone();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -54,15 +57,13 @@ Our example contains a hierarchy of different creatures. For example, let's look
|
||||
`OrcBeast` classes.
|
||||
|
||||
```java
|
||||
@EqualsAndHashCode
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@NoArgsConstructor
|
||||
public abstract class Beast implements Prototype {
|
||||
public abstract class Beast extends Prototype<Beast> {
|
||||
|
||||
public Beast(Beast source) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract Beast copy();
|
||||
}
|
||||
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@@ -76,19 +77,15 @@ public class OrcBeast extends Beast {
|
||||
this.weapon = orcBeast.weapon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OrcBeast copy() {
|
||||
return new OrcBeast(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish wolf attacks with " + weapon;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
We don't want to go into too much details, but the full example contains also base classes `Mage`
|
||||
We don't want to go into too many details, but the full example contains also base classes `Mage`
|
||||
and `Warlord` and there are specialized implementations for those for elves in addition to orcs.
|
||||
|
||||
To take full advantage of the prototype pattern, we create `HeroFactory` and `HeroFactoryImpl`
|
||||
|
||||
Reference in New Issue
Block a user