There is a lot of discussion on this site about why singletons should be avoided but are there any cases where a singleton makes sense and is the best option?
An example where I think that a singleton is the best option is when implementing an Identity Map where you need to keep track of all models that have already been loaded from the...
Considering the simplistic scenario:
function Base() {}
function Child() {}
Child.prototype = new Base;
I wonder why would an instance of Child have constructor property set to Base and not to Child?
...
Hello!
I am designing a graphical application for which I've decided to write my own menu. I would like this menu to be platform independent. For the time being, my menu will mostly consist of a number of buttons. My issue involves the handling of events when a button is clicked. My dilemma is with a button "knowing" about the conte...
Is it possible to make a public method private in a subclass ? I don't want other classes extending this one to be able to call some of the methods . Here is an example :
class A:
def __init__(self):
#do something here
def method(self):
#some code here
class B(A):
def __init__(self):
A.__init__(self...
If I break my Objects down to 'Single Responsibilities', is there a fundamental thought whether like objects should live together or separately, for example if I have
class Employee_DataProvider() : IEmployee_DataProvider { ... };
class Employee_Details() : IEmployee_Details { ... };
class Employee_Payroll() : IPayroll() { ... };
class ...
Fragile base class is one of the most common point that gets popped up in every discussion where reusability through implementation inheritance is discussed.
Has anyone faced any real issue with these apart from the common square, rectangle examples.
Everytime I need to explain this to someone I am stuck with some real world cases wher...
When I design classes and have to choose between inheritance and composition, I usually use the rule of thumb: if the relationship is "is-a" - use inheritance, and if the relationship is "has-a" - use composition.
Is it always right?
Thank you.
...
Any code-analysis or "reverse-engineering" tool that can do either of these?:
Calculate which classes are sub-classes of which classes
Calculate which classes instantiate which classes -- (like VS Class Designer)
Calculate which functions call which functions -- (much like a Call Stack)
...
I've done a little bit of object oriented stuff before, mainly in C++, and have just started programming in C#. I'd like to do a small OOP test project to just get my head round it again and find out how to do OOP stuff in C#, but I can't think of any ideas for a relatively simple first project.
I'd like something that includes things l...
I'm making a simple test object-oriented program to try and help me get to grips with OOP. I've got an ICommand inteface which has the method Run(params object[] parameters) and I have various classes that use that interface and implement their own versions of run. Each class also has a name property which says what the command is and wh...
The responses I've got to this question have solved the problem I had in that question, but I'm wondering whether it's possible to extend it a bit. For example, if I were to have third parties contributing commands to this system, would there be a way of extending the first answer to my previous question to allow it to load all the comma...
We have two PHP5 objects and would like to merge the content of one into the second. There are no notion of subclasses between them so the solutions described in the following topic cannot apply.
How do you copy a PHP object into a different object type
//We have this:
$objectA->a;
$objectA->b;
$objectB->c;
$objectB->d;
//We want the ...
Hi everyone,
I am new to programming so i figured i'd get help from those that know it.
I am currently writing a Registration Application which will basically take a users input,
validates the data entered, displays a review screen (which the user must print out and mail in a copy), and then save the info entered to a database.
Here ...
This came up in a conversation I was having online, and it occured to me that I have no idea how this is supposed to work: Quite a lot of programmers seem to just take as a given- indeed, obvious that classes are a necessary language feature for managing huge software projects.
It's not obvious to me how they do this.
My question to yo...
The following code in javascript gives me the error "this.callback is not a function
function ajaxRequest()
{
var httpObject;
this.open = open;
this.callback = function(){};
function getHTTPObject()
{
if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRe...
I have an interface called ICommand from which various classes inherit. These classes all do various different things with the Run() method, but they all need to have a Name property and a Description property that specifies what they do.
The question is, how should I let each subclass specify those properties (Name and Description). At...
An app has a Foo class that is a concrete class with all business logic and behaviour properly defined.
That said, one need to get the collection of all the Foos that are persisted at this app.
Where should this "IEnumerable GetFoos()" method be placed?
Not at the Foo class itself, right?
...
Let's say I have the following class structure:
class Car;
class FooCar : public Car;
class BarCar : public Car;
class Engine;
class FooEngine : public Engine;
class BarEngine : public Engine;
Let's also give a Car a handle to its Engine. A FooCar will be created with a FooEngine* and a BarCar will be created with a BarEngine*. Is ...
module Superpower
# instance method
def turn_invisible
...
end
# module method
def Superpower.turn_into_toad
...
end
module Fly
def flap_wings
...
end
end
end
Class Superman
include Superpower
...
def run_away
# how to call flap_wings?
# how to call tur...
I've got a polymorphic array of objects which implement two (informal) interfaces. I want to be able to differentiate them with reflection along the lines of:
if (hasattr(obj, 'some_method')) {
# `some_method` is only implemented by one interface.
# Now I can use the appropriate dispatch semantics.
} else {
# This must be th...