Design Patterns-& lt; abstract factory patterns & gt;

Source: Internet
Author: User

Package abstract factory;

Public class AbstractFactory {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// I want an IBM computer Nic
AnableFactory. createMac (new IBMMac (), "computer ");
// I want a network card for an IBM mobile phone
AnableFactory. createMac (new IBMMac (), "Mobile Phone ");
// I want a CPU for an IBM Computer
AnableFactory. createCup (new IBMCup (), "computer ");
// I need a CPU for an IBM mobile phone
AnableFactory. createCup (new IBMCup (), "Mobile Phone ");
}


/**
* Abstract Factory focuses on specific transactions
* Two interfaces are provided this time.
* Create Cup and Mac respectively
*/
Private static abstract class AnableFactory {
Public AnableFactory ()
{
System. out. println (AbstractFactory. class. getCanonicalName () + "start working ");
}
Public static void createCup (CupFactory cupFactory, String type ){
If (type. equals ("computer ")){
CupFactory. createComputerCup ();
}
Else {
CupFactory. createMobleCup ();
}
}
Public static void createMac (MacFactory cacFactory, String type)
{
If (type. equals ("computer ")){
CacFactory. createComputerMac ();
}
Else {
CacFactory. createMobleMac ();
}
}
}
/*
* CUP Zone
*/
Private static abstract class CupFactory extends AnableFactory {
Void createComputerCup (){};
Void createMobleCup (){};
}
Public static class IBMCup extends CupFactory {

Public void createComputerCup (){
// TODO Auto-generated method stub
System. out. println ("IBM create Computer Cup ");
}

Public void createMobleCup (){
// TODO Auto-generated method stub
System. out. println ("IBM create Moble Cup ");
}

}
Public static class LevenvoCup extends CupFactory {

Public void createComputerCup (){
// TODO Auto-generated method stub
System. out. println ("Levenvo create Moble Cup ");
}

Public void createMobleCup (){
// TODO Auto-generated method stub
System. out. println ("Levenvo create Moble Cup ");
}

}

Private static abstract class MacFactory extends AnableFactory {
Void createComputerMac (){};
Void createMobleMac (){};
}
Public static class IBMMac extends MacFactory {

Public void createComputerMac (){
// TODO Auto-generated method stub
System. out. println ("IBM create Computer Mac ");
}

Public void createMobleMac (){
// TODO Auto-generated method stub
System. out. println ("IBM create Moble Mac ");
}

}
Public static class LevenvoMac extends MacFactory {

Public void createComputerMac (){
// TODO Auto-generated method stub
System. out. println ("Levenvo create Computer Mac ");
}

Public void createMobleMac (){
// TODO Auto-generated method stub
System. out. println ("Levenvo create Moble Mac ");
}

}
}

 

This article is from the "feisha" blog

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.