Monday, October 17, 2011

Object-Oriented Design Part 1 (the beginning)

Congratulations! Now you know Java enough to write any program (as an algorithm)! As of now you know three sufficient language constructs: sequence, selection, and repetition. See the theorem ( http://en.wikipedia.org/wiki/Structured_program_theorem )  proving sufficiency of these constructs  for the design of any algorithm.
Then what is left in learning Java? Although we will pick a few small language enhancements – the main task will be in learning software engineering  as assembling (designing) increasingly complex software systems. The leading industrial approach to such engineering is called Object-Oriented design and programming. According to this approach (which we will be learning in more detail) – nobody writes programs. What do they do instead – is create classes (that will be instantiated as objects at runtime) and organize their interaction when the task triggers such collective attempt to solve the problem or just to be aware of the situation and react when necessary.  
This week gives you an introduction to the concept of classes and objects with some demonstration of data that an object can store and methods (work like local code libraries) that an object can invoke or that can be invoked by other objects in communication attempts.
A few good ideas for good design (many more to follow):
  1. A class should capture one and only one key abstraction/concept/responsibility. For example Student, Professor, Course, etc. are responsible for the behavior of particular entities with their data and methods (behaviors).  Thus a student can get a grade, be transferred, enroll into a course or drop one, pay the required tuition, can change the room in the dorm, etc. as well as have an assigned ID, name, address, telephone, and other data. Of course the program will work if to place these methods in, say, Course class, but following life modifications, updates, and relationships with other entities will become increasingly difficult.  Therefore an object is a special software construct combining properties of data types (allowing for information storage = state) and software libraries capable of executing the called process. Together they create a powerful combination capable of behaving as an entity (like Student) in a specific situation and state. They say that objects have state and behaviors. But in order to unleash their full capabilities – objects need to collaborate with other objects/entities providing necessary services and enriching overall system’s behavior.
  2. All data should be hidden within its class (called encapsulation): For now you might have direct access to data in a class, but in later designs should start avoiding such exposure allowing only access to the methods responsible for data reading/modifications (accessors and mutators”). We create private fields that hold the state of an object (like assigning a grade, a name, and address) and public methods that deal with them (in addition to other functions).  

No comments:

Post a Comment