Design mode builder - purchase articles
I recently wanted to buy a computer for learning, so I went to a computer company. After analysis, the following configuration was selected:
CPU P2.4
Motherboard Intel
Hard disk 80g
. . .
Friends who bought a computer may know that after we choose, the computer will have a special assemblyist (Assembler) to load us. Computer is composed of these things (we call Part). Friends who have learned economics may know that if this group is not sold, it is not a commodity, but is just a computer.
1. Here, let's define the product (Commodity) class first:
Public class commitness {
String commitity = ""
Public Commodity (Part partc) {// consists of various parts
this. commodity = parta.part "/ n";
This. commodity = product partb.part "/ n";
THIS. commodity = Product partc.part;
System.out.println ("My Machine is configured as: / N" commodity;
}
}
2, let's define the components of the computer (Part) class:
PUBLIC CLASS PART {
String part = "";
Public Part (String Part) {
this.part = part;
}
}
3. We define the computer (Computer) as an interface class:
Public interface computer {
// Assembled parts a, such as CPU
Void BuildParta ();
// Assembly component B, such as motherboard
Void BuildPartB ();
// assembly component C, such as a hard disk
Void BuildPartc ();
// Return the final assembly result (return to the last assembled computer)
// The assembly process of the finished product is not performed here, but is done by the assembly class class.
/ / Thus realize the separation of the process and components
Commodity getcommodity ();
}
4. Define the assembly of the computer (Assembler):
Public class assembler {
Private Computer Computer;
Public assembler (Computer Computer) {// The main task is to install a computer
This.Computer = computer;
}
// Put the part parta partb partc finally form complex objects
/ / Here is the process of assembling the motherboard, CPU and hard disk into a PC
Public void construct () {
Computer.buildparta ();
Computer.buildpartb ();
Computer.buildpartc ();
}
}
5, my computer is the specific implementation of the computer (Computer) interface, so the MyComputer implementation class is again defined:
Public class mycomputer imports computer {
Part parta, partb, partc; public void buildparta () {
Parta = new part ("P42.4 CPU");
}
Public void buildpartb () {
Partb = new part ("intermarkete");
}
Public void buildpartc () {
Partc = new part ("80g hard drive");
}
Public commodity getcommodity () {
// Return the final assembly result
Commodity mycomputer = new commodity (parta, partb, partc);
Return mycomputer;
}
}
6, write test class:
Public class mycomputertest {
Public static void main (string args []) {
Mycomputer mycomputer = new mycomputer (); // Pack my computer
AskEMBLER Assembler = new assembler (mycomputer); // send a mapping
askEMBLER.CONSTRUCT (); // Makeup
Commodity commodity = mycomputer.getCommodity (); // Sell to my computer (product)
}
}
7. Description:
A: The code is only used to learn the Builder mode. If you want to run, you must do a change.
B: Separate a complex object constructed with its representation, so that the same build process can create different representations. Because everyone's computer configuration may be different.
C: We use Builer to build a complex object process and its part decoupling, that is, the process is divided as fine, and every part is only used to complete your own function (each of the duties). 8. Special gratitude: Thanks to the opinions of Levinlee users, what you said is that the debugger is used by the Product class, and later changed to the commodity class, some places have forgotten to change, and you said you The situation. I have been correcting, I hope everyone will correct it. Thanks here!