其他分享
首页 > 其他分享> > 工厂模式

工厂模式

作者:互联网

public abstract class Product() {
    public void methodA() {
        // do something
    }
    // for subclass
    public abstract void methodB();
    
}

public class ConcreteProduct1 extends Product() {
    public void methodB(){
        // do something
    }
}

public class ConcreteProduct2 extends Product() {
    public void methodB(){
        // do something
    }
}

public abstract class Creator{
    public abstract <T extends Product> T  createProduct(Class <T> c);
}

public class ConcreteCreator extends Creator {
    public <T extends Product> T createProduct(Class<T> c){
        Product product = null;
        try {
            product = (Product) Class.forName(c.getName()).newInstance();
        } catch (Exception e) {
            //TODO: handle exception
        }
        return (T) product;
    }
}

 

标签:Product,abstract,模式,public,void,something,工厂,class
来源: https://www.cnblogs.com/zgh1949/p/15613149.html