Package com. yq1012. create mode. Factory method. Abstract factory;
Public class AMD implements CPU {
Public String getCPU (){
Return "Athlon XP 2008 + ";
}
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public abstract class ComputerFactory {
CPU;
HardDisk hd;
MainBoard mb;
Public void show (){
Try {
System. out. println (this. getClass (). getName (). toString () + ("computer configuration produced "));
System. out. println ("CPU:" + cpu. getCPU ());
System. out. println ("HardDisk:" + hd. getSize ());
System. out. print ("MainBoard :");
Mb. Attach (cpu );
}
Catch (Exception e ){
System. err. println (e. getMessage ());
}
}
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public interface CPU {
Public String getCPU ();
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public class DellFactory extends ComputerFactory {
DellFactory (){
Cpu = new AMD ();
Hd = new Maxtor ();
Mb = new MSIK7N2G ();
}
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public interface HardDisk {
Public String getSize ();
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public class IBMFactory extends ComputerFactory {
IBMFactory (){
Cpu = new Intel ();
Hd = new WestDigit ();
Mb = new MSI865PE ();
}
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public class Intel implements CPU {
Public String getCPU (){
Return "Pentium 4 3.2C ";
}
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public interface MainBoard {
Public void Attach (CPU) throws Exception;
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public class Maxtor implements HardDisk {
Public String getSize (){
Return "MaXLine Plus II 200G ";
}
}
Package com. yq1012. create mode. Factory method. Abstract factory;
// Motherboard Microstar MSI865PE, supporting Intel CPU:
Public class MSI865PE implements MainBoard {
Public void Attach (CPU) throws Exception {
If (cpu. getClass (). toString (). endsWith ("Intel ")){
System. out. println ("MSI865PE ");
}
Else {
Throw new Exception ("The motherboard MSI865PE can only be equipped with Intel CPU ");
}
}
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public class MSIK7N2G implements MainBoard {
Public void Attach (CPU) throws Exception {
If (cpu. getClass (). toString (). endsWith ("AMD ")){
System. out. println ("MSIK7N2G ");
}
Else {
Throw new Exception ("The motherboard MSIK7N2G can only be configured with amd cpu ");
}
}
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public class Test {
Public static void main (String [] args ){
// Abstract Factory mode: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
// Example of a computer product:
// IBM and Dell are well-known computer manufacturers who use different boards, hard disks, and CPUs,
// However, the accessories, the motherboard, and the CPU must be compatible with each other. For example, the Microstar msik7n2 G is used with amd cpu, and the Microstar MSI865PE is used with Intel CPU.
// As shown in the figure, ComputerFactory is an abstract factory, while Dell and IBM are the factories that produce products.
// CPU, HardDisk, and MainBoard are abstract products, and there are many types of CPU. For specific implementation, see the code:
//
IBMFactory IBM = new IBMFactory ();
Ibm. show ();
DellFactory dellFactory = new DellFactory ();
DellFactory. show ();
}
// Abstract factory model: the previous factory method model is more specific. The cat must have a kitten. This is not a problem.
//, Is specific, so the abstract factory does not produce so specific, the generated object may have no common characteristics.
// For example, a goat can not only produce lambs, but also produce goat milk. However, lambs are animals and goat milk is food.
}
Package com. yq1012. create mode. Factory method. Abstract factory;
Public class WestDigit implements HardDisk {
Public String getSize (){
Return "WD2500JD 250G ";
}
}