BenMcLean
OOP design pattern question
I have an interface called IModel for voxel models. I don't want to change it because it has lots of existing implementations without a base class.
There are about a dozen different operations that can be done on the IModel interface. Currently these are static methods in a static class with no global state.
Certain special implementations of IModel have special implementations of some of these methods that get the same result more efficiently from taking advantage of their internal data structures beyond what IModel offers. These are only available for some operations on some IModel implementations some of the time, not consistently.
I want to make it so that when user code calls one of these operations, the program will choose the more efficient special implementation when available or resort to the default IModel version if not available.
What OOP design pattern should I be following to make that happen?
30 replies
Collections expression to add an item at the start.
I've got this:
What I intend for it to do is to return an array of uints which starts with zero as the first value, then appends the contents of
Colors
(another array of uint
) up to a maximum of 255 items, so that this array can never be larger than 256 items.
This expression I've got appears to work, technically, but it also appears that I should be able to do this without creating a new List<uint>
just to add that one item. I'm almost certian there's some expression I could include which wouldn't require that. Anyone know?22 replies
Binary serialization / deserialization where you don't control the format
I wrote this:
The purpose of this is binary serialization/deserialization to/from a file format I don't control. It has to exactly match that format. The question is, how would I convert my naïve
Read
and Write
methods into something that would properly implement Serializable
and ISerializable
the idiomatic C# way?8 replies
✅ Safe type casting with LINQ?
As part of an octree I'm implementing, I have this line:
popped enumerates type
Node
. Type Leaf
is a child class of Node
.
My question is, can I do this Where
and Cast
in a single operation which is type safe or does that line look correct as-is?5 replies
LINQ to keep adding items until a fixed length is reached?
Let's say I have a LINQ collection of
uint
some length less than 256 and I want to keep adding 0u
until it reaches length 256. If the length is already 256 or higher then this method should do nothing. Can I do that with LINQ? If so, what would be the method for that?23 replies
❔ Combine two arrays by alternating items?
Let's imagine I have two arrays, A and B, which each have four items.
I want to make a new array C which is made up of the contents of A and B.
so
Except can I do this with LINQ? And for any arbitrary size?
38 replies
❔ Return groups of results using LINQ?
I'm iterating through a set of data with LINQ.
The data's broken up into sections, where there's a "1" in the "NewSection" column on the first row of a new section, otherwise there's a "0" in that column.
I want to make a function that returns an Enumerable of Enumerables of my data rows, where the inner enumerable is all the entries in each section.
How would I construct my loop for this?
19 replies