|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Features Beyond Entity Objects
Modeling concepts with objects
By: Bill Kohl
Sep. 7, 2004 12:00 AM
Thus in object-oriented programming we have the view that computation is simulation. We have all read that objects are software representations of real-world entities and that one of the first design tasks is identifying these entities in our problem domains. These entities then become classes of our applications. However, the object-oriented paradigm allows us to model not only entity objects, but any abstract concept for which behavior can be identified. This article explores how to logically model abstract concepts with objects and why this will deliver higher quality software that is more maintainable and extendable. Entity and Conceptual Objects Creating Conceptual Objects Simulating concepts works in a similar way. First we identify the properties of the concept, then we identify its responses to logical interactions with other objects. A list, for example, has its elements as properties. Its logical interactions would be to answer queries about its size or one of its elements, or add or remove an element. For an integer, its property is its binary representation inside the computer. Its logical interactions are asking for its string representation or to compare itself to another integer object. Concepts Become Objects Encapsulation hides data and implementation; it does not hide the interface. Where does that leave us with the required conditions for simulating concepts? It means that the only requirement for simulation is that we can identify logical requests (interface) that the concept object should respond to. There does not need to be properties for the object. Most objects do have properties, and we should still be asking the question: "What are the logical properties of the concept we are simulating?" Now, however, the answer may be none. An example would be a NetworkBuilder, an object that implements the rules for creating a network. The rules are pure logic and the NetworkBuilder object contains no properties. All the data contained in the network is passed in as arguments; the NetworkBuilder constructs the network and passes back a Network object. Cohesion, Coupling, and Encapsulating Change We could first observe that many business rules are applied by validating data entered into a system. The data may create new entities - a new employee, for example; or couple existing data - add Employee A to Dept. X. In either case, business rules are applied through validation, so validation becomes the concept we wish to objectify. Note that the examples I used above form two distinct cases for validation. In the case of validating a new employee, the data that creates the new employee is cohesive, that is it's all contained in one object, an Employee object. Since the business rules associated with creating this new object involve only that object, they may be included with the behavior of the Employee class and we say that an Employee is self-validating. In the case where Employee A is added to Dept. X, the business rules involve at least two objects and probably more. (A HumanResources object may also be involved.) If the validation logic resides in the objects being validated, they are coupled by the rule logic and, furthermore, the rule itself is fragmented, parts of its logic lying in two or more classes. The concept of validation can be used to create a better situation here. The idea is to create a Validation class (this will probably become a hierarchy in the application) that encapsulates a business rule that would ordinarily be fragmented across two or more objects. This provides cohesion for the rule and decouples the objects involved in the rule. Process Objects What are validation and process objects? How do we logically arrive at their structure? We begin by asking the same questions we asked in simulating lists and integers: What are the properties? What are the behaviors? I will put off the properties question for now and answer the behaviors question with an example, that of an order-fulfillment process. There is an order process that includes four objects. Each of the objects participates in a part of the process and knows which object follows it in the process and what message to send to that object to continue the process. These objects are coupled by the process logic and the process logic is fragmented across these objects. The same process can also have a ProcessObject coordinating the process. The objects involved in the process still carry out the tasks that represent their part of the process. However, they don't have any knowledge of the overall process. They don't know what objects follow them or that they are even a part of a process. The OrderProcess object knows the entire process and is responsible for carrying it out. Process logic is encapsulated in the OrderProcess object and the four objects taking part in the process are decoupled with respect to the process. The OrderProcess object represents the entire process, but has delegated individual tasks to other objects. The PartsClerk object may participate in more than one process. The OrderProcess and a Financial object determine the value of a company's assets. These functionalities are orthogonal, having only the collaboration with the PartsClerk object in common. By encapsulating the logic of each process in one object, the PartsClerk doesn't need any knowledge of either process and contains logic related only to its own purpose. The behavior of the OrderProcess object is the flow logic of the process. In our OrderProcess example, when each step of the process is completed, the OrderProcess object initiates the following step, which constitutes the behavior of the object. What are the properties of the OrderProcess object? Does it have any data properties? It has a name as a possible data property, but that's contained in its class name. Since it is conceptual and doesn't represent any real-world entity, it has no physical properties. Are there any data properties required for its behavior? The answer is probably yes, but if we take the simplest case it's no. In the simplest case the process could be "hard coded" with the logic (hardly ever a good idea) and the OrderProcess would not require any properties Instead of hard coding the process, properties of the process object could include a collection of process nodes, each of which knows how to initiate and finalize its step of the process. We would also include a DataDictionary to contain the data required for completion of the process. In our example, there would be four process nodes, one for each step. When the process is initiated, it sends a "begin( this)" message with itself as an argument to the TakeOrder process node. The process node in turn notifies the OrderClerk object to "takeOrder(this)". When the order has been taken, the OrderClerk notifies the TakeOrder node "orderComplete( )", which in turn notifies the ProcessOrder. The ProcessOrder then sends "begin(this)" to the next node, etc. All ProcessNodes implement a ProcessNodeInterface so that adding a node to the process would simply require inserting into the proper place in the node sequence. One property of the ProcessOrder would be this collection of process nodes. In addition we may wish to place some constraints on the process. This would lead to properties such as beginTime, endTime, and maxElapsedTime. If the process elapsed time exceeds the maxElapsedTime, the OrderProcess object would send notification of this condition to the appropriate party. These self-timing ideas would also apply to the process nodes. (You might already be imagining some additional benefits of process objects. The OrderProcess object could collect information about itself. This could be used to determine how efficient the process is, allowing us to improve the process over time.) For complex processes, the process object may contain as a property a network that represents the process as task nodes and the state conditions dictating the next task of the process. This naturally leads to the idea of a subsystem of objects that form a framework for creating process subsystems. What about the validation object? A validation object is simpler than a process object because it represents a single event. Its behavior is the application of the business rules via its methods. Properties may or may not be present. In the example above, a HumanResources object is required for the validation along with the employee and department objects. The validation class containing this rule may maintain a reference to a HumanResource object as a property. In general, validation objects will not have properties. All of the data necessary to carry out a validation will be passed in as arguments. For example, an employee has certain information. The department may require that employees have certain skills before they can work there. The HR department would apply the process of validation by introducing the employee to the department where the department could interrogate the employee to determine if there is a match. Each department may ask different questions or they may all ask the same question and expect different answers; that information behavior is delegated to the department. The supplied object must conform to the Employee interface and, in doing so, decouples itself from the department. In turn, the department is decoupled from the implementations of Employee so that new types can be introduced with minimal effects on the code. Conclusion I've attempted to show how objects are created from abstract concepts. This is the first step in being able to create object-oriented applications that are composed of systems of cohesive, purposeful objects whose interactions fulfill the requirements. For other ideas of conceptual objects, let me recommend the Wirfs-Brock reference below. Her "roles" should be helpful to you in finding conceptual objects in your applications. References Reader Feedback: Page 1 of 1
Your Feedback
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||