从汽车组装说起:
创建汽车接口--
package com.moshi.abstractfactory;
public interface car {
public void build();
}
创建车胎接口:
package com.moshi.abstractfactory;
public interface tyre {
public void build();
}
创建车轮接口:
package com.moshi.abstractfactory;
public interface whell {
public void build();
}
分别创建实现类:
汽车实现---
package com.moshi.abstractfactory;
public class car1 implements car{
@Override
public void build() {
// TODO Auto-generated method stub
System.out.println("汽车1组装开始...");
}
}
//车轮组装实现
package com.moshi.abstractfactory;
public class car1tyre implements tyre{
@Override
public void build() {
// TODO Auto-generated method stub
System.out.println("组装汽车1车轮完毕!");
}
}
//车胎组装实现
package com.moshi.abstractfactory;
public class car1whell implements whell{
@Override
public void build() {
// TODO Auto-generated method stub
System.out.println("组装汽车1轮胎完毕!");
}
}
//建立抽象工厂。。。
package com.moshi.abstractfactory;
public interface factory {
public whell createwhell();
public car createcar();
public tyre createtyre();
}
//抽象工厂实现
package com.moshi.abstractfactory;
public class carfactory implements factory{
@Override
public car createcar() {
// TODO Auto-generated method stub
return new car1();
}
@Override
public tyre createtyre() {
// TODO Auto-generated method stub
return new car1tyre();
}
@Override
public whell createwhell() {
// TODO Auto-generated method stub
return new car1whell();
}
}
测试组件---:
package com.moshi.abstractfactory;
public class testclient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
factory fac=new carfactory();
car car1=fac.createcar();
car1.build();//1
tyre tyre1=fac.createtyre();
tyre1.build();//2
whell whell1=fac.createwhell();
whell1.build();//3
}
}
;;;本着面向接口面向抽象的编程思想,学习23种设计模式是很有必要的!