-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package Design_Pattern.原型模式; | ||
|
||
//浅复制 | ||
public class Computer implements Cloneable{ | ||
private String cpu; | ||
private String memory; | ||
private String disk; | ||
|
||
public Computer(String cpu, String memory, String disk) { | ||
this.cpu = cpu; | ||
this.memory = memory; | ||
this.disk = disk; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Computer{" + | ||
"cpu='" + cpu + '\'' + | ||
", memory='" + memory + '\'' + | ||
", disk='" + disk + '\'' + | ||
'}'; | ||
} | ||
|
||
public Object clone(){ | ||
try { | ||
return (Computer)super.clone(); | ||
} catch (CloneNotSupportedException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package Design_Pattern.原型模式; | ||
|
||
public class ComputerDetail implements Cloneable{ | ||
private String cpu; | ||
private String memory; | ||
private Disk disk; | ||
|
||
public ComputerDetail(String cpu, String memory, Disk disk) { | ||
this.cpu = cpu; | ||
this.memory = memory; | ||
this.disk = disk; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ComputerDetail{" + | ||
"cpu='" + cpu + '\'' + | ||
", memory='" + memory + '\'' + | ||
", disk=" + disk + | ||
'}'; | ||
} | ||
|
||
public Object clone(){ | ||
try { | ||
ComputerDetail computerDetail= (ComputerDetail) super.clone(); | ||
computerDetail.disk=(Disk) this.disk.clone(); | ||
return computerDetail; | ||
} catch (CloneNotSupportedException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
|
||
class Disk implements Cloneable{ | ||
private String ssd; | ||
private String hhd; | ||
|
||
public Disk(String ssd, String hhd) { | ||
this.ssd = ssd; | ||
this.hhd = hhd; | ||
} | ||
public Object clone(){ | ||
try { | ||
return (Disk)super.clone(); | ||
} catch (CloneNotSupportedException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package Design_Pattern.原型模式; | ||
|
||
public class Test { | ||
public static void main(String[] args) { | ||
Computer computer=new Computer("8core","16G","1TB"); | ||
Computer computerClone=(Computer) computer.clone(); | ||
Disk disk=new Disk("28G","2TB"); | ||
|
||
ComputerDetail computerDetail=new ComputerDetail("12core","64G",disk); | ||
System.out.println(computerDetail); | ||
|
||
ComputerDetail computerDetailClone=(ComputerDetail) computerDetail.clone(); | ||
System.out.println(computerDetailClone); | ||
} | ||
} |