class-design

Design(How-to) of classes containing collections of other classes

三世轮回 提交于 2019-12-04 07:04:51
How to design classes involving collections of other classes? General Example: A Workspace contains number of Projects . A Project contains large number of Resources . Each Resource may contain large number of Files . So here the classes identified can be Workspace,Project,Resource and File. Workspace will have list of Project.Project will have list of Resources and Resource will have list of Files. Of course each class has its related settings. Now the basic questions are : a) Who creates and adds a class to a particular collection? Another class or the class containing the collection? b)

.NET ORMs, immutable value objects, structs, default constructors, and readonly properties

别来无恙 提交于 2019-12-04 05:11:53
I am just getting started with .NET ORMs, to the point where I haven't even decided between Entity Framework and NHibernate. But in both cases, I'm running into a problem in that they seem to want me to compromise the integrity of my domain model in various ways, especially on finer points of C# object design. This is one of several questions on the subject. I am very used to enforcing immutability on appropriate properties with a pattern that looks like this: public class Foo { private readonly string bar; public string Bar { return this.bar; } public Foo(string bar) { this.bar = bar; } }

Large Inner classes and private variables

隐身守侯 提交于 2019-12-04 03:29:06
One thing I've run into a few times is a service class (like a JBoss service) that has gotten overly large due to helper inner classes. I've yet to find a good way to break the class out. These helpers are usually threads. Here's an example: /** Asset service keeps track of the metadata about assets that live on other * systems. Complications include the fact the assets have a lifecycle and their * physical representation lives on other systems that have to be polled to find * out if the Asset is still there. */ public class AssetService { //...various private variables //...various methods

Changing the Type of a inherited property (to a inherited type)

纵饮孤独 提交于 2019-12-04 01:18:43
问题 using C# I have a class which contains among other meta information the root node of a directed graph. Let's call this the Container-Class . This container can appear in two different modes, Editor-Mode and Configurator-Mode. Depending on the mode, the root-node is of a different type NodeEdit or NodeConfig , both inheriting from the same subclass. public abstract class NodeBase { string Name { get; set; } ... } public class NodeEdit : NodeBase ... public class NodeConfig : NodeBase ... For

How to add callback function to a javascript class?

时间秒杀一切 提交于 2019-12-03 20:31:02
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.XMLHttpRequest) return new XMLHttpRequest(); else { alert("Your browser does not support AJAX."); return null; } } function onstatechange() { if(httpObject.readyState == 4) { this.callback(httpObject.responseText); } } function open(url, callback) { httpObject = getHTTPObject(); if (httpObject != null) {

How to decide between C# static and non-static methods?

回眸只為那壹抹淺笑 提交于 2019-12-03 18:30:32
问题 [Edit] My original-question was "Why to decide between static and non-static? Both do the same..." Unfortunately it was edited to a C#-specific question what I really wanted to avoid. So, let me do some additions: When I say interface, I don't mean the C#-keyword-interface but what I understand something like a C++-interface: A set of well defined functions to operate with my object. When saying weaken my interface, I mean I have different functions (static/non-static) that do the same thing.

Java: Return class (Not an instance)

被刻印的时光 ゝ 提交于 2019-12-03 17:03:29
问题 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? 回答1: 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

Is it expensive to create objects in .Net?

試著忘記壹切 提交于 2019-12-03 16:29:37
问题 I have just refactored a colleague's code that, roughly, looked like this... public class Utility public void AddHistoryEntry(int userID, HistoryType Historytype, int companyID) { // Do something... } public void AddHistoryEntry(int userID, HistoryType historyType, int companyID, string notes) { // Do something... } } To this... public class HistoryEntry { public long UserID { get; private set; } public HistoryType HistoryType { get; private set; } public long CompanyID { get; set; } public

How to model cycles between immutable class instances?

独自空忆成欢 提交于 2019-12-03 14:12:50
Immutable classes are great but there is one big problem i cant think of a sensible way to solve - cycles. class Friend { Set<Friend> friends(); } How does one model Me having You as a friend who in turn has me as a Friend back ? IMMUTABILITY This class from the outside world should definitely be immutable. The value held internally should be constant for the purposes of equality checks. [[[ Edit: Added code to demonstrate fully immutable concept ]]] That's why builders are so nice for immutables - they allow mutability during construction to get everything set before you "freeze" it. In this

In what cases superclass shouldn't be abstract?

冷暖自知 提交于 2019-12-03 13:56:01
In this thread I found some interesting moment, If class uses only as superclass there isn't rule to make it abstract. Why so? Thanks It all depends on whether or not it makes sense to have instances of the class . Suppose you for instance have one class Dog and one class Cat . They both extend Animal . Now an Animal may have a name and a few methods, but it doesn't make sense to have Animal s running around. An Animal is... well an abstract notion. In other circumstances you may have subclasses (the LinkedHashSet for instance extends HashSet ) but it still makes a lot of sense to instantiate