Tag Info

Hot answers tagged

125

should there always be some design pattern that I am following? Dear God NO! I mean, you can go ahead and say that any random code is following some random XYZ pattern, but that's no more useful than me claiming to be king of my computer chair. Nobody else really knows what that means and even those that do won't exactly respect my claim. Design ...


29

No. This is what the Gang of Four (who originally popularized design patterns) had to say about it in their book: "No discussion of how to use design patterns would be complete without a few words on how not to use them. Design patterns should not be applied indiscriminately. Often they achieve flexibility and variability by introducing ...


26

if I am writing more complex code, should there always be some design pattern that I am following? No. Design patterns are just that: patterns in relationships between objects. In other words, relationships that are used and reused often enough that someone said "Hey, we seem to be doing this a lot, let's give it a name." The list of design patterns ...


9

Design patterns have two advantages They are easy to describe to other developers, because people generally agree on what the patterns are They tend to have been beaten on pretty thoughtfully by our predecessors, so their strengths and weaknesses are well understood. The goals of every program should be It works. It has to do whatever the end goal is, ...


6

Basically a Subway network is a collection of line, a line is a collection of stations and a station a collection of intersections (1 or more other stations connected) . You could have your Subway network represented by a basic hashMap with all your lines. Your stations could be modeled as a collection of intersection/sequences Subway network - Lines ...


5

There is a third, hybrid alternative, which is to put each global into its own module and write functions to support it. This gives you the OOP-y benefit of all code relating to the variable being isolated in one place and much easier to re-use elsewhere if needed. Putting the support functions in the header so they can be inlined instead of forcing calls ...


5

First of all, a simplification: all real numbers are a subset of complex numbers, so just do everything with complex numbers. Secondly, why isn't Root supported by ComplexNumber? If you're supporting complex numbers, then you can take the root of any number, including negative or complex ones. It all just becomes vector multiplication. So if it's all ...


5

Repeat after me - Patterns are not always the answer Phew. Now that we've gotten that out of the way, lets move on. First off, anytime you're extending 3rd party code that doesn't make any guarantees to lifespan or function declarations, you're asking for a nightmare when updating that 3rd party code. A method could change it's parameters, or the class may ...


4

WordPressParser asks WordPressClient to get blog-posts... Is this really what a parser should do? Asking to get blog posts is not "parsing" by any definition of the term. It's like scissors sending you an invitation to come over to your local barber shop. Now, scissors surely have their business in making it happen, but triggering this entire chain of ...


4

You're doing this backwards. Forget about the design patterns for awhile. Just build your calculator program. Build it from scratch, and just design it however it is intuitive to you. Think about it for a few minutes, come up with a few ideas, then roll with it, start to finish. While you are trying to implement your calculator, you might not design it ...


4

I will buck the trend a little, because the answer is more subtle than other answers are letting on. Every class you write should not employ a design pattern, but most non-trivial programs you write likely should. A non-trivial program without any design patterns indicates: Your program is so unique that no portion of it is similar to common problems ...


4

Broken question. Let me give you a novel definition of design pattern that would undo a lot of damage released by GoF: a design pattern is a good coding practice. That's it. Any reasonably complex module will have several design patterns in it. Any time you cache it's probably a flyweight pattern but I'm not going to revoke your programming degree if you ...


4

You are confused. Synchronizing two different instances of similar data requires knowing which version is correct if there is a discrepancy. If an email message is on one side and not on the other, how do you know whether you should replay a "delete" on the one side, or an "add" on the one side? If email messages with the same ID differ in their content, ...


4

If BusinessAssembly2 contains all of BusinessAssembly1's public members, then you could write one or more Interfaces for that subset of members, which could be used as a constructor parameter in one or more Facade classes, thereby providing Dependency Injection. That would allow you to switch between either version of the underlying business assemblies at ...


4

Option 4: Authorization Headers and RFC 6750 (Bearer Tokens) The solution you are looking for has already been specified as part of the OAuth2 standard, but it stands by itself and will be useful in your scenario. https://tools.ietf.org/html/rfc6750 All requests from the client will pass in a bearer token (your access token), and it looks like any other ...


