易学设计模式八 工厂方法(Factory Method)
工厂方法模式的用意是定义一个创建产品对象的工厂,将实际的创建工作推迟到子类中。
修改上节中简单工厂模式
抽象工厂
public interface FruitGardener {public Fruit factory();}public class AppleGardener implements FruitGardener {public Fruit factory() {return new Apple();}}public class GrapeGardener implements FruitGardener {public Fruit factory() {return new Grape();}}public class StrawberryGardener implements FruitGardener {public Fruit factory() {return new Strawberry();}}public class Client {public static void main(String[] args) {FruitGardener ag = new AppleGardener();Fruit apple = ag.factory();apple.plant();apple.grow();apple.harvest();FruitGardener gg = new GrapeGardener();Fruit grape = gg.factory();grape.plant();}}public class URLConnectionReader {public static void main(String[] args) throws Exception {URL hao360 = new URL("http://hao.360.cn/");URLConnection hc = hao360.openConnection();BufferedReader in = new BufferedReader(new InputStreamReader(hc.getInputStream()));String inputLine;while((inputLine = in.readLine()) != null) {System.out.println(inputLine);}in.close();}}