473,666 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OOP Question

RSH
I was reading a tutorial on OOP principals and design and I stumbled on
something that I don't understand. The tutorial used a Oven class. In that
class there was a private field called temperature.

class Oven {
private int _temperature;

public int Temperature {
get {
return _temperature;
}
set {
_temperature = value;
}
}
}

This is the implementation I would normally employ. BUT... The article
says...
"The type Oven exposes its temperature as a property, which is a direct
reference to the variable _temperature. This means that the internal oven
temperature implementation is tied directly to its external interface. The
point of good object-oriented programming is to avoid this sort of
programming."

That seems strange to me...what if I have to expose temperature? What if my
cookie object needs to preheat the oven to 350 degrees dont I need to
monitor the temperature of the oven object?

Damn just when I started to think I was getting OOP!

Thanks,

Ron
Dec 7 '07 #1
16 1593
On Dec 7, 1:32 pm, "RSH" <way_beyond_o.. .@yahoo.comwrot e:
I was reading a tutorial on OOP principals and design and I stumbled on
something that I don't understand. The tutorial used a Oven class. In that
class there was a private field called temperature.
<snip>
This is the implementation I would normally employ. BUT... The article
says...
"The type Oven exposes its temperature as a property, which is a direct
reference to the variable _temperature. This means that the internal oven
temperature implementation is tied directly to its external interface. The
point of good object-oriented programming is to avoid this sort of
programming."
That sounds like a bad book to me. The implementation will always be
tied to the interface - the point is to avoid the *reverse* being
true, i.e. exposing the implementation itself as the interface.

I've certainly heard people saying that properties should almost
always be avoided etc, but it's not a particularly commonly-held view.

Jon
Dec 7 '07 #2
Ron,

I am curious, what does the article recommend is the workaround? I
think that what it is trying to say is that you would have a Temperature
object which you would expose which would expose all the details of working
with temperature.

Of course, eventually, one will reach the point of diminishing returns,
meaning that you could only extend this so far before it becomes impossible
to work with (expose an object for temperature, which exposes an object for
degrees, etc, etc).

Oh, and the cookie wouldn't change the temperature, the Baker instance
would. The Baker instance would add the Cookie to the Sheet, and then add
the Sheet to the Oven. Then the Bake method would be called on the Oven,
which would change the Baked property of the individual Cookie instances on
the sheet over time.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"RSH" <wa************ *@yahoo.comwrot e in message
news:e9******** ******@TK2MSFTN GP02.phx.gbl...
>I was reading a tutorial on OOP principals and design and I stumbled on
something that I don't understand. The tutorial used a Oven class. In
that class there was a private field called temperature.

class Oven {
private int _temperature;

public int Temperature {
get {
return _temperature;
}
set {
_temperature = value;
}
}
}

This is the implementation I would normally employ. BUT... The article
says...
"The type Oven exposes its temperature as a property, which is a direct
reference to the variable _temperature. This means that the internal oven
temperature implementation is tied directly to its external interface. The
point of good object-oriented programming is to avoid this sort of
programming."

That seems strange to me...what if I have to expose temperature? What if
my cookie object needs to preheat the oven to 350 degrees dont I need to
monitor the temperature of the oven object?

Damn just when I started to think I was getting OOP!

Thanks,

Ron

Dec 7 '07 #3
RSH
Nicholas and Jon,

I appreciate both of your input...I highly respect the both of you and have
learned alot from you both.

This is the solution that they suggest:

delegate void OnTemperature( int temperature);

class Oven {
private int _temperature;
OnTemperature _listeners;

public void BroadcastTemper ature() {
_listeners( _temperature);
}
public void AddTemperatureL istener( OnTemperature listener) {
_listeners += listener;
}
}
class Controller {
public Controller( Oven oven) {
oven.AddTempera tureListener(
new OnTemperature( this.OnTemperat ure));
}
public void OnTemperature( int temperature) {
Console.WriteLi ne( "Temperatur e (" + temperature + ")");
}
}http://en.csharp-online.net/CSharp_Coding_S olutions%E2%80% 94Dont_Expose_a _Class_Internal _StateRon"Nicho las
Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in message
news:10******** *************** ***********@mic rosoft.com...
Ron,

I am curious, what does the article recommend is the workaround? I
think that what it is trying to say is that you would have a Temperature
object which you would expose which would expose all the details of
working with temperature.

Of course, eventually, one will reach the point of diminishing returns,
meaning that you could only extend this so far before it becomes
impossible to work with (expose an object for temperature, which exposes
an object for degrees, etc, etc).

Oh, and the cookie wouldn't change the temperature, the Baker instance
would. The Baker instance would add the Cookie to the Sheet, and then add
the Sheet to the Oven. Then the Bake method would be called on the Oven,
which would change the Baked property of the individual Cookie instances
on the sheet over time.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"RSH" <wa************ *@yahoo.comwrot e in message
news:e9******** ******@TK2MSFTN GP02.phx.gbl...
>>I was reading a tutorial on OOP principals and design and I stumbled on
something that I don't understand. The tutorial used a Oven class. In
that class there was a private field called temperature.

class Oven {
private int _temperature;

public int Temperature {
get {
return _temperature;
}
set {
_temperature = value;
}
}
}

This is the implementation I would normally employ. BUT... The article
says...
"The type Oven exposes its temperature as a property, which is a direct
reference to the variable _temperature. This means that the internal oven
temperature implementation is tied directly to its external interface.
The point of good object-oriented programming is to avoid this sort of
programming. "

That seems strange to me...what if I have to expose temperature? What if
my cookie object needs to preheat the oven to 350 degrees dont I need to
monitor the temperature of the oven object?

Damn just when I started to think I was getting OOP!

Thanks,

Ron


Dec 7 '07 #4
On Dec 7, 2:12 pm, "RSH" <way_beyond_o.. .@yahoo.comwrot e:
I appreciate both of your input...I highly respect the both of you and have
learned alot from you both.

This is the solution that they suggest:
<snip>

So basically an event model with no direct "read on demand" other than
telling *all* listeners the temperature, and also with no control over
the temperature.

That kind of thing will be appropriate *sometimes*, but far from
always - in particular, I can't see why one client should be able to
make the oven notify all the other listeners of the temperature,
whether or not it's changed (the most obvious reason for wanting to
know).

Jon
Dec 7 '07 #5
RSH
Jon,

Thank you...I thought I was losing it! I agree the Observer pattern is
without question a powerful pattern and has its place...but this seemed to
me like someone was overcomplicatin g design just to match an acedemic
interpretation, and not looking at the maintainability of actual coding. i
mean it seems to me that if you have a complex series of objects
communicating like this it would be almost impossible to debug and unit
test.

Ron
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:cb******** *************** ***********@w40 g2000hsb.google groups.com...
On Dec 7, 2:12 pm, "RSH" <way_beyond_o.. .@yahoo.comwrot e:
>I appreciate both of your input...I highly respect the both of you and
have
learned alot from you both.

This is the solution that they suggest:

<snip>

So basically an event model with no direct "read on demand" other than
telling *all* listeners the temperature, and also with no control over
the temperature.

That kind of thing will be appropriate *sometimes*, but far from
always - in particular, I can't see why one client should be able to
make the oven notify all the other listeners of the temperature,
whether or not it's changed (the most obvious reason for wanting to
know).

Jon

Dec 7 '07 #6


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:10******** *************** ***********@mic rosoft.com...
Ron,

I am curious, what does the article recommend is the workaround? I
think that what it is trying to say is that you would have a Temperature
object which you would expose which would expose all the details of
working with temperature.

Of course, eventually, one will reach the point of diminishing returns,
meaning that you could only extend this so far before it becomes
impossible to work with (expose an object for temperature, which exposes
an object for degrees, etc, etc).

Oh, and the cookie wouldn't change the temperature, the Baker instance
would. The Baker instance would add the Cookie to the Sheet, and then add
the Sheet to the Oven. Then the Bake method would be called on the Oven,
which would change the Baked property of the individual Cookie instances
on the sheet over time.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"RSH" <wa************ *@yahoo.comwrot e in message
news:e9******** ******@TK2MSFTN GP02.phx.gbl...
>>I was reading a tutorial on OOP principals and design and I stumbled on
something that I don't understand. The tutorial used a Oven class. In
that class there was a private field called temperature.

class Oven {
private int _temperature;

public int Temperature {
get {
return _temperature;
}
set {
_temperature = value;
}
}
}

This is the implementation I would normally employ. BUT... The article
says...
"The type Oven exposes its temperature as a property, which is a direct
reference to the variable _temperature. This means that the internal oven
temperature implementation is tied directly to its external interface.
The point of good object-oriented programming is to avoid this sort of
programming. "

That seems strange to me...what if I have to expose temperature? What if
my cookie object needs to preheat the oven to 350 degrees dont I need to
monitor the temperature of the oven object?

Damn just when I started to think I was getting OOP!

Thanks,

Ron

In my case, where I AM THE BAKER, the BAKED property is an enumeration.

Values of BAKED:

Uncooked,
Undercooked,
Burned,
DUDEYourHouseIs OnFire
Temperature property is similar:

Off,
LukeWarm,
TemperatureOfTh eSun,
HotterThanTheSu n,
DUDEYourHouseIs OnFire

:)

Mythran
Dec 7 '07 #7
Hmmm... Get and set the _desired_ temperature seems to be an attribute
of the oven. When I want to know the actual temperature of our oven I
need to use a thermometer. I suppose some ovens have a built in
thermometer, in which case the oven class could contain a thermometer
with a get only property. I suppose the oven could beep when the preset
temperature is reached, so there could be an event that fires when the
desired temperature is reached.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '07 #8
On Fri, 07 Dec 2007 15:27:54 -0800, Jeff Louie <an*******@devd ex.com>
wrote:
>Hmmm... Get and set the _desired_ temperature seems to be an attribute
of the oven. When I want to know the actual temperature of our oven I
need to use a thermometer. I suppose some ovens have a built in
thermometer, in which case the oven class could contain a thermometer
with a get only property. I suppose the oven could beep when the preset
temperature is reached, so there could be an event that fires when the
desired temperature is reached.
That is similar to my oven. I can set the temperature through one
control and read the temperature using a different control. Both
controls are public members of some oven-control type. All objects of
type oven should have a controller member. This indirection is likely
what the original quote in question meant.

"The type Oven exposes its temperature as a property, which is a
direct reference to the variable _temperature. This means that the
internal oven temperature implementation is tied directly to its
external interface. The point of good object-oriented programming is
to avoid this sort of programming."

regards
A.G.
Dec 8 '07 #9
AG... Without access to the tutorial I really don't know what the author
meant:)

Off the top of my head, I don't see any problem with the Oven class
having a get temperature property. The use of a get Temperature as a
_property_ still involves indirection. For instance, say get is in
degrees F. In the future, the oven could be redesigned using an internal
hidden "thermomete r" which "reports and stores" in degrees C without
affecting the public view which could still return in degrees F.

As I see it, the use of events instead of get only is a different design
decision: get/polling vs push/events. An "event",
OnTemperatureCh angedOneDegreeF :) would be useful to avoid polling.

FWIW, some have argued against the use of the term "Broadcaste rs" or
"Broadcast" with events and listeners. I certainly used that naming
convention in the past, but Broadcast in IP is used to notify all nodes
and the term Broadcaster fell out of favor for events with a limited
domain of listeners.

Regards,
Jeff
>That is similar to my oven. I can set the temperature through one
control and read the temperature using a different control. Both
controls are public members of some oven-control type. All objects of
type oven should have a controller member. This indirection is likely
what the original quote in question meant.
<

*** Sent via Developersdex http://www.developersdex.com ***
Dec 8 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3096
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
5029
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
2653
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask it differently. FOUR QUESTIONS: The background: I got three (3) files
3
3076
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
10
3418
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a database or continue to process on to the next page. I am now trying to learn ASP to see if we can replace some of our applications that were written in php with an ASP alternative. However, after doing many searches on google and reading a couple...
10
3711
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server Error in '/QuickStartv20' Application. -------------------------------------------------------------------------------- Configuration Error Description: An error occurred during the processing of a configuration file
53
4058
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
4752
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
4272
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from less to more for example). I have a question object that has two properties that contain the collections Options and Ratings. now I want this kind of layout: --- Rating1 Rating2 Rating3 Option 1 () () ...
3
2547
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div class="not-required-question"><p>Question Text</p><input /></div>
0
8443
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.