1 工厂方法模式,可以理解为:为了得到一辆汽车。可以让汽车代理商使用不同品牌的汽车生产公司,然后通过不同品牌的汽车生产公司来从中得到一辆汽车。以达到目的。
如:
public abstract class Animal {
}
-------------------------------------------------------
public class Dog extends Animal{
}
--------------------------------------------------------
public class Cat extends Animal{
}
--------------------------------------------------------
public interface FactoryInterface {
public Animal getObject();
}
--------------------------------------------------------
public class DogFactory implements FactoryInterface{
@Override
public Animal getObject() {
// TODO Auto-generated method stub
return new Dog();
}
}
--------------------------------------------------------
public class CatFactory implements FactoryInterface{
@Override
public Animal getObject() {
// TODO Auto-generated method stub
return new Cat();
}
}
--------------------------------------------------------
public class Test {
FactoryInterface factoryInterface=new DogFactory();
Animal animal=factoryInterface.getObject();
}
一个不错的java设计模式地址:http://java.chinaitlab.com/special/patterns/Index.html
基于java理解的23中设计模式地址:http://blog.csdn.net/chmask/article/details/2631485
如:
public abstract class Animal {
}
-------------------------------------------------------
public class Dog extends Animal{
}
--------------------------------------------------------
public class Cat extends Animal{
}
--------------------------------------------------------
public interface FactoryInterface {
public Animal getObject();
}
--------------------------------------------------------
public class DogFactory implements FactoryInterface{
@Override
public Animal getObject() {
// TODO Auto-generated method stub
return new Dog();
}
}
--------------------------------------------------------
public class CatFactory implements FactoryInterface{
@Override
public Animal getObject() {
// TODO Auto-generated method stub
return new Cat();
}
}
--------------------------------------------------------
public class Test {
FactoryInterface factoryInterface=new DogFactory();
Animal animal=factoryInterface.getObject();
}
一个不错的java设计模式地址:http://java.chinaitlab.com/special/patterns/Index.html
基于java理解的23中设计模式地址:http://blog.csdn.net/chmask/article/details/2631485