实例类图:
实例代码:
package logbag;
public interface Log {
public void writeLog();
}
package logbag;
public interface LogFactory {
public Log createLog();
}
public class DatabaseLog implements Log{
publicvoid writeLog(){
System.out.println("写数据日志");
}
}
package logbag;
public class DatabaseLogFactory implementsLogFactory{
publicLog createLog(){
System.out.println("创建数据文件");
returnnew DatabaseLog();
}
}
package logbag;
public class FileLog implements Log {
publicvoid writeLog(){
System.out.println("写日志");
}
}
package logbag;
public class FileLogFactory implementsLogFactory {
@Override
public Log createLog(){
System.out.println("创建新的日志文件");
return new FileLog();
}
}
public class LogTest {
public static void main(String[]args) {
try{
Log log;
LogFactory factory;
factory = newFileLogFactory();
log=factory.createLog();
log.writeLog();
factory = newDatabaseLogFactory();
log=factory.createLog();
log.writeLog();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}