Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best OO implementation?

Status
Not open for further replies.

azop51

Automotive
Dec 7, 2014
1
Hi dear prgrammers,
I would like to know which of the following Implementations of an enxemple-Class is the best one:

Solution 1:
Code:
  class Library < handle
  
  	properties ( Private ) % Here Private !
  
  		address = '';
  
  		books = struct('name', {}, 'author', {});
  
  	end
  
  	methods
  
  		function obj = Library()
  		end
  
  		function address = GetAddress(obj)
  			address = obj.address;
  		end
  
  		function SetAddress(obj, address)
  			% check address ist string
  			obj.address = address;
  		end
  
  		function AddBooks(obj, names, authors)
  			% check params (cell arrays of strings)
  			obj.books = [obj.books, struct('name', names, 'author', authors)];
  		end
  
  		function SetBooks(obj, names, authors)
  			% check params (cell arrays of strings)
  			obj.books = [];
  			obj.AddBooks(names, authors);
  		end
  
  		function books = GetBooksField(obj, index, field)
  			% (check field in {'name', 'author'})
  			books = {obj.books(index).(field)};
  		end
  
  		% Eventuell weitere Get.. und Set... für books
  
  	end
  
  end
  
  
  % Use:
  schoolLib = Library();
  schoolLib.SetAddress('Mond, 3er Stock');
  schoolLib.SetBooks({'a', 'b', 'c'}, {'d', 'e', 'f'});
  FirstBookName = schoolLib.GetBooksField(1, 'name');

Code:
Solution 2:

  class Library < handle
  
  	properties ( Public ) % Here Public !
  
  		address = '';
  
  		books = struct('name', {}, 'author', {});
  
  	end
  
  	methods
  
  		function obj = Library()
  		end
  
  		function set.address(obj, address)
  			% check address is string
  			obj.address = address;
  		end
  
  		function set.books(obj, books)
  			% check books is struct array with string fields 'name', 'author'
  			obj.books = books;
  		end
  
  		function AddBooks(obj, names, authors)
  			% check params (cell arrays of strings)
  			obj.books = [obj.books, struct('name', names, 'author', authors)];
  		end
  
  		% Eventuell weitere Get.. und Set... für books
  
  	end
  
  end
  
  
  % Use:
  schoolLib = Library();
  schoolLib.address = 'Mond, 3er Stock';
  schoolLib.books = struct('name', {'a', 'b', 'c'}, 'author', {'d', 'e', 'f'});
  FirstBookName = schoolLib.books(1).name;
  schoolLib.books(1).name = 'new name';

Solution 3: make another class for Book... I would like to avoid it for such a simple data.
What are the pros and cons (Parameters Public and Private)? Why? Is there a solution in between?
Is it important or just a matter of taste? Especially for 'books' (Struct Array) is this question relevant. can we have a Struct Array Parameter Public and unse the native getter & setter on it?

Thand you for sharing your opinion!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor