I have the following type:
我有以下类型:
public class Vehicle { ... }
The vehicle is instantiated by a user action. Think of it as an invention game, a user can create any type of vehicle he/she wants. For each object of the vehicle, I need to log the status of the object on a regular basis. So I have the following classes:
车辆由用户动作实例化。将其视为发明游戏,用户可以创建他/她想要的任何类型的车辆。对于车辆的每个物体,我需要定期记录物体的状态。所以我有以下课程:
public abstract class Log { ... }
public class LogTyre extends Log { ... }
public class LogHandle extends Log { ... }
public class LogBrake extends Log { ... }
public class LogEngine extends Log { ... }
...etc...
An added complication is that each vehicle will have a different set of logging requirement. For example, a bike type vehicle will not require the LogEngine object.
另外一个复杂因素是每辆车都有不同的记录要求。例如,自行车型车辆不需要LogEngine对象。
So during my instantiation of the Vehicle I need to store the class types for the type of log the vehicle requires. So when it is time to perform a log, the logger will be able to instantiate the necessary logging class for that particular vehicle. How can I achieve this in Java? Or maybe just in OOP in general.
因此,在我实例化车辆时,我需要存储车辆所需的日志类型的类类型。因此,当需要执行日志时,记录器将能够为该特定车辆实例化必要的日志记录类。我怎样才能在Java中实现这一目标?或者也许只是在OOP中。
Any help will be appreciated, thank you.
任何帮助将不胜感激,谢谢。
2 个解决方案
#1
0
I'm not sure I correctly understand your problem. But, with the hint by Hovercraft Full of Eels, the following code is written. You may add additional types of logs. Hope this helpful.
我不确定我是否正确理解你的问题。但是,在Hovercraft Full of Eels的提示下,编写了以下代码。您可以添加其他类型的日志。希望这有用。
import java.io.PrintStream;
import java.util.ArrayList;
public class Logger {
public static void main(String[] args) {
// creating vehicles
Vehicle[] vs = { new Vehicle("Bike"), new Vehicle("Car") };
// adding logs of different type
vs[0].addLog(new LogTyre("Slim"));
vs[1].addLog(new LogTyre("Wide"));
vs[1].addLog(new LogEngine("Lean burn"));
// maintaining logs
Logger logger = new Logger();
for (Vehicle v: vs)
v.writeLog(logger);
// printing logs
for (Vehicle v: vs)
v.printLogs();
}
public void write(Log log) {
System.out.printf(".. writing to %s.%n", log.name());
// You may write different logs depending on the log types using common methods
}
}
class Vehicle {
String type; // vehicle type
ArrayList<Log> logs; // logs to be maintained
public Vehicle(String type) {
this.type = type;
logs = new ArrayList<Log>();
}
public void addLog(Log log) {
logs.add(log);
}
public void writeLog(Logger w) {
for (Log log: logs)
log.accept(w);
}
public void printLogs() {
for (Log log: logs) {
log.print(System.out);
}
}
}
abstract class Log {
protected String name; // common fields
// accepting the visitor
public void accept(Logger logger) {
logger.write(this);
}
// common methods for maintaining logs
public abstract String name();
public void print(PrintStream ps) {
ps.printf("[Log: %s]%n", name());
}
}
class LogTyre extends Log {
public LogTyre(String name) {
super.name = name;
}
public String name() {
return name+" Tyre Log";
}
}
class LogEngine extends Log {
public LogEngine(String name) {
super.name = name;
}
public String name() {
return name+" Engine Log";
}
}
This key of the above code is to pass the Logger to the Log using accept method. Then this accept method will invoke the appropriate write method of the logs of various types.
上面代码的这个键是使用accept方法将Logger传递给Log。然后这个accept方法将调用各种类型日志的相应write方法。
#2
0
Assuming the methods you need to access are defined by log, could you add a "log type" enumeration to your log class, so you can look up each log? Something like:
假设您需要访问的方法是由日志定义的,您是否可以在日志类中添加“日志类型”枚举,以便查找每个日志?就像是:
private void logStuff(LogType logType, Map<LogType, Log> vehicleLogs){
Log log = vehicleLogs.get(logType);
if(log != null){
log.doLogStuff();
}
}
Where LogType has values like Handle, Engine, etc.
其中LogType具有Handle,Engine等值。
#1
0
I'm not sure I correctly understand your problem. But, with the hint by Hovercraft Full of Eels, the following code is written. You may add additional types of logs. Hope this helpful.
我不确定我是否正确理解你的问题。但是,在Hovercraft Full of Eels的提示下,编写了以下代码。您可以添加其他类型的日志。希望这有用。
import java.io.PrintStream;
import java.util.ArrayList;
public class Logger {
public static void main(String[] args) {
// creating vehicles
Vehicle[] vs = { new Vehicle("Bike"), new Vehicle("Car") };
// adding logs of different type
vs[0].addLog(new LogTyre("Slim"));
vs[1].addLog(new LogTyre("Wide"));
vs[1].addLog(new LogEngine("Lean burn"));
// maintaining logs
Logger logger = new Logger();
for (Vehicle v: vs)
v.writeLog(logger);
// printing logs
for (Vehicle v: vs)
v.printLogs();
}
public void write(Log log) {
System.out.printf(".. writing to %s.%n", log.name());
// You may write different logs depending on the log types using common methods
}
}
class Vehicle {
String type; // vehicle type
ArrayList<Log> logs; // logs to be maintained
public Vehicle(String type) {
this.type = type;
logs = new ArrayList<Log>();
}
public void addLog(Log log) {
logs.add(log);
}
public void writeLog(Logger w) {
for (Log log: logs)
log.accept(w);
}
public void printLogs() {
for (Log log: logs) {
log.print(System.out);
}
}
}
abstract class Log {
protected String name; // common fields
// accepting the visitor
public void accept(Logger logger) {
logger.write(this);
}
// common methods for maintaining logs
public abstract String name();
public void print(PrintStream ps) {
ps.printf("[Log: %s]%n", name());
}
}
class LogTyre extends Log {
public LogTyre(String name) {
super.name = name;
}
public String name() {
return name+" Tyre Log";
}
}
class LogEngine extends Log {
public LogEngine(String name) {
super.name = name;
}
public String name() {
return name+" Engine Log";
}
}
This key of the above code is to pass the Logger to the Log using accept method. Then this accept method will invoke the appropriate write method of the logs of various types.
上面代码的这个键是使用accept方法将Logger传递给Log。然后这个accept方法将调用各种类型日志的相应write方法。
#2
0
Assuming the methods you need to access are defined by log, could you add a "log type" enumeration to your log class, so you can look up each log? Something like:
假设您需要访问的方法是由日志定义的,您是否可以在日志类中添加“日志类型”枚举,以便查找每个日志?就像是:
private void logStuff(LogType logType, Map<LogType, Log> vehicleLogs){
Log log = vehicleLogs.get(logType);
if(log != null){
log.doLogStuff();
}
}
Where LogType has values like Handle, Engine, etc.
其中LogType具有Handle,Engine等值。