Design Patterns

27 Apr 2019

Design patterns are helpful for solving many common problems that occur in coding. Whether it’s properly creating classes and methods, interactions between objects or inheritance, creating reusable code or a design pattern is good because it helps you maintain things and not have such a chaotic code (when you eventually get to many lines of code in a program). Three main types of design patterns are creational, structural, and behavioral. Creational is based on object “creation”, such as singleton or factory. Class creation patterns use inheritance, while object creation uses delegation. Structural is more about making a larger “structure”, or to make a new functionality from different classes. Behavioral is about finding the “behavior” of the classes. It finds how one class interacts with another and realizes patterns between objects. You’d mainly want to use loosely coupled code so that when you refactor it in the future (which will be highly likely), it is easier to add new functionalities to it.

In University of Hawaii at Manoa, in the CS curriculum, everyone should be going through object oriented programming and design patterns whether they know it or not. Basically in the intro class of ics 111 which is intro to Java, you learn basic concepts of coding for object oriented programming and inheritance. They don’t explain in great detail in 111 what object oriented programming or design patterns are, but you are doing it as you go through the course. Currently in my 212 class, we’re learning C++ towards the end of the semester. We’ve worked on programs with classes and inheritance as well in this course but in greater detail, as well as in ics 211 which is another Java class. I didn’t really know this until recently, but I’ve been using design patterns all along throughout all of my CS courses.

A basic example of a design pattern which i’ve used in the basics of C++ would be a program of creating shapes. Creating a circle class and using it to createa cylinder class is a simple example, as you could use the circle class for spheres as well. Another easy example of inheritance would be animals. Creating an animal class and having something simple as nameAnimal(name) and using it as a super for animals such as cat or dog, then naming the cat or dog, and having separate member functions inside the cat and dog classes such as

void meow() {
    cout << "meow" << endl;
}

or

void bark() {
    cout << "bark" << endl;
}

is a simple inheritance program.