LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-09-2017, 02:29 PM   #16
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 142Reputation: 142

Quote:
Originally Posted by sundialsvcs View Post
The whole idea behind "Getters and Setters," in my estimation, is that the language is allowing the users of your class to ignore the details of exactly how the values of any of your properties are retrieved or set. From their point of view, they merely reference the property as they usually do. Meanwhile, the language substitutes a subroutine or function call at each appropriate spot. The end-user's code is none the wiser. In fact, "they neither know nor care!"

Since an "interface" is supposed to be "your class's contract with the public," I would strongly encourage you to simply give them ... "properties." Don't tell them how it actually works, and don't require them to be party to it. ("You want a property? Hey, you got it!" And that's all you need to know. Pay no attention to the little man behind the curtain, who you can't see anyway.)

"Getters and Setters" are properly part of the "implementation." The stuff that goes on behind the curtain. The messy details that they will never have to bother their silly little heads about . . .
Exactly!

Like in my simple example, it only works for right triangles, but you can make the object more useful by expanding it to all triangles and have to use cos() or sin() or tan() or something to come up with the right value for a non-right triangle, but the code already using it will still work to return the longest side of a triangle (not really technically the hypotenuse, since that, by definition refers to a right triangle, but whatever).

Last edited by Laserbeak; 06-09-2017 at 02:35 PM.
 
Old 06-09-2017, 04:10 PM   #17
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 142Reputation: 142
I wrote a better Triangle Objective-C class, although it compiles, it's completely untested since it's just a class, it has no programatic interface to it.

But, it still might be helpful to those looking to see how these things work:

Code:
//
//  Triangle.h
//  test
//

#import <Foundation/Foundation.h>

@interface Triangle : NSObject

@property long double lengthOfSide1;
@property long double lengthOfSide2;
@property long double largestAngleInDegrees;
@property (readonly, nonatomic) long double lengthOfLongestSide;
@property (readwrite, nonatomic) long double largestAngleInRadians;

@end
Code:
//
//  Triangle.m
//  test
//

#import "Triangle.h"

@implementation Triangle

@synthesize lengthOfSide1; //Automatically create getters/setters for side1
@synthesize lengthOfSide2; //Automatically create getters/setters for side2
@synthesize largestAngleInDegrees; //Automatically create getters/setters for the angle between side1 & side2

-(long double) getLengthOfLongestSide {
    if (self.largestAngleInDegrees == (long double) 60.0) {
        return lengthOfSide1; // All sides are of equal length
    } else if (self.largestAngleInDegrees == (long double) 90.0) { // right triange
        return sqrtl([self square:lengthOfSide1] + [self square:lengthOfSide2]);
    } else {
        /*
         
         The law of cosines for calculating one side of a triangle when the angle opposite and the other two sides are known. Can be used in conjunction with the law of sines to find all sides and angles.
         
         c^2 = a^2 + b^2 - 2ab cos(C)
        
        */
        
        long double c = sqrt ([self square:lengthOfSide1] + [self square:lengthOfSide2] - 2 * lengthOfSide1 * lengthOfSide2 * cosl (self.largestAngleInRadians));
        
        return c;
    }
}

-(long double)square :(long double)x {
    return x * x;
}

-(long double) getLargestAngleInRadians {
    
    return (M_PI * largestAngleInDegrees)/180;
    
}

-(void) setLargestAngleInRadians :(long double)inRadians {
    largestAngleInDegrees = (180 * inRadians) / M_PI;
}


@end

Last edited by Laserbeak; 06-09-2017 at 07:21 PM.
 
Old 06-09-2017, 04:45 PM   #18
Myk267
Member
 
Registered: Apr 2012
Location: California
Posts: 311
Blog Entries: 14

Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
I think you misread that other thread, they were saying that an interface having only getters and setters likely indicates a design problem. Taking your Media example, doing it like this would be bad:

Code:
public interface Media {
 getCurrentTime();
 setCurrentTime();
 getPlayingState();
 setPlayingState();
 getMediaTitle();
}
If anything is weird here, it's setPlayingState. I don't know the real type signature, but I can take a pretty good guess that it's going to lead to a switch statement being implemented in each class which implements that method. Polymorphism is for decoupling what is being done from how it's done, so baking in a function that's going to be a switch statement, a construct that instead couples those together, is probably not the best choice. The rest of those don't seem like the worst things ever, but my brain is getting tired.

I think that interfaces and abstract base classes are constructs for implementing polymorphism, and they probably smell, real bad, if they're not actually supporting that goal in a given program.

Beyond that, it's probably some sort of smell to stare at a 6 line program fragment and try to derive the rest of the program, so I'd better stop that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
3 network interfaces - only works from boot with two interfaces enabled linux_driver Linux - Networking 4 11-26-2014 05:42 PM
ifup: couldn't read interfaces file "/etc/network/interfaces" dobharweim Linux - Networking 1 12-13-2013 11:20 AM
[SOLVED] /etc/network/interfaces configuration for virtual interfaces nonshatter Linux - Networking 4 10-25-2010 06:22 AM
ifup: couldn't read interfaces file "/etc/network/interfaces" debian lenny lorimer73 Linux - Networking 1 08-24-2010 03:47 PM
changing objects parameters via getters inside functions in c++? markhod Programming 1 03-08-2005 05:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration