package AbstractFactory;
/**
* 3G接口
* @author L-Seven
*
*/
interface I3G{
void show();
}
/**
* CDMA接口
* @author L-Seven
*
*/
interface ICDMA{
void show();
}
/**
* 实现Apple3G
* @author L-Seven
*
*/
class Apple3G implements I3G{
@Override
public void show() {
System.out.println("Apple3G");
}
}
/**
* 实现Nokia3G
* @author L-Seven
*
*/
class Nokia3G implements I3G{
@Override
public void show() {
System.out.println("Nokia3G");
}
}
/**
* 实现AppleCDMA
* @author L-Seven
*
*/
class AppleCMDA implements ICDMA{
@Override
public void show() {
System.out.println("AppleCMDA");
}
}
/**
*实现 NokiaCMDA
* @author L-Seven
*
*/
class NokiaCMDA implements ICDMA{
@Override
public void show() {
System.out.println("NokiaCMDA");
}
}
/**
* 选择工厂
* @author L-Seven
*
*/
public abstract class AbstractFactory {
public abstract I3G get3G();
public abstract ICDMA getCDMA();
public static AbstractFactory chooseFactory(char f){
switch (f) {
case 'a':
return new AppleFactory();
case 'n':
return new NokiaFactory();
default:
return null;
}
}
}
/**
* Apple工厂
* @author L-Seven
*
*/
class AppleFactory extends AbstractFactory{
@Override
public I3G get3G() {
return new Apple3G();
}
@Override
public ICDMA getCDMA() {
return new AppleCMDA();
}
}
/**
* Nokia工厂
* @author L-Seven
*
*/
class NokiaFactory extends AbstractFactory{
@Override
public I3G get3G() {
return new Nokia3G();
}
@Override
public ICDMA getCDMA() {
return new NokiaCMDA();
}
}
/**
* 测试类
* @author L-Seven
*
*/
class AbstractFactoryTest{
public static void main(String[] args) {
AbstractFactory.chooseFactory('a').get3G().show();
AbstractFactory.chooseFactory('a').getCDMA().show();
AbstractFactory.chooseFactory('n').get3G().show();
AbstractFactory.chooseFactory('n').getCDMA().show();
}
/**
* 3G接口
* @author L-Seven
*
*/
interface I3G{
void show();
}
/**
* CDMA接口
* @author L-Seven
*
*/
interface ICDMA{
void show();
}
/**
* 实现Apple3G
* @author L-Seven
*
*/
class Apple3G implements I3G{
@Override
public void show() {
System.out.println("Apple3G");
}
}
/**
* 实现Nokia3G
* @author L-Seven
*
*/
class Nokia3G implements I3G{
@Override
public void show() {
System.out.println("Nokia3G");
}
}
/**
* 实现AppleCDMA
* @author L-Seven
*
*/
class AppleCMDA implements ICDMA{
@Override
public void show() {
System.out.println("AppleCMDA");
}
}
/**
*实现 NokiaCMDA
* @author L-Seven
*
*/
class NokiaCMDA implements ICDMA{
@Override
public void show() {
System.out.println("NokiaCMDA");
}
}
/**
* 选择工厂
* @author L-Seven
*
*/
public abstract class AbstractFactory {
public abstract I3G get3G();
public abstract ICDMA getCDMA();
public static AbstractFactory chooseFactory(char f){
switch (f) {
case 'a':
return new AppleFactory();
case 'n':
return new NokiaFactory();
default:
return null;
}
}
}
/**
* Apple工厂
* @author L-Seven
*
*/
class AppleFactory extends AbstractFactory{
@Override
public I3G get3G() {
return new Apple3G();
}
@Override
public ICDMA getCDMA() {
return new AppleCMDA();
}
}
/**
* Nokia工厂
* @author L-Seven
*
*/
class NokiaFactory extends AbstractFactory{
@Override
public I3G get3G() {
return new Nokia3G();
}
@Override
public ICDMA getCDMA() {
return new NokiaCMDA();
}
}
/**
* 测试类
* @author L-Seven
*
*/
class AbstractFactoryTest{
public static void main(String[] args) {
AbstractFactory.chooseFactory('a').get3G().show();
AbstractFactory.chooseFactory('a').getCDMA().show();
AbstractFactory.chooseFactory('n').get3G().show();
AbstractFactory.chooseFactory('n').getCDMA().show();
}
}
//图为输出结果