欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

抽象工厂设计模式

程序员文章站 2022-03-09 20:33:20
...

 

 

/**
 * 园丁
 */
public interface Gardener {
}

 

/**
 *  北方园丁
 */
public class NorthernGardener implements Gardener {

    public Fruit createFruit(String name) {
        return new NorthernFruit(name);//生产北方水果
    }

    public Veggie createVeggie(String name) {
        return new NorthernVeggie(name);//生产北方蔬菜
    }
}

 

/**
 * 南方园丁
 */
public class TropicalGardener implements Gardener {

    public Fruit createFruit(String name) {
        return new TropicalFruit(name);//生产南方水果
    }

    public Veggie createVeggie(String name) {
        return new TropicalVeggie(name );//生产南方蔬菜
    }
}

 

/**
 * 水果
 */
public interface Fruit {
    
}

 

/**
 * 北方水果
 */
public class NorthernFruit implements Fruit {

    private String name;
    public NorthernFruit(String name){
        
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 

/**
 * 南方水果
 */
public class TropicalFruit implements Fruit {

    public TropicalFruit(String name){
        
    }
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 

/**
 * 蔬菜
 */
public interface Veggie {

}

 

/**
 * 北方蔬菜
 */
public class NorthernVeggie implements Veggie {
    
    private String name;
    public NorthernVeggie(String name){
        
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 

/**
 * 南方蔬菜
 */
public class TropicalVeggie implements Veggie {

    public TropicalVeggie(String name){
        
    }
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

   

相关标签: 设计模式