encapsulation

How to Encapsulate Change For Random Variations

I am writing a motor vehicle system for a county government that lets a user select many different types of car tags. The problem is that the state has built in a lot of special little exceptions for tag types. I realize it's very messy and inefficient to simply code if statements for each of these, but I can't seem to find a way to ab...

TDD, DDD and Encapsulation

Hi all, After several years of following the bad practice handed down from 'architects' at my place of work and thinking that there must be a better way, I've recently been reading up around TDD and DDD and I think the principles and practices would be a great fit for the complexity of the software we write. However, many of the TDD sa...

What methods are there to modularize C code?

What methods, practices and conventions do you know of to modularize C code as a project grows in size? ...

C#: Encapsulation of for example collections

I am wondering which one of these would be considered the cleanest or best to use and why. One of them exposes the a list of passengers, which let the user add and remove etc. The other hides the list and only let the user enumerate them and add using a special method. Example 1 class Bus { public IEnumerable<Person> Passengers { ...

C#: Difference between List<T> and Collection<T> (CA1002, Do not expose generic lists)

Tried to run Run Code Analysis on a project here, and got a number of warnings that said something like this: CA1002 : Microsoft.Design : Change 'List<SomeType>' in 'SomeClass.SomeProtectedOrPublicProperty' to use Collection, ReadOnlyCollection or KeyedCollection Why should I use Collection<T> instead of List<T>? When I look at the...

Avoiding casts when translating public APIs to internal glue code

So, I have this public API that my application exposes, allowing customers to write plug-ins. For the sake of this example, let's say it's a fairly simple key-value pair system, something like: public interface Key { // marker interface, guaranteed unique in some scope } public interface KVPService { Set<Key> getKeys(); Object ge...

Patterns/Practices for encapsulating predicates

I'm guessing most of us have to deal with this at some point so I thought I'd ask the question. When you have a lot of collections in your BLL and you find that you're writing the same old inline (anonymous) predicates over and over then there's clearly a case for encapsulation there but what's the best way to achieve that? The project...

Drop packet with libpcap

Is it possible to have libpcap remove a packet instead of just sniff it as it passes through? I'm wanting to intercept each packet and encapsulate it into a new packet along with measurement data, but both packets (mine and the original) both reach the destination. Many thanks ...

Should I access POST-Parameters in the model or pass as method arguments from controller?

I have to process about 20 POST-parameters, and I am not sure where to do that. I could define each as an argument of the method on the model, and pass them from the controller when the method is called. This would result in quite a bit of work and make the function call less readable, due to the number of arguments. Or I could call th...

Encapsulating and inheriting methods

Hi all I'm wondering if it's possible to encapsulate the methods of a class, but then expose them within a consuming class. For example (JFTR, I know this code is wrong) class Consumer{ public function __construct($obj){ $this->obj = $obj; } public function doCommand(){ $this->obj->co...

does the 'getters and setters are evil' fail for the view layer?

Many people know this article: more on getters and setters. I think it does a convincing job of portraying the evil side of getters/setters. I've also tested it by trying to convert an existing project (unfinished) to code without getters/setters. It worked. Code readability improved greatly, less code and I've even managed to get rid of...

Encapsulation Aggregation / Composition

The Wikipedia article about encapsulation states: "Encapsulation also protects the integrity of the component, by preventing users from setting the internal data of the component into an invalid or inconsistent state" I started a discussion about encapsulation on a forum, in which I asked whether you should always clone objects inside...

using another object's functionality following a proper OO design - encapsulation

Hello, I am debating the proper, OO-design to use another object's functionality (methods) from a java class, while both objects remain decoupled as much as possible. For example, at some point in my class, to implement my logic, I need to call a method that belongs to another object, say a helper class. This helper class does not need...

Can you explain this thing about encapsulation?

In response to What is your longest-held programming assumption that turned out to be incorrect? question, one of the wrong assumptions was: That private member variables were private to the instance and not the class. (Link) I couldn't catch what he's talking about, can anyone explain what is the wrong/right about that with a...

Yet another question on mocking....

First let me state that, despite being a fairly new practitioner of TDD, I'm pretty much sold on its benefits. I feel like I've progressed enough to consider using mocks and have hit a real brick wall when it comes to understanding where mocks fit in with OOP. I've read as many relevant posts/articles on the subject as I could find (Fo...

linking methods from an enum to member variables of client class

Hello, The following enum structure performs certain operations while remaining agnostic to the client class (for encapsulation reasons) public enum MyEnum implements Commands{ A{ public int method1(int varY) { return varY+2; } public MyEnum method2(){ return MyEnum.B; ...

A question about scope and encapsulation.

I have a generic question about scope and encapsulation. Take two scenarios: Scenario 1: // a global application level constant public static const IS_DEMO_MODE:Boolean = false; ... // somewhere deep in the codebase private function _myFunction():void { if (IS_DEMO_MODE == true) { // If Demo Mode do not allow this functi...

How to encapsulate python modules?

Is it possible to encapsulate python modules 'mechanize' and 'BeautifulSoup' into a single .py file? My problem is the following: I have a python script that requires mechanize and BeautifulSoup. I am calling it from a php page. The webhost server supports python, but doesn't have the modules installed. That's why I would like to do thi...

Interacting with User Controls through Events

I've browsed through the questions on the site, but haven't found a decent answer yet. My problem is that my main form class is cluttered due to all the events and methods. I decided to break the form into user controls. Unfortunately I am having trouble accessing information from other User controls. For example I have a user control...

Java: Good way to encapsulate Integer.parseInt()

I have a project in which we often use Integer.parseInt() to convert a String to an int. When something goes wrong (for example, the String is not a number but the letter 'a', or whatever) this method will throw an exception. However, if I have to put exceptions in my code everywhere, this starts to look very ugly very quickly. I would l...