473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 "MyVisualizer 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($visualizer) {
$visualizer->output($this)
}
}

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

$a = new DataCollector();
$b = new MyVisualizer();
$a->collectData("foo");
$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 1604
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 "MyVisualizer 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($visualizer) {
$visualizer->output($this)
}
}

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

$a = new DataCollector();
$b = new MyVisualizer();
$a->collectData("foo");
$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*******@attglobal.net
==================

Nov 10 '07 #2
On Nov 10, 3:18 pm, Jerry Stuckle <jstuck...@attglobal.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 "MyVisualizer 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($visualizer) {
$visualizer->output($this)
}
}
class MyVisualizer implements iVisualizer {
public function output ($dataCollector)
{
echo $dataCollector->data;
}
}
$a = new DataCollector();
$b = new MyVisualizer();
$a->collectData("foo");
$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.magic.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
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...
11
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...
0
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...
0
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....
12
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...
4
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...
2
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...
4
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"...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...

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.