class-design

When should you use friend classes? [duplicate]

最后都变了- 提交于 2019-12-03 13:36:13
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When should you use 'friend' in C++? I have come to a stumbling block because of lack of documentation on friend classes. Most books just explain it briefly, e.g an excerpt from C++: the Complete Reference : Friend Classes are seldom used. They are supported to allow certain special case situations to be handled. And frankly, I have never seen a friend class in any good code made by an experienced C++ programmer

Performance of Singleton Class Instance Method vs. Static Class Method in PHP?

笑着哭i 提交于 2019-12-03 13:25:24
问题 I'm interested in objective analysis of which is more performant; calling instance methods of a singleton class or methods of a static class. I've already seen this so I'm not looking for a discussion about the difference between the two or a discussion of which is "better." I'm only interested in relative performance between the two. Thanks in advance. -Mike 回答1: Check this chart :) grabbed from this article 回答2: Unless you're calling them in a tight loop (meaning no other significant code,

Methods in Object-Oriented Design

余生长醉 提交于 2019-12-03 12:41:04
问题 Q1. In my university studies of object-oriented modelling and design they recommend thinking about what an object can do for its method, and what its responsibilities are for its attributes. All attempts at clarification have resulted in further confusion. This tends to generate a class diagram with actors who have all the actions, and inner classes which only hold data. This doesn't seem correct. Is there another way of thinking about how to model the objects? Q2. Also, the course seems to

Object oriented programming - class design confusion

风流意气都作罢 提交于 2019-12-03 12:06:33
问题 I am trying to wrap my head around object oriented programming. My understanding is that we have objects so we can design our programs to mirror real-life objects. Let's take a class hierarchy: class Fruit { void Eat() { } } class Apple extends Fruit { } Obviously, you can use Fruit polymorphically if Eat() is virtual. But does this make sense? Fruit cannot eat itself! Should a fruit object rather be passed to a human object which has a Eat() function? I am trying to figure out the correct

Is there a rule of thumb for when to code a static method vs an instance method?

北慕城南 提交于 2019-12-03 09:56:10
问题 I'm learning Java (and OOP) and although it might irrelevant for where I'm at right now, I was wondering if SO could share some common pitfalls or good design practices. 回答1: One important thing to remember is that static methods cannot be overridden by a subclass. References to a static method in your code essentially tie it to that implementation. When using instance methods, behavior can be varied based on the type of the instance. You can take advantage of polymorphism. Static methods are

What is the logic behind having a mutable and immutable versions of classes like NSArray, NSDictionary etc in Objective C?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 09:50:47
Why do common collection classes in Objective C like NSString, NSArray, NSDictionary etc have a mutable as well as an immutable version. What is the logic behind defining them separately? Performance, memory management or anything else? The immutable versions of the classes exist because an immutable object is, in and of itself, a unique identifier for a particular state . I.e. if you have an NSArray of 100 NSString instances, that NSArray instance can be treated as idempotent for any one of those strings. As well, the immutability means that a change cannot happen after the state has been

How should I split large and bloated classes into smaller ones?

匆匆过客 提交于 2019-12-03 09:42:33
I have a large 'Manager' class which I think is doing too much but I am unsure on how to divide it into more logical units. Generally speaking the class basically consists of the following methods: class FooBarManager { GetFooEntities(); AddFooEntity(..); UpdateFooEntity(..); SubmitFooEntity(..); GetFooTypes(); GetBarEntities(); } The Manager class is part of my business logic and constains an instance of another "Manager" class on the data access level which contains all CRUD operations for all entities. I have different entities coming from the data access layer and therefore have a

new types may not be defined in a return type - C++

五迷三道 提交于 2019-12-03 08:04:08
问题 I am confused I think on C++ class structure. I have a .h called FxMathFunctions.h and a .cpp called FxMathFunctions.cpp the .h starts like: class FxMathFunctions { public: FxMathFunctions(); ~FxMathFunctions(); and in the .cpp I have: #include "FxBasicTypes.h" #include "FxMathFunctions.h" FxMathFunctions::FxMathFunctions() {} FxMathFunctions::~FxMathFunctions() {} I am getting errors like: error: new types may not be defined in a return type error: return type specification for constructor

Java: Return class (Not an instance)

烂漫一生 提交于 2019-12-03 06:11:55
Is it possible to return in a static method a class? I will explain... I have: public class A { public static void blah(){} } public class B { } I want to create a static method in B witch returns A . So you can do: A.blah(); And B.getA().blah(); This, without creating an instance of A . Just use it static methods. Is this possible? This is a rebuttal of @irreputable's answer: public class B { public static A getA(){ return null; } } B.getA().blah(); //works! It "works", but probably not in the sense that you expect, and certainly not in a useful way. Let's break this down into two parts: A a

why is java.lang.Throwable a class?

社会主义新天地 提交于 2019-12-03 04:08:26
问题 In java adjectives ending in -able are interfaces Serializable , Comparable etc... So why is Throwable a class? Wouldn't exception handling be easier if Throwable were an interface? (Edit: e.g. Exception classes don't need to extend Exception/RuntimeException.) Obviously, changing it now is out the question. But could it be made abstract? Wouldn't that avoid the bad practice of throw new Throwable() ; 回答1: So why is Throwable a class? I can think of two reasons: Exceptions have state. In