1.抽象工厂方法模式:为了创建一组或相互依赖的对象提供一个接口,而不需要指定它们是具体类……无语
2.找个代码看看:
抽象工厂类<成员为接口>
/** * 抽象工厂 * */public abstract class Factory { public abstract A CreateA(); public abstract B CreateB();}
接口A
public interface A { void aa();}
接口B
public interface B { void bb();}
A的实例
public class One_A implements A { @Override public void aa() { System.out.println("***:One_A"); }}
public class Two_A implements A { @Override public void aa() { System.out.println("***:Two_A"); }}
B的实例
public class One_B implements B { @Override public void bb() { System.out.println("***:One_B"); }}
public class Two_B implements B { @Override public void bb() { System.out.println("***:Two_B"); }}
Factory实例
public class A_Factory extends Factory { @Override public A CreateA() { return new One_A(); } @Override public B CreateB() { return new One_B(); }}
public class B_Factory extends Factory { @Override public A CreateA() { return new One_A(); } @Override public B CreateB() { return new Two_B(); }}
实现:
Factory factory=new A_Factory(); factory.CreateA().aa(); factory.CreateB().bb(); Factory factory1=new B_Factory(); factory1.CreateA().aa(); factory1.CreateB().bb();
断网了……………………