oop

Can an object know from which object its method is called?

In Moritz Haarmann's Blog I found an example of usage of Bonjour by Java. Here is the code taken from there: public class ServiceAnnouncer implements IServiceAnnouncer, RegisterListener { private DNSSDRegistration serviceRecord; private boolean registered; public boolean isRegistered(){ return registered; } ...

Referring to parent class properties and methods when inside a class property object.

I want to settle this confusion in my mind once and for all! I am having trouble reaching some of my class properties and methods. Now, I know I could use a object literal and append all of my references with the objects name, e.i. animal., but I want to know how to handle this with an instantiated class. Here is a quick example of my ...

Can I use methods of a class without instantiating this class?

I have a class with several methods and there is no constructor among these methods. So, I am wondering if it is possible to call a method of a class without a creation of an instance of the class. For example, I can do something like that: NameOfClass.doMethod(x1,x2,...,xn) In general I do not see why it should be impossible. I jus...

Policy-based template design: How to access certain policies of the class?

I have a class that uses several policies that are templated. It is called Dish in the following example. I store many of these Dishes in a vector (using a pointer to simple base class), but then I'd like to extract and use them. But I don't know their exact types. Here is the code; it's a bit long, but really simple: #include <iostre...

Java Object Oriented Design Question: Returning multiple objects in java(Updated)

Hi, The below code in Java throws Null pointer exception. public class New{ int i; New(int i) { this.i = i; } public void func(New temp) { temp.i = 10; temp = new New(20); } public static void main(String[] args) { New n = null; n.func(n); System.out.println("value "+ n.i); } } The...

Most Important and frequently used design patterns

Hi I am currently reading head first design patterns book. I feel that the best way to learn design patters is to apply them. So, I wanted to know the most frequently used design patterns, so that I can experiment with them and apply them in the example programs I write. Which are the most important and useful design patterns you use ...

const vs static in PHP

IN PHP5 i can declare a const value to a class: class config { const mailserver = 'mx.google.com'; } But i can also declare public static: class config { public static $mailserver = 'mx.google.com'; } In case of a config file which i will later us, like: imap_connect(config::$mailserver ... imap_connect(config::mailserve...

How to design a class appropriate for millions of allocations?

If I want to allocate millions of objects of a class Foo, and I want to be memory- and time-efficient, how should I design the Foo class? Obviously, Foo should not contain much member data. Also, I guess, it should not use virtual functions? And how costly is it for Foo to derive from a Base class? And from several Base classes? ...

How to correctly refactor this?

I have trouble finding way to correctly refactor this code so that there would be as little duplicate code as possible, I have a couple of methods like this (pseudocode): public List<Something> parseSomething(Node n){ List<Something> somethings = new ArrayList<Something>(); initialize(); sameCodeInBothClasses(); List<Node> nodes...

How to have a policy class implement a virtual function?

I'm trying to design a policy-based class, where a certain interface is implemented by the policy itself, so the class derives from the policy, which itself is a template (I got this kind of thinking from Alexandrescu's book): #include <iostream> #include <vector> class TestInterface { public: virtual void test() = 0; }; class TestI...

Extend DOMElement object

How could I extend objects provided with Document Object Model? Seems that there is no way according to this issue. class Application_Model_XmlSchema extends DOMElement { const ELEMENT_NAME = 'schema'; /** * @var DOMElement */ private $_schema; /** * @param DOMDocument $document * @return void ...

Python: Can subclasses overload inherited methods?

I'm making a shopping cart app in Google App Engine. I have many classes that derive from a base handler: class BaseHandler(webapp.RequestHandler): def get(self, CSIN=None): self.body(CSIN) Does this mean that the body() method of every descendant class needs to have the same argument? This is cumbersome. Only one descenda...

Difference between Inheritance and Composition

Are Composition and Inheritance the same? If I want to implement the composition pattern, how can I do that in Java? ...

Script stops while waiting for user input from STDIN.gets

I'm trying to do something like this, where I have two loops going in seperate threads. The problem I am having is that in the main thread, when I use gets and the script is waiting for user input, the other thread is stopped to wait as well. class Server def initialize() @server = TCPServer.new(8080) run end ...

Is Method Overloading considered part of polymorphism?

Is Method Overloading considered part of polymorphism? Thank you very much. ...

How to check if two records have a self-referencing relation?

Consider the following schema with users and their collegues (friends): Users User: columns: user_id: name: user_id as userId type: integer(8) unsigned: 1 primary: true autoincrement: true first_name: name: first_name as firstName type: string(45) notnull: true last_name: ...

inner class within Interface

is that possible to create a inner class within an interface? If yes, why do we create like that? Anyways we are not going to create any interface objects? Do they help in any Development process? ...

Insert data in an object to a database.

I am facing the following design/implementation dilemma. I have a class Customer which is below with getters and setters. I would like to insert the value of the Customer into a "Customer" table of a database. But Customer has an address which is of type "Address". How do I go about inserting this field into the database?(I am using sqli...

C++, function pointer to member function

I'd like to set up a function pointer as a member of a class that is a pointer to another function in the same class. The reasons why I'm doing this are complicated. In this example, I would like the output to be "1" class A { public: int f(); int (*x)(); } int A::f() { return 1; } int main() { A a; a.x = a.f; printf("%d\n",a....

Factory configured by a. object's class - how to do it nicely?

In my current project we have a couple of data classes that deal with core concepts of our application domain. Now at some places in our project we have to have different behavior depending on the concrete object in hand. E.g. a JList has the task to render a list of objects but we want the rendering to be slightly different, depending ...