二十四种设计模式:工厂方法模式(使用频率5个星,共5星)

interface Logger{
    public void WriteLog();
}

public class FileLog implements Logger{
    public void WriteLog(){
        system.out.print("file wirte");
    }
}

public class DBFileLog implements Logger{
    public void WriteLog(){
        system.out.print("db wirte");
    }
}

interface Factory{
    public Logger createLogger();
}

public class FileFactory implements Factory{
    public Logger createLogger(){
        Logger fileLogger=new FileLog();
        return fileLogger;
    }
}

public class DBFactory implements Factory{
    public Logger createLogger(){
        Logger dbLogger=new DBFileLog();
        return dbLogger;
    }
}

public class App{
    public void Main(string args){
        Logger logger;
        Factory factory;
        factory=new FileFactory();
        logger=factory.createLogger();
        logger.wirte("测试");
    }
}

 

posted on 2018-02-06 22:58  lvlv岁月流逝  阅读(123)  评论(0编辑  收藏  举报

导航