From: http://developerlife.com/tutorials/?p=21
Problem
When using interfaces, it is important NOT to access the implementation classes (which implement these interfaces) directly.Here is a simple example of what NOT to do:
1: public interface LogEntryIF{
2: public setUserId( String s );
3: public setSessionId( String s );
4: }
5:
6: public class LogEntry implements LogEntryIF{
7: private String userId, session;
8: public setUserId( String s ){ userId = s; }
9: public setSessionId( String s ){ sessionId = s; }
10: }
1: LogEntryIF le = new LogEntry();
2: le.setUserId( “boo” );
3: le.setSessionId( “sessionboo” );
- without directly accessing the implemenation classes themselves
- by relying entirely on the interfaces to access these objects.
Solution
- create an instance of an implementation classes, but return this object reference as the interface.
1: public interface LogEntryIF{
2: public setUserId( String s );
3: public setSessionId( String s );
4: }
5:
6: public class LogEntry implements LogEntryIF{
7: private String userId, session;
8: public setUserId( String s ){ userId = s; }
9: public setSessionId( String s ){ sessionId = s; }
10: }
11:
12: public class LogFactory{
13: public static LogEntryIF getLogEntryInstance(
14: String userId ,
15: String sessionId ){
16: LogEntry le = new LogEntry();
17: le.setUserId( userId );
18: le.setSessionId( sessionId );
19: return le;
20: }
21: }
1: LogEntryIF le = LogFactory.getLogEntryInstance( “boo” , “sessionboo” );
No comments:
Post a Comment