473,804 Members | 3,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a class to handle the output of another class

Hello everybody!

I'm having a little problem with the following: I wrote a class to
collect data (Let's call it DataCollector). Now I want the data to be
interpreted in various ways. My idea was to write an interface
(iVisualizer) so DataCollector could use every user defined
(MyVisualizer) class implementing it. Saying "MyVisualiz er extends
DataCollector" is not an option, since the data should only be
collected once and then then being "rendered" by a variety of output
classes (to XML, to plain text, to PDF, etc.).

Here is the code example:

<php>
class DataCollector {
private $data

public function collectData ($someText) {
// Write data here
}

public function output($visuali zer) {
$visualizer->output($this )
}
}

class MyVisualizer implements iVisualizer {
public function output ($dataCollector )
{
echo $dataCollector->data;
}
}

$a = new DataCollector() ;
$b = new MyVisualizer();
$a->collectData("f oo");
$a->output($b);
</php>

Now, obviously calling "$a->output($b);" will get me in trouble, since
DataCollector:: data is private. Making DataCollector:: data public
would make it easy, but also "unsafe". Adding get & set methods will
add lots of methods, since $data is just an example for a large number
of properties.

I really hope to get some good ideas here. Thanks in advance!

Oli

Nov 10 '07 #1
2 1631
Oli Thissen wrote:
Hello everybody!

I'm having a little problem with the following: I wrote a class to
collect data (Let's call it DataCollector). Now I want the data to be
interpreted in various ways. My idea was to write an interface
(iVisualizer) so DataCollector could use every user defined
(MyVisualizer) class implementing it. Saying "MyVisualiz er extends
DataCollector" is not an option, since the data should only be
collected once and then then being "rendered" by a variety of output
classes (to XML, to plain text, to PDF, etc.).

Here is the code example:

<php>
class DataCollector {
private $data

public function collectData ($someText) {
// Write data here
}

public function output($visuali zer) {
$visualizer->output($this )
}
}

class MyVisualizer implements iVisualizer {
public function output ($dataCollector )
{
echo $dataCollector->data;
}
}

$a = new DataCollector() ;
$b = new MyVisualizer();
$a->collectData("f oo");
$a->output($b);
</php>

Now, obviously calling "$a->output($b);" will get me in trouble, since
DataCollector:: data is private. Making DataCollector:: data public
would make it easy, but also "unsafe". Adding get & set methods will
add lots of methods, since $data is just an example for a large number
of properties.

I really hope to get some good ideas here. Thanks in advance!

Oli

First of all, a Visualizer object is not a "type of" DataCollector, so
trying to derive Visualizer from DataCollector would be incorrect.

You have the data items are private, as they should be (encapsulation) .
So you need getter and setter methods for each one. The getter
methods return copies, and the setter methods validate the data before
actually storing it.

Yes, sometimes it means a bunch of setters and getter methods. But it's
also the correct way to do it.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Nov 10 '07 #2
On Nov 10, 3:18 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
Oli Thissen wrote:
Hello everybody!
I'm having a little problem with the following: I wrote a class to
collect data (Let's call it DataCollector). Now I want the data to be
interpreted in various ways. My idea was to write an interface
(iVisualizer) so DataCollector could use every user defined
(MyVisualizer) class implementing it. Saying "MyVisualiz er extends
DataCollector" is not an option, since the data should only be
collected once and then then being "rendered" by a variety of output
classes (to XML, to plain text, to PDF, etc.).
Here is the code example:
<php>
class DataCollector {
private $data
public function collectData ($someText) {
// Write data here
}
public function output($visuali zer) {
$visualizer->output($this )
}
}
class MyVisualizer implements iVisualizer {
public function output ($dataCollector )
{
echo $dataCollector->data;
}
}
$a = new DataCollector() ;
$b = new MyVisualizer();
$a->collectData("f oo");
$a->output($b);
</php>
Now, obviously calling "$a->output($b);" will get me in trouble, since
DataCollector:: data is private. Making DataCollector:: data public
would make it easy, but also "unsafe". Adding get & set methods will
add lots of methods, since $data is just an example for a large number
of properties.
I really hope to get some good ideas here. Thanks in advance!
Oli

First of all, a Visualizer object is not a "type of" DataCollector, so
trying to derive Visualizer from DataCollector would be incorrect.

You have the data items are private, as they should be (encapsulation) .
So you need getter and setter methods for each one. The getter
methods return copies, and the setter methods validate the data before
actually storing it.

Yes, sometimes it means a bunch of setters and getter methods. But it's
also the correct way to do it.
Everything Jerry said is correct. I would add that, if you have a lot
of private members (i.e. columns from a table in a database) you can
save yourself some work by taking advantage of the __get() and __set()
magic methods:

<http://www.php.net/manual/en/language.oop5.m agic.php>

That way you don't need to write individual getters and setters for
every piece of data stored in your object.

Nov 11 '07 #3

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

Similar topics

121
10195
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
11
6607
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
0
3484
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems that I can set the comm state however when I try and read from the port using ReadFile method I cannot, I've tried to change the DCB object flag types but this does not make a difference as I still cannot read from the port. I have pasted my code...
0
3945
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
12
2216
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control server. It "serves" back the required control (either WebControl or UserControl) based on the contents of an xml file. The code in the webform places each control in a TableCell. My problem is that the control server works as far as returning the...
4
1329
by: Miro | last post by:
Vb2003, im still learning vb.net but I do not understand my output from this logic. If someone can help me out here. Cor Ligthert, you I believe were on the right track of what Im trying to create. -Thank you very much for leading me on to Classes. Ok here is my code. ( see below )
2
4410
by: lewisms | last post by:
Hello all, I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it (before I incorporate them in another program). I just can’t seem to get a non-static call to work in my thread that has access to the Form1 variables and controls I need. I can call a non-static function using another class but then I can seem to get in...
4
4311
by: Donos | last post by:
Hi I have a HANDLE to an Event, like this.. HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL); This is running in one thread in one class. For example we will call that class as "Class A" Now i want to use this HANDLE in another thread in another class to
0
288
by: Oli Thissen | last post by:
Hello everybody! I'm having a little problem with the following: I wrote a class to collect data (Let's call it DataCollector). Now I want the data to be interpreted in various ways. My idea was to write an interface (iVisualizer) so DataCollector could use every user defined (MyVisualizer) class implementing it. Saying "MyVisualizer extends DataCollector" is not an option, since the data should only be collected once and then then...
0
9705
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
9576
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
10323
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...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
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...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
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();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.