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