×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Best OO implementation?

Best OO implementation?

Best OO implementation?

(OP)
Hi dear prgrammers,
I would like to know which of the following Implementations of an enxemple-Class is the best one:

Solution 1:

CODE --> matlab

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 --> matlab

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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close