class-design

Critique of immutable classes with circular references design, and better options

耗尽温柔 提交于 2019-12-22 05:33:41
问题 I have a factory class that creates objects with circular references. I'd like them to be immutable (in some sense of the word) too. So I use the following technique, using a closure of sorts: [<AbstractClass>] type Parent() = abstract Children : seq<Child> and Child(parent) = member __.Parent = parent module Factory = let makeParent() = let children = ResizeArray() let parent = { new Parent() with member __.Children = Seq.readonly children } [Child(parent); Child(parent); Child(parent)] |>

PDO connection class / code and class design

扶醉桌前 提交于 2019-12-21 21:33:26
问题 I'm trying to understand how to use PDO with a "connection" class. class db { private static $dbh; private function __construct(){} private function __clone(){} public static function connect() { if(!self::$dbh){ self::$dbh = new PDO("mysql:host=localhost;dbname=database", "user", "password"); self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } return self::$dbh; } final public static function __callStatic( $chrMethod, $arrArguments ) { $dbh = self::connect(); return call

Should entities implement interfaces?

冷暖自知 提交于 2019-12-21 15:45:30
问题 I personally don't have my entities implement interfaces. For a Task class I wouldn't have ITask that just had the same properties defined on it. I've seen it done a few times though, so I'm wondering where that advice comes from, and what benefits you get from it. If you're using an ORM then the argument that says "I can change my data access" is irrelevent, so what other reason is there for doing this? UPDATE: A good point was made in the comments about INotifyPropertyChanged . That wasn't

“is A” VS “is Like A” relationships, what does each one mean and how do they differ?

巧了我就是萌 提交于 2019-12-21 12:58:16
问题 First an example to discuss: class Foo { // Attributes: int attribute1, attribute2; // Methods: virtual void Foo1() { /* With or without Implementation */ } virtual void Foo2() { /* Also with or without Implementation */ } }; class ExactDuplicate: Foo // No New Attributes or Methods { virtual void Foo1() { /* A new Implementation */ } // Also there might be new Implementations to other methods }; class ExtraMethods: Foo // Having New Methods { virtual void Foo3() { /* Implementation */ } };

RPG Game loop and class structure (cocos2D for iPhone)

落爺英雄遲暮 提交于 2019-12-21 12:45:18
问题 I'm looking to make an RPG with Cocos2D on the iPhone. I've done a fair bit of research, and I really like the model Cocos2D uses for scenes. I can instantiate a scene, set up my characters etc. and it all works really nicely... what I have problems with is structuring a game loop and separating the code from the scenes. For example, where do I put my code that will maintain the state of the game across multiple scenes? and do I put the code for events that get fired in a scene in that scene

Large Inner classes and private variables

六眼飞鱼酱① 提交于 2019-12-21 09:07:44
问题 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

Override member data in subclass, use in superclass implementation?

偶尔善良 提交于 2019-12-21 02:39:27
问题 In Java, is it possible to override member data in a subclass and have that overridden version be the data used in a super class's implementation? In other words, here's what I am trying to get to happen, and it's not happening: abstract public class BasicStuff { protected String[] stuff = { "Pizza", "Shoes" }; public void readStuff() { for(String p : stuff) { system.out.println(p); } } } .. public class HardStuff extends BasicStuff { protected String[] stuff = { "Harmonica", "Saxophone",

Large scale usage of Meyer's advice to prefer Non-member,non-friend functions?

不问归期 提交于 2019-12-20 10:59:43
问题 For some time I've been designing my class interfaces to be minimal, preferring namespace-wrapped non-member functions over member functions. Essentially following Scott Meyer's advice in the article How Non-Member Functions Improve Encapsulation. I've been doing this with good effect in a few small scale projects, but I'm wondering how well it works on a larger scale. Are there any large, well regarded open-source C++ projects that I can take a look at and perhaps reference where this advice

How can a singleton class use an interface?

心不动则不痛 提交于 2019-12-20 10:57:29
问题 I read at many places that singletons can use interfaces. Some how I am unable to comprehend this. 回答1: Every class can implement an interface, and a Singleton is just a "normal" class that makes sure that only one instance of it exists at any point in time apart from the other business logic it may implement. This also means that a Singleton has at least 2 responsibities and this is not good OO design as classes should only have 1 responsibility and make sure they are good at that

What's wrong with Copy Constructors? Why use Cloneable interface?

给你一囗甜甜゛ 提交于 2019-12-20 10:33:19
问题 When programming C++ we used to create copy constructors when needed (or so we were taught). When switching to Java a few years ago, I noticed that the Cloneable interface is now being used instead. C# followed the same route defining the ICloneable interface. It seems to me that cloning is part of the definition of OOP. But I wonder, why were these interfaces created, and the copy constructor seems to have been dropped? When I thought about it, I came to the thought that a copy constructor