抽象工厂模式【ABSTRACT FACTORY PATTERN】 a

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Vector;

 public abstract class Factory
{
    public static Factory getFactory(String classname)
    {
        Factory factory = null;
        try
        {
            factory = (Factory)Class.forName(classname).newInstance();
        }
        catch (ClassNotFoundException e)
        {
            System.err.println(classname + e.getMessage());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return factory;
    }
    
    public abstract Link createLink(String caption, String url);
    
    public abstract Tray createTray(String caption);
    
    public abstract Page createPage(String title, String author);
}

 public abstract class Item
{
    protected String caption;
    
    public Item(String caption)
    {
        this.caption = caption;
    }
    
    public abstract String makeHTML();
}

  public abstract class Link extends Item
{
    protected String url;
    
    public Link(String caption, String url)
    {
        super(caption);
        this.url = url;
    }
}


  public abstract class Page {
    protected String title;
    protected String author;
    protected Vector content = new Vector();
    public Page(String title, String author) {
        this.title = title;
        this.author = author;
    }
    public void add(Item item) {
        content.add(item);
    }
    public void output() {
        try {
            String filename = title + ".html";
            Writer writer = new FileWriter(filename);
            writer.write(this.makeHTML());
            writer.close();
            System.out.println(filename + "  createm over");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public abstract String makeHTML();
}


  public abstract class Tray extends Item {
    protected Vector tray = new Vector();
    public Tray(String caption) {
        super(caption);
    }
    public void add(Item item) {
        tray.add(item);
    }
}

你可能感兴趣的:(java,html)