4

The purpose of the PIMPL idiom is to provide a more low-level separation between the interface and the implementation* so that you can alter the implementation without affecting the interface. In particular, this means the library can retain binary compatibility, allowing users to link to a new version of the library without recompiling their executables. ...


4

From a purely practical perspective, if you include events that are triggered by other events in your log, there are two consequences: If you need to replay the log, these events will be automatically triggered anyway, so you'll need a way of filtering them out If you later determine that the logic that generates some such events is incorrect, and ...


3

Even if it is true that these things will never change, these textures and models are logically part of a specific part of your program: the 3D environment. They only make sense and will only be used within this context. Thus they should not be globals; they should be declared within the relevant context of your program. A basic principle is that all ...


3

I would recommend typedef-ing all the structs, and putting those declarations in a separate include file. Then define all your actual instances of the structs in whatever your "main" file ends up being. Then, simply include the header and declare the needed instances extern in the other files. The linker will take care of matching up all the references, it ...


3

In my experience, as a general rule, you should instantiate the underlying object as soon as you have enough information to create it, and destroy it when the scope or object that owns it terminates. In other words, as soon as you know you don't need the information contained in it and can afford the runtime cost of destruction. Considerations include: ...


3

Good Question! When the service becomes complicated, the number of dependencies will grow more and more In that case your dependencies are maybe not standalone dependencies but can be grouped into simple dependency-holding classes that can be used to pass into the constructor. A good sign would be if more than one of your classes use both IRepositoryA ...


3

I agree with @Ampt's answer. I would add another argument here. Note that you can only ever inherit from one class - I mean, in most popular languages, which happens to include C#. (And even in those where it is possible, such as C++, it is adviced to use this possibility sparringly). So by making your class extend ActiveDirectoryMembershipProvider, you ...


3

The premise of this question is a bit confusing. You cannot "implement a variable". It either exists or it doesn't. Further, "wanting" a class to contain a variable either means: You own the code, so just put the variable in each of the implementing classes (via base class if so desired). You don't own the code (future implementations of this interface). ...


3

I think an important factor is who your service clients are. If your service layer is just an architectural boundary between layers in your own project, and the service client is within the same trust realm, then its ok to relax things, and let unchecked exceptions bubble out to the controller layer, or the service client. However, for public facing code; ...


2

How about using interfaces? public interface IImmutableWorld { // only get methods } public interface IMutableWorld : IImmutableWorld { // add set methods } public class World : IMutableWorld { // implementation for both get and set methods. } You don't need to directly talk with World class, you talk with the interfaces. For example, ...


2

From what (little) I've seen, using maps or other nested structures to make a single global immutable state object like this is fairly common in functional languages, at least the pure ones, especially when using the State Monad as @Ptharien'sFlame mentioend. Two roadblocks to using this effectively that I've seen/read about (and other answers here have ...


2

If you want to give a name to "moving data between related schemas": In Datawarehousing this is called ETL: From Extract_transform_load@wikipedia In computing, extract, transform, and load (ETL) refers to a process in database usage and especially in data warehousing that: Extracts data from outside sources Transforms it to fit operational needs, which ...


2

Creating an interface is the best solution to your problem, and your only arguments against them are: You won't ever have multiple implementations (YAGNI) What I really want is the freedom to add that Interface and alternate implementations later if needed. Interfaces are not just for switching implementations at run-time. Giving yourself ability to ...


2

First I take program to an interface to mean more of programming to a contract rather than programming to a literal Java interface. Contracts can be implemented in classes or interfaces (or even functions). I want to take advantage of this lack of distinction. I also want to interpret the principle "program to an interface" to mean, "I shouldn't ...


2

As I understand it, you have no problem writing OO code. It is in the design phase that you have trouble with. I therefore suggest you study more about Systems Analysis and Design or Object-Oriented Systems Analysis and Design. There are many good books on that topic. Choose one you like. That should help with your "Blank Page" issue, which is something like ...



Only top voted, non community-wiki answers of a minimum length are eligible