Example of abstract Factory pattern in java design pattern

Source: Internet
Author: User


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 ";
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.