Object Oriented Style
Note
When you choose to develop a project using an object oriented language like Java or C++ you are not just choosing a language. object oriented principles, constructs, and design problems are part of the whole package.
And you also have object-oriented design patterns divided into three categories:
- Creational Patterns: Help you with creating new objects.
- Structural Patterns: describes the relationships between objects and interactions between classes and subclasses.
- Behavioral Patterns: Focuses on how objects perform work individually and how they can work as a group to accomplish a goal.
These elements to the creation of an object oriented architectural style for the system, that style of architecture is focused on the data. So when represented with a system to model, you may begin by lookin at the different types of data handled by the system to see how the system would be broken into Abstract Data Types.
The object oriented paradigm allows for inheritance among abstract data types, so one abstract type can be an extension of another.
Consider how an automatic pet feeder would be implemented using the object oriented approach, our goal is to create classes that accurately model the entities involved in the problem and to do this you must first take stock of the data and information flow that is present in the system so that we can identify the classes in that system.
The objects in this system are:
- The pets
- The food dispenser
The food dispenser device consists of:
- Storage for the kibble: The bottom of the container may be temporarily opened to dispense food to a dish below.
- A light: Will flash when the container is empty.
Each pet will have a collar with a unique ID tag, suppose that when the pet is in close proximity with the dispenser, the collar triggers the dispenser to fill the bowl.
Now we can model the pet and dispenser objects in our system as abstract data types, the pet class would have attributes for:
- Collar ID
- Maximum Daily Intake: A value set by the owner for the allowed amount of food each day.
- Current Food Intake: Amount of food already consumed by the pet that day.
And methods for updating or resetting the current food intake amount so each day the value would be reset to zero.
The Food dispenser class would have attributes to keep track of which pets are authorized to visit it and how mush food is left, and methods for:
- Dispensing food
- Flash the "Empty" light
Instances of these classes would interact with each other to determine whether or not food should be dispensed when a particular pet visits the dispenser.
The overall object-oriented architectural style of the system directly follows from the fact that an object-oriented approach was used in the development.
Some problems are well-suited to the object-oriented architectural style. In the example we looked at above, it was relatively easy to identify the classes involved. However, this is not always the case. In a system centered around scientific computation, the identification of classes may result in unnecessary complexity, and in turn, lower performance.
As we shall soon see, the object-oriented programming paradigm will not always be the most appropriate solution to a problem. Another design choice may be better suited for the problem at hand.