Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: With the Builder template, where are private internal variables initiated?

  1. #1
    dazz is offline Member
    Join Date
    Apr 2017
    Posts
    27
    Rep Power
    0

    Default With the Builder template, where are private internal variables initiated?

    Hi
    Below is some example code of the builder template. I have modified it by adding a string array of image file names plus an ArrayList of the actual images of the named files.
    So I want to provide the file name of a file, then have the constructor go and get the file and load it into the ArrayList.
    The reason I want to do this is to allow the whole record to be serialized.

    I expect this means the record object will hold both the image file name (public) and the array of actual images (private).

    The ArrayList is not a parameter that will ever be directly visible to the user. If I parse it to the Builder constructor like the other parameters, it will be accessible.
    I am thinking that it should be located in the constructor. Pseudo code below:

    Java Code:
    		public Computer build(){
    			c = new Computer(this);  //build a new computer which includes the image file name as a public parameter
                            ArrayList<Image> imgList = new ArrayList<Image>;  \\initiate the new ArrayList to hold the images, always private
                            ...
                            get the file c.imageFilename[i]   //get the filename from the c object.
                            do a lot of file io error checking here 
                            and get the image file 
                            loop for i ( imgList[i].add(imageFile[i]) )  ; //add the image(s) to the Image ArrayList
                            return c; //return the modified record
    		}
    Is this the right approach??

    I will need a getImage(i) getter but the user won't ever need to see the ArrayList directly.


    Java Code:
    package com.journaldev.design.builder;
    
    public class Computer {
    	
    	//required parameters
    	private String HDD;
    	private String RAM;
            private String[] ImageFiles;
            private ArrayList<Image>  Images;
    
    	
    	//optional parameters
    	private boolean isGraphicsCardEnabled;
    	private boolean isBluetoothEnabled;
    	
    
    	public String getHDD() {
    		return HDD;
    	}
    
    	public String getRAM() {
    		return RAM;
    	}
    
    	public boolean isGraphicsCardEnabled() {
    		return isGraphicsCardEnabled;
    	}
    
    	public boolean isBluetoothEnabled() {
    		return isBluetoothEnabled;
    	}
    
            public Image getImage(int index);
                    \\ do some error/range checking here
                    return images[index];
            {
    	
    	private Computer(ComputerBuilder builder) {
    		this.HDD=builder.HDD;
    		this.RAM=builder.RAM;
    		this.isGraphicsCardEnabled=builder.isGraphicsCardEnabled;
    		this.isBluetoothEnabled=builder.isBluetoothEnabled;
    	}
    	
    	//Builder Class
    	public static class ComputerBuilder{
    
    		// required parameters
    		private String HDD;
    		private String RAM;
    
    		// optional parameters
    		private boolean isGraphicsCardEnabled;
    		private boolean isBluetoothEnabled;
    		
    		public ComputerBuilder(String hdd, String ram){
    			this.HDD=hdd;
    			this.RAM=ram;
    		}
    
    		public ComputerBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) {
    			this.isGraphicsCardEnabled = isGraphicsCardEnabled;
    			return this;
    		}
    
    		public ComputerBuilder setBluetoothEnabled(boolean isBluetoothEnabled) {
    			this.isBluetoothEnabled = isBluetoothEnabled;
    			return this;
    		}
    		
    		public Computer build(){
    			return new Computer(this);
    		}
    
    	}
    
    }
    Last edited by dazz; 05-13-2017 at 11:12 AM.

  2. #2
    dazz is offline Member
    Join Date
    Apr 2017
    Posts
    27
    Rep Power
    0

    Default Re: With the Builder template, where are private internal variables initiated?

    Hi
    I figured this out. This code snippet shows mandatory variables and args that the user sees. They include filenames but not file objects.
    I wrote a function getImage(servImageFilename) that takes a file name and returns an image object.
    For debugging/testing purposes, that image is displayed as an icon in a window.

    The image object cannot be directly set by the user. In my code, the user sets the filename (the path is defined in a properties file) and the constructor loads it in.

    This demonstrates what can be done within the constructor builder.

    Java Code:
        public static class FlagDescriptorBuilder {
            //### RE-DECLARE VARIABLES
            private static final String FILEPATH = getFlagImagesDir();  
            // Mandatory Variables
            private static String flagImageFilename; 
            private static String[] flagImageFilenames = new String[10];
            private static String   servImageFilename;        
    
            //Private Variables loaded by the Constructors
            private static Image  servFlagImage;
            private static ArrayList<Image> flagImages;     
            
           
            //### CONSTRUCTORS (3x)
            // include mandatory variables within constructor args.
            // The general case with multiple images to display
            public FlagDescriptorBuilder(String[] flagImageFilenames,
                                        String servImageFilename){
                this.flagImageFilenames = null;
                this.flagImageFilenames = Arrays.copyOf(flagImageFilenames,
                                                        flagImageFilenames.length);
                this.servImageFilename = servImageFilename;
    
                this.servFlagImage = getImage(servImageFilename);
                drawIcon(this.servFlagImage);
                
            }

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,172
    Rep Power
    12

    Default Re: With the Builder template, where are private internal variables initiated?

    I would use the name of the class rather than "this" for referencing your static fields. I would also use a list implementation to
    hold your flagImageFilenames. This prevents overflowing the array or iterating over empty array elements.

    Regards,
    Jim
    jepoys1 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2017, 12:05 AM
  2. Replies: 16
    Last Post: 01-08-2014, 06:02 PM
  3. Why private variables
    By PhQ in forum New To Java
    Replies: 6
    Last Post: 06-07-2012, 09:11 PM
  4. Why make class variables private?
    By PrinceSendai in forum New To Java
    Replies: 3
    Last Post: 10-18-2010, 11:01 AM
  5. Inheritance - accessing private variables
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 01:21 AM

Posting Permissions

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