Developing By Me

Project Development and Design

OOP Design Basics

When developing a software project it is important to understand how to design the classes and how they work together. This is the essence of Object Oriented Programming [OOP]. In OOP classes can be self-standing, extend another class, implement an interface or be extended into another class. This can sound complicated, but it is fairly simple to understand. This article will not go into detail about design patterns or methodologies but instead it will cover what is a good OOP class design. Every developer is different and has his or her own way of doing things. This is not bad because it provides diversity to the team. However, when those differences prevent the project from being coherently designed and maintainable then OOP guidelines and standards are not being followed.

Every language handles extending, implementing, scope and overriding differently. There are many similarities in the C style languages. The examples will be in Java but they can be ported to any language that is required for the project. Java is a very middle of the road language because it is used for web pages, web services, applications, and applets. However, if you need to translate to another language do some research on that language to find the equivalent conventions. The following example class extends an ArrayList which is a collection of objects organized in an Array style format. It also implements a custom interface class called ISortable. Here is the class and it will be broken down afterword.

public class MyExampleClass extends ArrayList implements ISortable {
     public String str;        // Accessable outside of class
     protected int count;      // Accessable inside of class and children
     private int local;        // Accessable inside this class only

     public MyExampleClass() {
          //@TODO: overrides the parent constructor
     }

     @Override    
     public void sort() {
          //@TODO: write sorting here
     }

     public void setCount(int count) {
           this.count = count;
     }

     public int getCount() {
          return this.count;
     }
}

As you can see the class is very simple there is no implementation of the actual methods nor is there any control logic created for the array objects. The default collection controls will be used unless they are replaced within the class. The first line is as I explained before where we extend and implement other classes.

public class MyExampleClass extends ArrayList implements ISortable

When a class extends another class it is taking all of its methods (functions) and members (variables). Unless the developer adds methods or members to the class it will be an identical clone of the original class. Which does not provide much usefulness but by adding more to the class the extended class will provide the user with more functionality and provide a more balanced environment through inheritance. The class that is being extended can also be an abstract class which cannot be instantiated on its own. Instantiation is when a class or variable is initialized for the first time. If the class is abstract it cannot be done unless it is extended through another class. An interface is similar with a few exceptions. The interface class cannot have predefined functionality for any of the methods and all methods and members must be public. The abstract class can have members of any scope (not in all languages) but the methods can have default logic available for the developer. In order to either replace or implement these functions the override must be declared.

@Override public void sort() {
     //@TODO: write sorting here
}

In this method we are implementing the sort() function from ISortable. The original function was abstract and had no default functionality. Because of this the new class must implement it with some code. The advantage to this is that any class that uses ISortable will always have the same functions and it allows the developer to expect them to be there when programming.

Scope is one of the most important things to understand about OOP development. OOP languages support three major scopes which are public, protected, and private. The scope of the object determines what can see the object. There are other options such as const, static and final which I will cover shortly. However before we get to the auxiliary types understanding the normal types is important. The public object is one that can be used both internally and externally. This means that the local variables can access it but it can also be accessed by other objects outside of the class. Most functions will be public because they need to be accessed outside of the class to return variables or to process information that requires a returned value. Protected and private objects can only be accessed inside the class. And in order for the values to be set or returned to an external object an getter and a setter are required. The difference is that protected objects can also be used by the children or classes that extend the class. The private objects cannot be used in this way they are only available to the current class. There are advantages to both because some values need to be carried to the children while others should remain local to the upper class because they effect the functions used by that class only.

To make things both easier and more complicated the languages provide the availability of objects to persist outside of an instantiated class which are const and static objects. The const object can be of any scope however its value cannot be changed at runtime. This provides the developer with stability and reliability knowing that the value will remain the same at all times. Constants also allow a single point of failure when working with literals. By using a constant the developer can reduce the number of typos within the project. They can be abused and overused but if used correctly they provide an invaluable service that will reduce development time and debugging of literal strings.

The static object is similar to const in that it will persist in and out of classes. However, that is where the similarities stop. That is because the object is the same in all instances of the parent class. For a function this is not a big deal because it means that it is available outside of the class instantiation however for a variable it is a bit different. When static is applied to a member it will persist throughout all instances of a class and retain the same value. However, unless it is defined as a const the value can be changed by any instance and that change will be reflected in the others. The static members should be used rarely and prudently because if the class relies on it for special processing the changing of the value could produce unforeseen side effects.

The last scope option is final a member or method that is marked as final cannot be overridden via the @Override option. This guarantees that your function cannot be changed by extending the class. A final method often uses private variables within its scope which would be lost if the function was overridden because those values would no longer be available. However, it can be used with static which forces it to retain functionality but still provide that base information in any scope of the class. It is not uncommon to have a public static final method within the code base. An example of this might be the following code.

public static final int Multiply(int a, int b) {
     return a*b;
}

In this case there is no need to override this method because the result should always be the same in every instance of the object. But because it is a single minded function it can be available to all classes even if the parent class is not instantiated.

The different ways to handle OOP are as endless as the developers that use the languages. However, these are some of the basic concepts that will help a new developer to work with the language while they are learning the basics. It is also possible that more seasoned developers may need to relearn the basics which could improve their own code base.

Related pages

  1. Bass Classes and Inheritance
  2. Documentation
  3. OOP for Games
  4. Programming Languages
powered by Related Pages WP plugin

One Response to “OOP Design Basics”

%d bloggers like this: