<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sumi2509</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sumi2509"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Sumi2509"/>
	<updated>2026-09-12T05:28:57Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37877</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37877"/>
		<updated>2010-10-13T15:14:19Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Delegation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot;, which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from both Teacher and Student and indirectly from Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, Letter object can be part of some other big problem domain also(except that of Drawer).&lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation allows the behavior of an object to be defined in terms of the other object and can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate. In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B calls A's foo method delegating the execution of its function foo to an object of class A. This example clearly explains how delegation works in most of the programming languages.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that delegation as new definition is not widely supported in programming languages, though exceptions exist in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation is based on dynamic binding, as it require that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP should be given more importance. Inheritance based models seem a very attractive choice for almost all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept would make object oriented design more efficient and robust.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance, in the proceedings of conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_S10_ms&amp;diff=37151</id>
		<title>CSC/ECE 517 Fall 2010/ch1 S10 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_S10_ms&amp;diff=37151"/>
		<updated>2010-10-06T17:41:40Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2010/ch1 1a vc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1a br]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1b mg]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1c JF]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1e az]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1e bb]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1f vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2b dg]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 2e RI]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S6 aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S6 km]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 GP]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 MS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 MM]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 PH]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2a CB]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2a mw]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2c ck]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S23 GP]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S24 NS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S23 SS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S23 NR]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S20 TT]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2d AS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3j KS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S24 rm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3h PW]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3e br]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3h az]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3i MM]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 S30 SK]]&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37150</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37150"/>
		<updated>2010-10-06T17:40:21Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot;, which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from both Teacher and Student and indirectly from Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, Letter object can be part of some other big problem domain also(except that of Drawer).&lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation allows the behavior of an object to be defined in terms of the other object and can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate. In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP should be given more importance. Inheritance based models seem a very attractive choice for almost all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept would make object oriented design more efficient and robust.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance, in the proceedings of conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37149</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37149"/>
		<updated>2010-10-06T17:39:17Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot;, which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from both Teacher and Student and indirectly from Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, Letter object can be part of some other big problem domain also(except that of Drawer).&lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation allows the behavior of an object to be defined in terms of the other object and can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate. In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP should be given more importance. Inheritance based models seem a very attractive choice for almost all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept would make object oriented design more efficient and robust.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance in the proceedings of conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida, 1987), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37148</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37148"/>
		<updated>2010-10-06T17:38:28Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot;, which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from both Teacher and Student and indirectly from Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, Letter object can be part of some other big problem domain also(except that of Drawer).&lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation allows the behavior of an object to be defined in terms of the other object and can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate. In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP should be given more importance. Inheritance based models seem a very attractive choice for almost all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept would make object oriented design more efficient and robust.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance in Conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida, 1987), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37147</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37147"/>
		<updated>2010-10-06T17:34:58Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Delegation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot;, which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from both Teacher and Student and indirectly from Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, Letter object can be part of some other big problem domain also(except that of Drawer).&lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation allows the behavior of an object to be defined in terms of the other object and can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate. In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance in Conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida, 1987), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37146</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37146"/>
		<updated>2010-10-06T17:30:52Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Decomposition */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot;, which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from both Teacher and Student and indirectly from Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, Letter object can be part of some other big problem domain also(except that of Drawer).&lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance in Conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida, 1987), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37145</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37145"/>
		<updated>2010-10-06T17:30:38Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Decomposition */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot;, which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from both Teacher and Student and indirectly from Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except that of Drawer).&lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance in Conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida, 1987), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37144</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37144"/>
		<updated>2010-10-06T17:29:20Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot;, which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from both Teacher and Student and indirectly from Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance in Conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida, 1987), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37143</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37143"/>
		<updated>2010-10-06T17:28:15Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot;, which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance in Conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida, 1987), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37142</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=37142"/>
		<updated>2010-10-06T17:24:30Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance in Conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida, 1987), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished.  Retrieved 5th october 2010, from   www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36917</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36917"/>
		<updated>2010-10-06T03:03:56Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[1] Thomas K¨uhne, A Functional Pattern System for Object-Oriented Design. Retrieved 4th October 2010, from http://homepages.mcs.vuw.ac.nz/~tk/fps/fps-sans-escher.pdf&lt;br /&gt;
&lt;br /&gt;
[2] Composition vs Inheritance in Java. Retrieved 4 October 2010, from http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html&lt;br /&gt;
&lt;br /&gt;
[3] Inheritance versus composition: Which one should you choose, retrieved 4th October 2010, from  http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=2&lt;br /&gt;
&lt;br /&gt;
[4] Inheritance. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&lt;br /&gt;
&lt;br /&gt;
[5] Decomposition. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Decomposition_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[6] Object Oriented Programming Concepts, Retrieved 4th October 2010, from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
[7] Message Passing. Wikipedia, the Free Encyclopedia. Retrieved 3rd October 2010, from http://en.wikipedia.org/wiki/Message_passing&lt;br /&gt;
&lt;br /&gt;
[8] Object Oriented Programming, Retrieved 4th October 2010, from http://objc.toodarkpark.net/oop.html&lt;br /&gt;
&lt;br /&gt;
[9] Meilir Page-Jones, Larry L. Constantine, Fundamentals of object-oriented design in UML, Addison-Wesley Professional,1999&lt;br /&gt;
&lt;br /&gt;
[10] Lynn Andrea Stein,Delegation is inheritance in Conference on Object Oriented Programming Systems Languages and Applications, (Orlando, Florida, 1987), ACM, 138 - 146  Year of Publication: 1987&lt;br /&gt;
&lt;br /&gt;
[11] Delegation Definition. Retrieved 5th October from , http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
[12] Manish Sharma, When Not to use Inheritance but Delegation.  Retrieved 5th october 2010, from http://developergeeks.com/article/16/when-not-to-use-inheritance-but-delegation&lt;br /&gt;
&lt;br /&gt;
[13] Peter Bosch, Inheritance vs. delegation: Is one better than the other? Unpublished. www.python.org/ftp/python/doc/delegation.ps&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36902</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36902"/>
		<updated>2010-10-06T02:26:36Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Elements of OOP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36847</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36847"/>
		<updated>2010-10-06T00:29:06Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the discussion of the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36846</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36846"/>
		<updated>2010-10-06T00:27:50Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the discussion of the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36844</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36844"/>
		<updated>2010-10-06T00:25:34Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the discussion of the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;br /&gt;
&lt;br /&gt;
==Comparing Inheritance with Others==&lt;br /&gt;
&lt;br /&gt;
As discussed above, inheritance forms a basic way to compartmentalize and code reuse in object oriented programming. However in some cases, inheritance may not be the best way to approach the problem. Some of the limitations of inheritance are:&lt;br /&gt;
&lt;br /&gt;
* When single inheritance is used, an object can only inherit from a single object. If in the above given inheritance example, single inheritance is used Teaching assistant can either be student or a teacher but not both. Using multiple inheritance solves the problem (as in above example), but it can still inherit from each super class only once and hence a Teaching assistant cannot be a student at two different institutions. Also, multiple inheritance creates problem when a same trait is inherited from two different parents.&lt;br /&gt;
&lt;br /&gt;
* Inheritance hierarchy for an object is fixed at the time of instantiation and cannot be changed. In the above example, Student object cannot change to Teacher while retaining characteristics of an Employee. Hence inheritance locks the original design which becomes difficult to change.&lt;br /&gt;
&lt;br /&gt;
* When any client has access of any of the subclass, it automatically has access to the data which is part of the super class, even if data is private to the parent class. E.g in above example, access to student class gives an access of person class and can lead to unwarranted change of Person class properties.&lt;br /&gt;
&lt;br /&gt;
Hence, it can be said that inheritance is not always a good approach for all the problems and some of the elements of OOP discussed above can be good alternatives for inheritance.&lt;br /&gt;
&lt;br /&gt;
===Decomposition and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Traditionally, inheritance is used when there is a &amp;quot;is a&amp;quot; relationship between objects and decomposition when there is &amp;quot;has a&amp;quot; relationship between objects. An example of inheritance where &amp;quot;Programmer&amp;quot; and &amp;quot;Manager&amp;quot; extend &amp;quot;Employee&amp;quot; (is a relationship) can also be looked as, &amp;quot;Programmer&amp;quot; as a set of duties that an &amp;quot;Employee&amp;quot; may have, and this same employee may later change duties for &amp;quot;Manager&amp;quot; duties (has a relationship). Hence the way we design our system makes decomposition or inheritance favorable. But as a rule of thumb, object oriented design based on decomposition is more stable when parent objects are likely to change. In our decomposition example considered above, even if drawer functionality and role change drastically basic participants (letter object in this case) are likely to remain same and can be used in some other domain also. Hence, decomposition is used when there is lot of code reuse and/or polymorphism. By separating behaviors from the primary class hierarchy and including specific behavior for the child class, child class is not restricted by behavior of its ancestral classes.&lt;br /&gt;
Inheritance should be preferred when there is significant interaction between parent and its child classes. If parent must call back to child methods, then inheritance is usually the good answer. As an example, consider a class apple which inherits from its super class Fruit. Any change in the behavior of fruit will directly effect Orange which &amp;quot;is a&amp;quot; type of fruit. When super class and its subclasses are meant to be tied together inheritance should be used as it allows us to take advantage of dynamic binding and polymorphism.&lt;br /&gt;
&lt;br /&gt;
===Message Passing and Inheritance===&lt;br /&gt;
Object oriented programs generally have two kinds of structures; one style is to have an inheritance hierarchy. The other is to have a pattern of message passing. Inheritance structure represents how different objects are related by their types. For example, in the program that models water use, Faucets and WaterPipes can be modeled as same kind of objects, except that Faucets can be turned on and off and WaterPipes can have multiple connections to other WaterPipes. Thus Faucet and WaterPipe objects can inherit from a common antecedent with all common traits and will have their specific properties in their classes.&lt;br /&gt;
Message passing structure represents how program works. For example, in the above mentioned program for water use, Appliance objects might send messages requesting water to Valves, and Valves to WaterPipes. The Building object can communicate through messages with all the Valves, Faucets, and WaterPipes, but not directly with Appliances. The connection between different objects which know whom to contact and pass message defines program structure.  &lt;br /&gt;
[Example taken from: http://objc.toodarkpark.net/oop.html ]&lt;br /&gt;
&lt;br /&gt;
There are situations where inheritance structure for the program may not work well. For example, consider an object Room which defines characteristics for the room. Room can be cubiod in shape so Object Room can inherit characteristics from Cuboid Object. However problem arise when different Rooms can have different shapes say cylindrical. Inheriting from both Cubiod and Cylindrical Objects will make Room both cylindrical and cubiod, instead of being cylindrical or cubiod in shape. Hence inheritance would not work here. A better way would be to have separate objects Shape and Room and shape object points to Cubiod or Cylindrical object. A Room object can interact with object Shape by passing messages. In this design approach access to all of other object facilities is not granted, instead an object needs to pass messages to access attributes and operations of the other class. Thus message passing exposes only the required interface of the object.&lt;br /&gt;
&lt;br /&gt;
===Delegation and Inheritance===&lt;br /&gt;
&lt;br /&gt;
Delegation is closely related to message forwarding and as discussed in above section, just like message forwarding and inheritance, there can be two kinds of structures for programs involving inheritance and delegation. Delegation supports the prototype-based programming model and inheritance, class based programming model. Inheritance schemes involve two kinds of objects, classes and instances. A class defines what is known as structure of its instances. Each instance maintains its values of attributes defined in the structure and the state of an object is given by the values of its attributes at any point in time. In delegation, there is only one type of object which represents real-world items. These objects are often taken to be “instances without classes.” This independence of all objects in delegation scheme allows delegation to capture the behavior of inheritance to some extent.&lt;br /&gt;
&lt;br /&gt;
When it comes to building large systems with many objects, using an inheritance based system seems to make more sense. This is due to the fact that inheritance based system forces programmer to think and place some common behavior in super classes. In delegation based system, common behavior can be put in a single class, but this results in dependencies between objects and changing one object might result in errors in another object which is delegating the job to the first object. &lt;br /&gt;
&lt;br /&gt;
In the nutshell, following are the situations when using delegations over inheritance is beneficial:&lt;br /&gt;
&lt;br /&gt;
* When an object can play more than one role in the system, then sub-classing is not advisable, as the numbers of combinations are very large. Delegation would result in fewer classes and work best in this case.&lt;br /&gt;
* Inheritance, as discussed before, is a static relationship. If a class inherits from another class, this relationship cannot be changed at run time. On the other hand, Delegation is a dynamic relationship that can be changed at runtime. &lt;br /&gt;
* If an object needs to hide a function or an attribute from its super-class, then it is not a right candidate for inheritance. Delegation is an appropriate solution for this situation.&lt;br /&gt;
* If there’s no control over the class which is being inherited, it is good to use delegation over inheritance as inheritance has the risk that classes may have to be changed due to changes in parent class over which there’s no control. &lt;br /&gt;
* Delegation, unlike Inheritance, doesn’t force an object to accept all the methods of the super class, an object can only provide those methods that really make sense.&lt;br /&gt;
&lt;br /&gt;
However, delegation has certain disadvantages over inheritance&lt;br /&gt;
&lt;br /&gt;
* Delegation results in large number of objects in memory, which could result in performance issue when the objects in memory grow fast due to inefficient handling or disposal of objects.&lt;br /&gt;
* Delegation results in code more difficult to read understand and debug. This is due to the fact that delegation involves certain level of object indirection. &lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
OOP has many features which makes it a favorable option in many problem domains. However, based on the requirements of the application, what is needed are subtle design decisions about which feature of OOP is a best option. Inheritance based models seem a very attractive choice for all object oriented applications, which is not true. As discussed in this article, techniques like decomposition, delegation and message forwarding do compass better alternatives for inheritance for some object oriented applications. What is needed is a perspective to see which OOP concept fits best in the overall picture.&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36843</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36843"/>
		<updated>2010-10-06T00:21:50Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the discussion of the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
Image taken from http://www.freecomputertutor.net/object-oriented-programming-concepts.php#messagepassing&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36842</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36842"/>
		<updated>2010-10-06T00:20:59Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the discussion of the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[Image:Decomposition.jpg|frame|center|Decomposition]]&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
[[Image:MessagePassing.jpg|frame|center|Message Forwarding]]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36837</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36837"/>
		<updated>2010-10-06T00:19:10Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the discussion of the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36836</id>
		<title>CSC/ECE 517 Fall 2010/ch3 S30 SK</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_S30_SK&amp;diff=36836"/>
		<updated>2010-10-06T00:17:31Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Decomposition, message forwarding, and delegation versus inheritance in OOP'''&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Object oriented programming (OOP) paradigm is based upon what is known as &amp;quot;objects&amp;quot; which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. &lt;br /&gt;
Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition &amp;amp; delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.&lt;br /&gt;
&lt;br /&gt;
==Elements of OOP==&lt;br /&gt;
&lt;br /&gt;
Some of the elements of OOP, relevant to the discussion of the topic are discussed below&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects.  In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.&lt;br /&gt;
&lt;br /&gt;
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.&lt;br /&gt;
&lt;br /&gt;
===Decomposition===&lt;br /&gt;
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.&lt;br /&gt;
&lt;br /&gt;
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type &amp;quot;has a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of &amp;quot;Drawer&amp;quot; has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer). &lt;br /&gt;
&lt;br /&gt;
===Message Forwarding===&lt;br /&gt;
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values.  Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.&lt;br /&gt;
&lt;br /&gt;
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.&lt;br /&gt;
&lt;br /&gt;
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling. &lt;br /&gt;
&lt;br /&gt;
As an example for message forwarding, when the object called &amp;quot;joe&amp;quot; (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object &amp;quot;my Porsche&amp;quot;, which in turn, invokes the &amp;quot;my Porsche&amp;quot; accelerate method. &lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper &amp;quot;Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Example for old definition:&lt;br /&gt;
&lt;br /&gt;
Suppose there are class A and B defined as follows:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
      print(&amp;quot;Object A doing the job&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
  public:&lt;br /&gt;
    A a;&lt;br /&gt;
    foo()&lt;br /&gt;
    {&lt;br /&gt;
       a.foo();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Here class B delegates the execution of its function foo to an object of class A.&lt;br /&gt;
&lt;br /&gt;
Example of new definition:&lt;br /&gt;
&lt;br /&gt;
 class A&lt;br /&gt;
 {&lt;br /&gt;
   foo() {&lt;br /&gt;
       self.bar()   // self is also known under the names &amp;quot;current&amp;quot; and &amp;quot;this&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 class B&lt;br /&gt;
 {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
   bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.&lt;br /&gt;
&lt;br /&gt;
Example taken from http://www.wordiq.com/definition/Delegation_(programming)&lt;br /&gt;
&lt;br /&gt;
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.&lt;br /&gt;
&lt;br /&gt;
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.  Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:MessagePassing.jpg&amp;diff=36826</id>
		<title>File:MessagePassing.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:MessagePassing.jpg&amp;diff=36826"/>
		<updated>2010-10-06T00:00:49Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Inheritance.jpg&amp;diff=36825</id>
		<title>File:Inheritance.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Inheritance.jpg&amp;diff=36825"/>
		<updated>2010-10-05T23:59:50Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Decomposition.jpg&amp;diff=36824</id>
		<title>File:Decomposition.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Decomposition.jpg&amp;diff=36824"/>
		<updated>2010-10-05T23:59:33Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36253</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36253"/>
		<updated>2010-09-23T01:05:36Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Properties of any OODSL: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Domain Specific Object Oriented Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. Initial DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
===Techniques/Tools used===&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
===Steps to create an OODSL===&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2] Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3] Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4] Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5] Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6] Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7] Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8] ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9] MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10] Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11] LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12] Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S20_st&amp;diff=36165</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S20 st</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S20_st&amp;diff=36165"/>
		<updated>2010-09-22T21:43:18Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2010/ch1 1a vc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1a br]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1b mg]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1c JF]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1e az]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1e bb]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1f vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2b dg]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 2e RI]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S6 aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S6 km]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 GP]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 MS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 MM]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 PH]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2b AT]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2c ck]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S23 GP]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S24 NS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S23 SS]]&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S20_st&amp;diff=36163</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S20 st</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S20_st&amp;diff=36163"/>
		<updated>2010-09-22T21:42:49Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2010/ch1 1a vc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1a br]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1b mg]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1c JF]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1e az]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1e bb]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1f vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2b dg]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 2e RI]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S6 aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S6 km]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 GP]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 MS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 MM]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 PH]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2b AT]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2c ck]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S23 GP]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S24 NS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE_517_Fall_2010/ch2_S23_SS]]&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36162</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36162"/>
		<updated>2010-09-22T21:42:04Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Domain Specific Object Oriented Languages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. Initial DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL:===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
===Techniques/Tools used===&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
===Steps to create an OODSL===&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2] Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3] Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4] Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5] Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6] Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7] Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8] ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9] MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10] Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11] LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12] Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36161</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36161"/>
		<updated>2010-09-22T21:41:52Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Domain Specific Object Oriented Languages''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. Initial DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL:===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
===Techniques/Tools used===&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
===Steps to create an OODSL===&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2] Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3] Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4] Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5] Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6] Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7] Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8] ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9] MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10] Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11] LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12] Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36160</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36160"/>
		<updated>2010-09-22T21:41:35Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''''Domain Specific Object Oriented Languages''''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. Initial DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL:===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
===Techniques/Tools used===&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
===Steps to create an OODSL===&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2] Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3] Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4] Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5] Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6] Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7] Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8] ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9] MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10] Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11] LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12] Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36159</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36159"/>
		<updated>2010-09-22T21:39:56Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Domain Specific Object Oriented Languages=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. Initial DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL:===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
===Techniques/Tools used===&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
===Steps to create an OODSL===&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2] Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3] Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4] Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5] Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6] Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7] Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8] ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9] MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10] Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11] LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12] Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36068</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=36068"/>
		<updated>2010-09-22T16:09:14Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* History of OODSLs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. Initial DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL:===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
===Techniques/Tools used===&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
===Steps to create an OODSL===&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2] Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3] Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4] Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5] Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6] Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7] Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8] ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9] MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10] Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11] LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12] Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=35826</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=35826"/>
		<updated>2010-09-21T18:01:40Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. First DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL:===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
===Techniques/Tools used===&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
===Steps to create an OODSL===&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2] Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3] Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4] Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5] Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6] Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7] Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8] ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9] MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10] Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11] LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12] Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=35825</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=35825"/>
		<updated>2010-09-21T18:00:44Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Steps to create an OODSL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. First DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL:===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
===Techniques/Tools used===&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
===Steps to create an OODSL===&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1]Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2]Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3]Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4]Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5]Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6]Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7]Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8]ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9]MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10]Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11]LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12]Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=35824</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=35824"/>
		<updated>2010-09-21T18:00:27Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Techniques/Tools used */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. First DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL:===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
===Techniques/Tools used===&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
==Steps to create an OODSL==&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1]Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2]Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3]Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4]Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5]Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6]Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7]Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8]ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9]MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10]Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11]LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12]Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=35823</id>
		<title>CSC/ECE 517 Fall 2010/ch2 S23 SS</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_S23_SS&amp;diff=35823"/>
		<updated>2010-09-21T17:59:33Z</updated>

		<summary type="html">&lt;p&gt;Sumi2509: /* Creating an OODSL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
Domain Specific Language (DSL) is any programming language which is designed to solve some specific problems and tasks in a specific domain. These languages are very  focused and restricted to particular problem domain. This is in contrast to general programming languages (GPL) like java or C++ which can be used for software development in many application domains. Examples for DSL include:&lt;br /&gt;
* SQL for relational database&lt;br /&gt;
* Verilog for statistics  &lt;br /&gt;
* Latex for typesetting&lt;br /&gt;
&lt;br /&gt;
Object Oriented DSL's (OODSL) are DSL's based on object oriented programming paradigm. They employ the idea of classes and objects with concepts like encapsulation, polymorphism, abstraction etc. OODSL's like any other DSL's are much more expressive in their domain and much less comprehensive than general purpose object oriented programming languages like Java, C# etc. They are high-level languages, in the sense that they abstract from low-level implementation details. Some OODSL examples are Rails based on ruby programming language and JSP tags, Hibernate and Spring based on Java.&lt;br /&gt;
&lt;br /&gt;
===Pros and Cons of OODSL===&lt;br /&gt;
&lt;br /&gt;
Like any other DSL, employing OODSL in any application domain comes with its own set of advantages and disadvantages. Some of them are discussed below. &lt;br /&gt;
&lt;br /&gt;
Pros of using OODSL for application development are:&lt;br /&gt;
* OODSL's are developed for a particular domain and hence can handle the problems related to that domain. They help express solution in the idiom and abstraction of problem domain and can be easily understood by experts in that domain.&lt;br /&gt;
* OODSL's are much simpler to use than general purpose language in their domain.&lt;br /&gt;
* They are concise, self documented and easy to understand and hence aid in easy development, maintainability and productivity &lt;br /&gt;
&lt;br /&gt;
On the other side, cons of using OODSL are:&lt;br /&gt;
* Developing an OODSL require effort and cost.&lt;br /&gt;
* OODSL has limited scope within their domain.&lt;br /&gt;
* Using a newly developed OODSL requires users to understand its syntax.&lt;br /&gt;
* Using OODSL together with GPL constructs can be a difficult task.&lt;br /&gt;
&lt;br /&gt;
===Types of OODSL===&lt;br /&gt;
&lt;br /&gt;
OODSL can be of two types, internal and external : &lt;br /&gt;
&lt;br /&gt;
'''Internal OODSL''' are the ones which use a host language and give them the feel of a particular language. Developing internal OODSL's is comparatively simple as they use grammar, parsers and tools of underlying language. But they are constrained by the underlying language. The challenge is to design these languages so as to have syntax within the confines of host language, yet these languages are simple, concise and fluent. An example of internal OODSL would be Rake (make equivalent) based on host, Ruby.Internal OODSL's are also referred as embedded languages or fluent interfaces.&lt;br /&gt;
&lt;br /&gt;
'''External OODSL''', on the other hand is designed to be independent of any other language. Syntax, grammar, parsing of these languages is pretty much based on its author choice and gives both pain and pleasure of developing all from scratch. Example of external DSL would be Ant based on java.&lt;br /&gt;
&lt;br /&gt;
==History of OODSLs==&lt;br /&gt;
&lt;br /&gt;
History of DSL dates back than modeling approaches used now to develop them. First DSL's were developed many decades before. Worth mentioning is FORTRAN developed in late 1950s, an abbreviation of 'formula translation'. FORTRAN borrowed notation from mathematics so that programmers could write mathematical formulas directly.  OODSL's started coming up after object oriented paradigm became prominent in 1970's and 1980's. OODSL's appeared mostly in late 1980's and 1990's. Now, OODSL's are used in web applications (e.g. Rails), computer games and environments (e.g. MOO programming) and as programming languages for music (e.g. Formes).&lt;br /&gt;
&lt;br /&gt;
==OODSL Examples==&lt;br /&gt;
&lt;br /&gt;
Some examples of OODSL prevalent today are:&lt;br /&gt;
&lt;br /&gt;
* '''Kiddo''' - Kiddo is an internal OODSL based on ruby and used for building silverlight and WPF applications. Kiddo is based on ideals of &amp;quot;kid friendly&amp;quot; coding and has 5 core methods to get started with drawing.&lt;br /&gt;
* '''ColdFusion Markup Language''' - Commonly known as CFML, it is a scripting language used by Adobe's ColdFusion and other scripting engines. The CFML includes a set of tags that can be used in ColdFusion pages to interact with data sources and display output.&lt;br /&gt;
* '''MOO Programming Language''' - Moo programming language is used to support Mud Object Oriented or MOO server which is online virtual reality system to which multiple players can connect at the same time. Its domain is computer gaming environment.&lt;br /&gt;
* '''LPC''' - It is an object-oriented programming language derived from C and developed originally to facilitate MUD building. It was designed for game development.&lt;br /&gt;
* '''Formes''' - It is an object-oriented DSL used for music composition and synthesis. It is written in VLISP.&lt;br /&gt;
* '''R - R''' is a programming language for statistical computing and graphics. R has stronger object-oriented programming facilities than most statistical computing languages available.&lt;br /&gt;
* '''Kermeta''' - Kermeta is a meta-modeling language used to describe structure and the behavior of models. Kermeta can be used as the core language for a model oriented platform.&lt;br /&gt;
&lt;br /&gt;
==Creating an OODSL==&lt;br /&gt;
&lt;br /&gt;
OODSL can be created using many techniques and tools available in the market.&lt;br /&gt;
&lt;br /&gt;
===Properties of any OODSL:===&lt;br /&gt;
&lt;br /&gt;
Any OODSL should be fluent and in context. &lt;br /&gt;
&lt;br /&gt;
* '''Fluency''': A fluent OODSL should have interfaces which are properly designed for human use and has high readability and natural flows within the language. An example explaining fluency is given below:&lt;br /&gt;
&lt;br /&gt;
Looping through any collection in java was done traditionally as:&lt;br /&gt;
 &lt;br /&gt;
 for(int i = 0; i &amp;lt; products.size(); i++)&lt;br /&gt;
 {&lt;br /&gt;
  String name = (String) products.get(i);&lt;br /&gt;
  //...&lt;br /&gt;
  //.....&lt;br /&gt;
  &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
However it can be done more fluently starting Java 5 as:&lt;br /&gt;
 &lt;br /&gt;
 for(String product : products)&lt;br /&gt;
 {&lt;br /&gt;
 // ...&lt;br /&gt;
 // ...&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This flow comes naturally to any user and is easier to interpret.&lt;br /&gt;
&lt;br /&gt;
* '''Context:''' OODSL should have a proper context on which they are built upon, after all, they are domain specific. Shared context makes OODSL easier to understand and work with and highly expressive. For example:&lt;br /&gt;
&lt;br /&gt;
In java declaring and adding into the cart is done as:&lt;br /&gt;
&lt;br /&gt;
 java.util.ArrayList&amp;lt;String&amp;gt; cart = new java.util.ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
 cart.add(&amp;quot;Milk&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Juice&amp;quot;);&lt;br /&gt;
 cart.add(&amp;quot;Apple&amp;quot;);&lt;br /&gt;
 System.out.print(&amp;quot;My cart has %d items.&amp;quot;, cart.size())&lt;br /&gt;
&lt;br /&gt;
Same thing can be done in groovy as: &lt;br /&gt;
 cart = []&lt;br /&gt;
 cart.with {&lt;br /&gt;
  add &amp;quot;Milk&amp;quot;&lt;br /&gt;
  add &amp;quot;Juice&amp;quot;&lt;br /&gt;
  add &amp;quot;Apple&amp;quot;&lt;br /&gt;
  println &amp;quot;My cart has $size items.&amp;quot;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Languages like Groovy and javascript are easier than java to build context such that convention is followed.&lt;br /&gt;
&lt;br /&gt;
Examples taken from: http://www.javaworld.com/javaworld/jw-07-2008/jw-07-dsls-in-java-2.html?page=3&lt;br /&gt;
&lt;br /&gt;
==Techniques/Tools used==&lt;br /&gt;
&lt;br /&gt;
There are many OO languages like Smalltalk, ruby, groovy which easily lend themselves for creating internal OODSL. Ruby provides expressive, flexible syntax and has a dynamic nature which makes implementing a new OODSL easy without using any additional parser or compiler. Also, groovy easily supports creating fluent and in context OODSL's. On the other hand, C++ or Java syntax is not easy to attain the level of fluency required for a DSL, and so creating internal OODSLs using these languages is challenging. However, these languages offer good parsing libraries or capabilities and their support for creating external DSL’s is pretty strong. &lt;br /&gt;
&lt;br /&gt;
Tools like visual studio and ANTLR aid in creating OODSL. Visual studio contains Domain-Specific Language Designer Wizard which provides many solution templates which can be used to create an OODSL. ANTLR, ANother Tool for Language Recognition, is another language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions. It supports many runtime libraries of ruby, C#, java etc which helps to create internal OODSL.&lt;br /&gt;
&lt;br /&gt;
There are four basic approaches used for creating OODSL. These are:&lt;br /&gt;
&lt;br /&gt;
* '''Instantiation:''' This approach is used mostly in Ruby projects, and not even recognized as a DSL. Using this approach, DSL is defined by defining methods of an object. Interaction is like any other OODSL, done by instantiating the object and calling the methods. The HTML creation DSL of Ruby’s CGI class uses this approach.&lt;br /&gt;
* '''Class macros:''' DSL is defined as methods on some ancestor class, and those methods are tweaked to modify the behavior of super-class and for subclasses of the particular class in question. These kinds of macros often create new methods. “has_many” in ActiveRecord is a good example of this type of approach.&lt;br /&gt;
* '''Top-level methods:''' There exists a configuration file, which is just like a Ruby script augmented with some DSL syntax. Any application using this type of DSL will load this configuration and uses DSL top level methods defined. When those methods are called in the configuration file, they modify some central (typically global) data, which then application uses to determine how it should execute. Rake is an example of this kind of DSL.&lt;br /&gt;
* '''Sandboxing:''' This approach is a special case of the more general instantiation technique. Using this technique, DSL is defined as methods of some object which is really just a “sandbox”. Interaction with the object’s methods modifies state of the sandbox which is queried by the application. This technique is similar to the top-level methods technique, with an exception that the DSL is restricted to the sandbox and there are no global methods involved. Needle uses this approach.&lt;br /&gt;
&lt;br /&gt;
==Steps to create an OODSL==&lt;br /&gt;
&lt;br /&gt;
Some of the steps which can be used to create an OODSL are:&lt;br /&gt;
* Identifying the goals to be accomplished.&lt;br /&gt;
* Reasoning why creating a new OODSL would be better than existing well crafted OODSL or an API.&lt;br /&gt;
* Defining ideal syntax before even coding.&lt;br /&gt;
* Referring to other examples of OODSL's, learning from their code and looking at the similarities.&lt;br /&gt;
* Refining syntax to get the best Return on Investment. If creating an internal OODSL, it is best to take advantage of host language’s compiler and adapting syntax slightly without creating a new full fledged parser or compiler.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&lt;br /&gt;
Below is an example of how an OODSL for creating SQL statements and querying database systems can be created.  It is compared with how the same task is done in general purpose language. &lt;br /&gt;
&lt;br /&gt;
Dynamic construction of an SQL statement in Java : &lt;br /&gt;
&lt;br /&gt;
 String sql = &amp;quot;select id, name &amp;quot; +&lt;br /&gt;
             &amp;quot;from customers c, order o &amp;quot; +&lt;br /&gt;
             &amp;quot;where &amp;quot; +&lt;br /&gt;
             &amp;quot;c.since &amp;gt;= sysdate - 30 and &amp;quot; +&lt;br /&gt;
             &amp;quot;sum(o.total) &amp;gt; &amp;quot; + significantTotal + &amp;quot; and &amp;quot; +&lt;br /&gt;
             &amp;quot;c.id = o.customer_id and &amp;quot; +&lt;br /&gt;
             &amp;quot;nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot;; &lt;br /&gt;
&lt;br /&gt;
The same can be written using OODSL as follows : &lt;br /&gt;
&lt;br /&gt;
 Table c = CUSTOMER.alias();&lt;br /&gt;
 Table o = ORDER.alias();&lt;br /&gt;
 Clause recent = c.SINCE.laterThan(daysEarlier(30));&lt;br /&gt;
 Clause hasSignificantOrders = o.TOTAL.sum().isAbove(significantTotal);&lt;br /&gt;
 Clause ordersMatch = c.ID.matches(o.CUSTOMER_ID);&lt;br /&gt;
 Clause activeCustomer = c.STATUS.isNotNullOr(&amp;quot;DROPPED&amp;quot;);&lt;br /&gt;
 String sql = CUSTOMERS.where(recent.and(hasSignificantOrders)&lt;br /&gt;
                       .and(ordersMatch)&lt;br /&gt;
                       .and(activeCustomer)&lt;br /&gt;
                       .select(c.ID, c.NAME)&lt;br /&gt;
                       .sql(); &lt;br /&gt;
&lt;br /&gt;
The SQL statement written in Java requires developer to be familiar with Oracle-SQL. This may not be the case for every application development in every domain. An OODSL like above can help in these cases. Creating basic classes representing different constructs of SQL query makes creating query easy. E.g. Class “Table” represents a basic API available through DSL and is used for representing tables which are to be queried. Creating different clauses using class “Clause” help filter data from the database.&lt;br /&gt;
&lt;br /&gt;
The DSL code is less procedural, more declarative and object-oriented. It also has several advantages, major one being creating SQL statements without having familiarity of how to write SQL statements in database packages like Oracle, MySQL etc.  The commonly used phrase, “nvl(c.status, 'DROPPED') != 'DROPPED'&amp;quot; in Oracle SQL cannot be easily understood by a non-SQL programmer. By replacing this phrase with the more easily understandable and language-like &amp;quot;isNotNullOr(“DROPPED”),&amp;quot; readability has been enhanced, and the system is protected from a later need to change the implementation to take advantage of facilities offered by another database vendor.&lt;br /&gt;
&lt;br /&gt;
Note: The above example was explained in an easier manner. Further reading can be done from original source, http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
 &lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
This article gives some basic overview of OODSL features. OODSLs are mainly used in the domain of software engineering. The challenge using software engineering is to create flexible, reliable, scalable and maintainable systems. To achieve this, the software engineer must apply a wide variety of tools and techniques. One of these is to take advantage of the knowledge from underlying application domain, by means of a domain-specific language (DSL), which provides a notation that can be used to compose applications from a set of concepts tailored towards a specific application domain. Creating OODSL is a good option because OODSL provides benefits from both the worlds, DSL and OO concepts. An OODSL can be easily extended later, if required, which may not be the case with any other DSL. Hence it can be said that, OODSLs provide a certain level of abstraction of a domain, which can result in a greater control over or understanding of that domain. At the same time, they provide some good features of object oriented programming.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1]Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. Lecture Notes in Computer Science, 2008, Volume 5235/2008, 291-373.&lt;br /&gt;
&lt;br /&gt;
[2]Martin Fowler. MF Bliki: DomainSpecificLanguage. Retrieved September 15, 2010, from, http://www.martinfowler.com/bliki/DomainSpecificLanguage.html&lt;br /&gt;
&lt;br /&gt;
[3]Domain-specific Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/Domain-specific_language.&lt;br /&gt;
&lt;br /&gt;
[4]Jeff Barczewski. Creating DSL’s in Ruby. Retrieved September 16, 2010 from http://inspiredhorizons.com/presentations/CreatingDSLsInRuby-060926.pdf&lt;br /&gt;
&lt;br /&gt;
[5]Venkat Subramaniam, Creating DSL’s in java. Retrieved September 16, 2010 from http://www.java2s.com/Article/Java/Development/DSL.htm&lt;br /&gt;
&lt;br /&gt;
[6]Jamis Buck. Writing Domain Specific Languages. Retrieved September 16, 2010 from http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages&lt;br /&gt;
&lt;br /&gt;
[7]Alex Ruiz, Jeff Bay.  An Approach to Internal Domain-Specific Languages in Java Retreved September 16, 2010 from http://www.infoq.com/articles/internal-dsls-java&lt;br /&gt;
&lt;br /&gt;
[8]ColdFusion Markup Language. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/ColdFusion_Markup_Language&lt;br /&gt;
&lt;br /&gt;
[9]MOO. Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/MOO&lt;br /&gt;
&lt;br /&gt;
[10]Freedom Dumlao. Introducing Kiddo: A Ruby DSL for building simple WPF and Silverlight applications. Retrieved September 15, 2010 from http://weblogs.asp.net/freedomdumlao/archive/2010/05/27/introducing-kiddo-a-ruby-dsl-for-building-simple-wpf-and-silverlight-applications.aspx&lt;br /&gt;
&lt;br /&gt;
[11]LPC (programming language). Wikipedia, the Free Encyclopedia. Retrieved September 15, 2010 from http://en.wikipedia.org/wiki/LPC_(programming_language)&lt;br /&gt;
&lt;br /&gt;
[12]Kermeta Language Overview. Retrieved September 15, 2010 from https://www.kermeta.org/docs/KerMeta-MetaModel.pdf&lt;/div&gt;</summary>
		<author><name>Sumi2509</name></author>
	</entry>
</feed>