Results 1 to 4 of 4
  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Explanation of Joshua Bloch`s example needed!

    Hello,
    I`ve started to read J.Bloch Effective Java. His first practical example, which I`ve tested and make it work is interesting but I have a question - how is that to avoid constructors? Please take a look at his code:

    J.Bloach example:
    Java Code:
    import java.util.Map;
    import java.util.HashMap; //the example is with ConcurrentHashMap
    
    
    interface Service {
    	// some methods
    	public void tell();
    	}
    
    interface Provider {
    	
    	public Service newService();
    	
    	}
    	
    public class ServiceProvider {
    
    	private ServiceProvider() { /* dont instant it */ }
    	
    	private static final Map<String, Provider> providers =
    		new HashMap<String, Provider>();
    	
    	public static final String DEFAULT_NAME = "<def>";
    	
    	public static void registerDefaultProvider(Provider p) {
    			registerProvider(DEFAULT_NAME, p);
    		}
    	
    	public static void registerProvider(String name, Provider p) {
    			providers.put(name, p);
    		}
    	
    	public static Service newInstance() {
    		
    			return newInstance(DEFAULT_NAME);
    			}
    	
    	public static Service newInstance(String name) {
    	
    			Provider p = providers.get(name);
    			if ( p == null ) 
    				throw new IllegalArgumentException("No provider registered with name: "+name);
    			return p.newService();
    			}
    	}
    And my practical usage of the ServiceProvider class
    Java Code:
    public class TestingServices {
    
    	
    		public static void main(String[] args) {
    		
    			ServiceProvider.registerProvider("Yell", new Provider() {
    					@Override 
    					public Service newService() {
    						return new Service() {
    							@Override
    							public void tell() {
    								System.out.println("I AM TELLING");
    								}
    							};
    					}
    				});
    			ServiceProvider.registerProvider("Shout", new Provider() {
    				@Override
    				public Service newService() {
    					return new Service() {
    						@Override
    						public void tell() {
    							for (int i=0; i < 100; i++) {
    								System.out.format("I am shouting you %d times!\n", i+1);
    								}
    							}
    						};
    					}
    				});
    				
    			ServiceProvider.newInstance("Yell").tell();			
    			ServiceProvider.newInstance("Shout").tell();
    			ServiceProvider.newInstance("Tell").tell();
    					
    		}
    	
    	}
    So - did I got his approach wrong or this is the way of using it. But You can`t actually avoid the constructor... And is it really better to has a HashMap to hold referencies by Strings? Really I need some enlightenment.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Explanation of Joshua Bloch`s example needed!

    There is no way to tell you whether something is "better" or not unless we understand your context. What exactly are you attempting to achieve? If you're just trying to explore whether things are possible or not, then mission accomplished, but there isn't really a "better" without knowing what your criteria for better or worse is.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Explanation of Joshua Bloch`s example needed!

    He provided several reasons for doing this in the book. And it doesn't negate the need for constructors. It negates the need for publicly visible constructors.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Explanation of Joshua Bloch`s example needed!

    Thanks, I`ve used it in my program. Seems versatile.

Similar Threads

  1. Java RegEx explanation needed
    By heatblazer in forum New To Java
    Replies: 1
    Last Post: 12-03-2012, 07:12 AM
  2. Can't understand what Joshua Bloch is talking about...
    By Jackson86 in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-20-2010, 02:29 PM
  3. need explanation
    By marie in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 03:35 PM
  4. printf() method explanation needed
    By darek9576 in forum New To Java
    Replies: 1
    Last Post: 03-14-2010, 03:54 AM
  5. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-13-2007, 11:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •