|
yep. Give Room a public CloseAllWindows() method and let it keep the windows to itself.
(I substituted windows for doors, to keep Graham happy).
|
|
|
|
|
It's correct a Rooms will share a door in my example, but it was just ment as an example
So let's continue and consider a window class instead. What happends if I have alot of member data in my Window class should I still use the class 'Window' as a private member inside 'Room' and make "wrapper" Methods and events inside Room like: Room.CleanWindow(int index), Room.OnWindowCleaned(), Room.OpenWindow(int index). etc etc. Sort of chaining methods and events.
This seems to duplicate Methods and Eventsin a way, thoughts about this ?
|
|
|
|
|
One advantage of using "wrapper" methods is that you can use them to do things that the Window class doesn't know about:
class Room {
public:
void OpenWindow(int index)
{
try {
plants[index].Remove();
windows[index].Open();
plants[index].Replace();
} catch(DroppedPlantPotException& dppe) {
CleanTheFloor();
}
}
private:
std::vector<Window> windows;
std::vector<PlantPot> plants;
};
|
|
|
|
|
To model the real world it seems to me as if you should have a WindowCleaner class which has a public method CleanWindows(House) and which should fire events. Maybe WindowCleaner inherits from WindowOpener or a class Person who is a WindowOpener? The room is pretty inanimate, it's the people who do things - unless this is a very high tech house.
|
|
|
|
|
There are various patterns that you could apply here, but the one that immediately springs to mind here is that you could use a Mediator here. Depending on the implementation of the Mediator, what you might want to do is:
public class Door
{
public Door()
{
}
public void Open()
{
_isOpen = true;
Mediator.Instance.NotifyColleagues(DoorState.Open);
}
public void Close()
{
_isOpen = false;
Mediator.Instance.NotifyColleagues(DoorState.Closed);
}
}
public class House
{
private int _ambientTemperature = 22;
public House()
{
Mediator.Instance.Register((object o) => { _ambientTemperature -= 2; }, DoorState.Open);
Mediator.Instance.Register((object o) => { _ambientTemperature += 2; }, DoorState.Closed);
}
} In this example, House is decoupled from Door; it registers an interest in the DoorState messages and reacts accordingly without any events having to tunnel up. It's the responsibility of the Mediator to route the message to the appropriate targets, so everything becomes nice and decoupled - more importantly, this is easily testable. If you want a sample Mediator, this is based around the one I use here[^]."WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanx for the responses.
The classes "House", "Room", "Door" was just an example, I'm not looking for designs to solve the specific design of a House which have a CleanWindow method.
I will try to rephrase my question.
The question is about nested class architecute with composition/aggregation, it could be anything that tries to model the real world, but in this specific question it's not important what it is, just that composition/aggregation is uesd, see below for examples of nested class structures, maybe these aren't the best examples but there must be valid relationsships like this in reel world:
Computer/Cpu/Processor
Database/Table/Column
The question is should I use, which sort breaks the law of demeter:
Computer.Motherboard.Cpu.Temperature();
Computer.Motherboard.Cpu.IsOverheated();
Or should I make "Motherboard" and "Cpu" private and wrap/chain method calls and events thru the nested structure:
Computer.CpuTemperature()
Computer.CpuIsOverheated()
I hope the question is clearer this time.
|
|
|
|
|
Based on this[^] your first example pair fails due to "too many dots".
|
|
|
|
|
Ok, So it will be a benefit to make nested classes private and create extra code for wrapper methods and events in the main class for this sort of architecture instead of make a direct connection to the Cpu object?, will this even be true if I referenced Motherboard and Cpu as interfaces ?
|
|
|
|
|
DiiJAY wrote: Ok, So it will be a benefit to make nested classes private and create extra code for wrapper methods and events in the main class for this sort of architecture instead of make a direct connection to the Cpu object?
Yes. Using Computer.Motherboard.Cpu.IsOverheated() means the calling code has to know about not only Computer class but also the the Motherboard and Cpu classes as well. Using just Computer.CpuIsOverheated() means the caller doesn't know about, or care, how the temperature is obtained internally.
As an example of the benefit, if you upgraded your PC with a motherboard that had 4 parallel CPUs the call to Computer.CpuIsOverheated() will still work from the caller's perspective. Internally the new motherboard code might average out the temperature of the 4 CPUs but this internal change is hidden from the caller. With this upgrade the Computer.Motherboard.Cpu.IsOverheated(); call runs into problems: Which of the 4 CPUs does this call refer to?
DiiJAY wrote: will this even be true if I referenced Motherboard and Cpu as interfaces ?
Interfaces I use to refer to a set of related methods, if you wanted the temperature of, say, a Car, Room and a PC an ITemperature interface with a ITemperature.GetTemp() method would be handy but you would still want to hide your Motherboard and Cpu classes for the reasons already mentioned.
|
|
|
|
|
Thank you Jonathan, that made the picture clear!
|
|
|
|
|
|
All very true and all good advice. However, the danger is that your Computer interface is going to become very bloated. Consider that a computer contains not just a motherboard and one or more CPUs, but also hard drives, a USB bus (plus any USB devices which may or may not be connected), a floppy drive (if your computer is as old as mine), a graphics card, a network card, a sound card, RAM, etc. etc. etc. (I'm sure you can think of a few more components.)
Exposing every possible operation for every possible component through the Computer class interface leads to a very unwieldy design. The danger is that your Computer class becomes a sort of "god" object which tries to do everything and be all things to all men.
There are no hard and fast rules for object design. There is a certain amount of give and take to end up with the best solution, balancing one consideration against another.
|
|
|
|
|
I agree. In general I'm all for an architecture designed someone who recognises there can be rules there in the first place. Investigating what the Law of demeter is (I knew the principle but not the actual rule) and why it's used, and why in other cases it's perhaps not used, can only be a good thing.modified on Tuesday, March 9, 2010 5:41 AM
|
|
|
|
|
I'm a C#.Net programmer and what I like to do is separate classes that only hold data and those that have functionality.
I like that architecture because it's clear cut and I can immediately find any data or method and don't have to think long about it or search around where some method is.
I think it's a good approach for multitier architecture, so when I'd change the database I wouldn't have to change all classes but only the ones that handle db connection.
On the other hand I'm not quite sure if that's really object oriented programming.
For example I need to read some data about hotels out of a db, edit it and write it back. I'd have a hotel class and a dbconnection class.
The hotel class only holds data like rooms, prices, etc. without any methods (except for a deep copy method I add to all my classes) and dbconnection class has all the methods.
I can't really see the advantage of adding the methods for connecting to the db to the hotel class, but that would be actual oop, while I'm doing something close to procedural programming or?
|
|
|
|
|
Megidolaon wrote: I can't really see the advantage of adding the methods for connecting to the db to the hotel class, but that would be actual oop, while I'm doing something close to procedural programming or?
What you have done is keep a clear separation of concerns, and that's good practice (as well as being good OOP)."WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Have you considered a HotelManager class? The hotel has things such as rooms, but its the HotelManager that actually uses your data to make money out of the hotel building. Setting, say, a price per room or having rooms cleaned by a cleaning company is definitly something the HotelManager rather than the 'Hotel' does. What we consider 'hotel' in reality is actually a combination of the HotelBuilding and the HotelManaqer. Taking this view, it's the HotelManager who should interact with the your data, rather than the hotel.
|
|
|
|
|
This is an interview question I faced a couple of days back.
The person asked me to draw fully the System Architecture of my project, diagaram of how the three layers (since I told him that my project is three layer architecture)are communicating each other etc?
Even though I draw something, he replied that didn't clearly depict the architecture.
I have the following questions:
1) When we say System Architecture diagram, it shouldn't contain any functionaltiy? Or does it mean that how the system flows from top to bottom?
2) Is this represented through UML or some other ways?
3) How to draw the communication between the three layers?
Any help in this regard please? or any good site for the articles?
Thanks
meeram395Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
|
|
|
|
|
Not really a fair question since your definition of the diagram might be different from his yet no less right or wrong.
Systems architecture[^]
Read the overview. I'm sure there will be many conflicting opinions over this but, for me, if you can clearly depict and understand the various layers and interactions of the application that should be fine (unless the job was for an architect!).
also: Software architecture[^].
Whilst Wiki articles should not be taken as the final authority they can be quit ehelpful.Tychotics: take us back to the moon
"Life, for ever dying to be born afresh, for ever young and eager, will presently stand upon this earth as upon a footstool, and stretch out its realm amidst the stars."
H. G. Wells
|
|
|
|
|
I have installed Visual Studio 2008 Express edition in my home pc. I want to download and use the Application blocks for .Net.
When I tried to download Enterprise Library 4.1 2008, the system requirement says that the system should have Standard Edition or Professional Edition or TeamEdition or Team Suite. Does it mean it will not work for Express Edition?
In that case, is there any possible way to use the Application blocks in Express Editions? Please help.
ThanksSuccess is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
|
|
|
|
|
meeram395 wrote: In that case, is there any possible way to use the Application blocks in Express Editions?
No; since the Express editions are free, they have reduced functionality. If you want to use these libraries then you will need to buy one of the commercial editions.txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
How can I design a software Internet Firewall?Can you give one structure chart of Internet firewall and its explanation on working?
|
|
|
|
|
This question is impossible to answer in a technical forum. Try searching Google for some documentation on TCP/IP and firewalls; you will find plenty to keep you occupied for a few weeks. txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
I'm a college student and as part of my work I have to make a game in XNA (does not include life cycle)
I want to do this well and have researched software life cycles and such but many only apply to multiple developers or are too big i.e. dive into publishing and marketing which I don't need.
Is there anyone who can share some life cycles so that I can have a structured development?
Thanks!
|
|
|
|
|
|
There are lot of free UML tools available. I need any single one, which should be good (possible to draw all sort of diagrams and manipulate them), just for learning purpose.
Can anyone please recommend which one should I choose? Can I download the same and install?
Please suggest.
Thanks
meeram395. Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
|
|
|
|