<?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=Jechen</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=Jechen"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Jechen"/>
	<updated>2026-06-03T04:37:48Z</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/ch7_7d_EC&amp;diff=43394</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=43394"/>
		<updated>2010-12-09T20:05:57Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. Low coupling means that components such as the client and provider as shown below do not or have very little direct interaction with each other. For example, the client object would not call direclty a method in the provider object, and vise versa. Because of this, we can reuse the provider or client classes, since they are not direclty tied in with each other.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not directly interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. It is easier for the programmers to develop when components are divided, rather than trying to code everything in one monolithic program. When components are divided there is less coupling between components, and it makes it easier to change or add functionality.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using Indirection to Mediate Several Objects&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be used to centralize the communication between several related objects [7].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Imagine if we had to make this robot walk. We call &amp;lt;i&amp;gt;LeftFoot&amp;lt;/i&amp;gt; to step forward, as well as &amp;lt;i&amp;gt;LeftArm&amp;lt;/i&amp;gt; to move up, then &amp;lt;i&amp;gt;LefttArm&amp;lt;/i&amp;gt; sends message to &amp;lt;i&amp;gt;RightFoot&amp;lt;/i&amp;gt; to step up, which calls &amp;lt;i&amp;gt;RightArm&amp;lt;/i&amp;gt; to move up, which calls &amp;lt;i&amp;gt;LeftFoot&amp;lt;/i&amp;gt; to step forward, and repeat the process.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The objects would be calling each other through a series a direct calls and this requires lots of coordination between objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Trying to make robot walk like this?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Also, when we add another component, say &amp;lt;i&amp;gt;Head&amp;lt;/i&amp;gt;, we would have to change all other component objects in order add another component.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One way to relieve each class of the burden of having to know which message to pass to the next object, is the use of mediation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
By using a mediator, in this case, called &amp;lt;i&amp;gt;Controller&amp;lt;/i&amp;gt;, we can implement the walking procedure in the controller object and remove the direct calling between component objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Getting robot to walk using mediator&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;Controller&amp;lt;/i&amp;gt; object well now call the components, instead of components calling each other. This relieves each component from the burden of having to know how to interact with each other component. The code becomes much less, and less confusing. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Also, when we add additional components, we just change the code in &amp;lt;i&amp;gt;Controller&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Advantages of Indirection&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Reduces coupling between objects.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Code can be reused.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Code is more extensible and easier to maintain.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Reduces the amount of communications between objects.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Potentially more secure. Delegation can restrict access to unwanted functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Disadvantages of Indirection&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Mediator object itself could become too complex.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Potential performance issues if mediator is poorly implemented or too complex.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change [6]. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Sources&amp;lt;/h1&amp;gt;&lt;br /&gt;
[1] Dale Skrien. Object-Oriented Design Using Java. pp. 196-197 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] [http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 Wikipedia - GRASP] &amp;lt;br&amp;gt;&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Delegation_pattern Wikipedia - Delegation Pattern] &amp;lt;br&amp;gt;&lt;br /&gt;
[4] Dave Thomas. Agile Web Development with Rails. pp. 11-14 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] Dr. Gehringer. Lecture notes 10. pp 12-13 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] [http://www.icaen.uiowa.edu/~kuhl/SoftEng/Slides10.pdf More GRASP Patterns] &amp;lt;br&amp;gt;&lt;br /&gt;
[7] Head First Design Patters. Eric Freeman &amp;amp; Elisabeth Freeman. pp. 622-623&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=43393</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=43393"/>
		<updated>2010-12-09T19:55:03Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. Low coupling means that components such as the client and provider as shown below do not or have very little direct interaction with each other. For example, the client object would not call direclty a method in the provider object, and vise versa. Because of this, we can reuse the provider or client classes, since they are not direclty tied in with each other.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not directly interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. It is easier for the programmers to develop when components are divided, rather than trying to code everything in one monolithic program. When components are divided there is less coupling between components, and it makes it easier to change or add functionality.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using Indirection to Mediate Several Objects&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be used to centralize the communication between several related objects [7].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Imagine if we had to make this robot walk. We call &amp;lt;i&amp;gt;LeftFoot&amp;lt;/i&amp;gt; to step forward, as well as &amp;lt;i&amp;gt;LeftArm&amp;lt;/i&amp;gt; to move up, then &amp;lt;i&amp;gt;LefttArm&amp;lt;/i&amp;gt; sends message to &amp;lt;i&amp;gt;RightFoot&amp;lt;/i&amp;gt; to step up, which calls &amp;lt;i&amp;gt;RightArm&amp;lt;/i&amp;gt; to move up, which calls &amp;lt;i&amp;gt;LeftFoot&amp;lt;/i&amp;gt; to step forward, and repeat the process.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The objects would be calling each other through a series a direct calls and this requires lots of coordination between objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Trying to make robot walk like this?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Also, when we add another component, say &amp;lt;i&amp;gt;Head&amp;lt;/i&amp;gt;, we would have to change all other component objects in order add another component.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One way to relieve each class of the burden of having to know which message to pass to the next object, is the use of mediation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
By using a mediator, in this case, called &amp;lt;i&amp;gt;Controller&amp;lt;/i&amp;gt;, we can implement the walking procedure in the controller object and remove the direct calling between component objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Getting robot to walk using mediator&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;Controller&amp;lt;/i&amp;gt; object well now call the components, instead of components calling each other. This relieves each component from the burden of having to know how to interact with each other component. The code becomes much less, and less confusing. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Also, when we add additional components, we just change the code in &amp;lt;i&amp;gt;Controller&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change [6]. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Sources&amp;lt;/h1&amp;gt;&lt;br /&gt;
[1] Dale Skrien. Object-Oriented Design Using Java. pp. 196-197 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] [http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 Wikipedia - GRASP] &amp;lt;br&amp;gt;&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Delegation_pattern Wikipedia - Delegation Pattern] &amp;lt;br&amp;gt;&lt;br /&gt;
[4] Dave Thomas. Agile Web Development with Rails. pp. 11-14 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] Dr. Gehringer. Lecture notes 10. pp 12-13 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] [http://www.icaen.uiowa.edu/~kuhl/SoftEng/Slides10.pdf More GRASP Patterns] &amp;lt;br&amp;gt;&lt;br /&gt;
[7] Head First Design Patters. Eric Freeman &amp;amp; Elisabeth Freeman. pp. 622-623&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=43392</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=43392"/>
		<updated>2010-12-09T19:52:33Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. Low coupling means that components such as the client and provider as shown below do not or have very little direct interaction with each other. For example, the client object would not call direclty a method in the provider object, and vise versa. Because of this, we can reuse the provider or client classes, since they are not direclty tied in with each other.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not directly interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. It is easier for the programmers to develop when components are divided, rather than trying to code everything in one monolithic program. When components are divided there is less coupling between components, and it makes it easier to change or add functionality.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Using Indirection to Mediate Several Objects&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be used to centralize the communication between several related objects [7].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Imagine if we had to make this robot walk. We call &amp;lt;i&amp;gt;LeftFoot&amp;lt;/i&amp;gt; to step forward, as well as &amp;lt;i&amp;gt;LeftArm&amp;lt;/i&amp;gt; to move up, then &amp;lt;i&amp;gt;LefttArm&amp;lt;/i&amp;gt; sends message to &amp;lt;i&amp;gt;RightFoot&amp;lt;/i&amp;gt; to step up, which calls &amp;lt;i&amp;gt;RightArm&amp;lt;/i&amp;gt; to move up, which calls &amp;lt;i&amp;gt;LeftFoot&amp;lt;/i&amp;gt; to step forward, and repeat the process.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The objects would be calling each other through a series a direct calls and this requires lots of coordination between objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Trying to make robot walk like this?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Also, when we add another component, say &amp;lt;i&amp;gt;Head&amp;lt;/i&amp;gt;, we would have to change all other component objects in order add another component.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One way to relieve each class of the burden of having to know which message to pass to the next object, is the use of mediation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
By using a mediator, in this case, called &amp;lt;i&amp;gt;Controller&amp;lt;/i&amp;gt;, we can implement the walking procedure in the controller object and remove the direct calling between component objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Getting robot to walk using mediator&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;Controller&amp;lt;/i&amp;gt; object well now call the components, instead of components calling each other. This relieves each component from the burden of having to know how to interact with each other component. The code becomes much less, and less confusing. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Also, when we add additional components, we just change the code in &amp;lt;i&amp;gt;Controller&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change [6]. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Sources&amp;lt;/h1&amp;gt;&lt;br /&gt;
[1] Dale Skrien. Object-Oriented Design Using Java. pp. 196-197 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] [http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 Wikipedia - GRASP] &amp;lt;br&amp;gt;&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Delegation_pattern Wikipedia - Delegation Pattern] &amp;lt;br&amp;gt;&lt;br /&gt;
[4] Dave Thomas. Agile Web Development with Rails. pp. 11-14 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] Dr. Gehringer. Lecture notes 10. pp 12-13 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] [http://www.icaen.uiowa.edu/~kuhl/SoftEng/Slides10.pdf More GRASP Patterns]&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig8.jpg&amp;diff=43391</id>
		<title>File:EC 7 fig8.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig8.jpg&amp;diff=43391"/>
		<updated>2010-12-09T19:45:54Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig7.jpg&amp;diff=43390</id>
		<title>File:EC 7 fig7.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig7.jpg&amp;diff=43390"/>
		<updated>2010-12-09T19:45:44Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=43389</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=43389"/>
		<updated>2010-12-09T19:17:48Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. Low coupling means that components such as the client and provider as shown below do not or have very little direct interaction with each other. For example, the client object would not call direclty a method in the provider object, and vise versa. Because of this, we can reuse the provider or client classes, since they are not direclty tied in with each other.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not directly interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. It is easier for the programmers to develop when components are divided, rather than trying to code everything in one monolithic program. When components are divided there is less coupling between components, and it makes it easier to change or add functionality.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change [6]. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Sources&amp;lt;/h1&amp;gt;&lt;br /&gt;
[1] Dale Skrien. Object-Oriented Design Using Java. pp. 196-197 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] [http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 Wikipedia - GRASP] &amp;lt;br&amp;gt;&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Delegation_pattern Wikipedia - Delegation Pattern] &amp;lt;br&amp;gt;&lt;br /&gt;
[4] Dave Thomas. Agile Web Development with Rails. pp. 11-14 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] Dr. Gehringer. Lecture notes 10. pp 12-13 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] [http://www.icaen.uiowa.edu/~kuhl/SoftEng/Slides10.pdf More GRASP Patterns]&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=43388</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=43388"/>
		<updated>2010-12-09T18:52:22Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. Low coupling means that components such as the client and provider as shown below do not or have very little direct interaction with each other. For example, the client object would not call direclty a method in the provider object, and vise versa. Because of this, we can reuse the provider or client classes, since they are not direclty tied in with each other.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change [6]. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Sources&amp;lt;/h1&amp;gt;&lt;br /&gt;
[1] Dale Skrien. Object-Oriented Design Using Java. pp. 196-197 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] [http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 Wikipedia - GRASP] &amp;lt;br&amp;gt;&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Delegation_pattern Wikipedia - Delegation Pattern] &amp;lt;br&amp;gt;&lt;br /&gt;
[4] Dave Thomas. Agile Web Development with Rails. pp. 11-14 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] Dr. Gehringer. Lecture notes 10. pp 12-13 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] [http://www.icaen.uiowa.edu/~kuhl/SoftEng/Slides10.pdf More GRASP Patterns]&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42345</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42345"/>
		<updated>2010-11-23T03:31:12Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change [6]. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Sources&amp;lt;/h1&amp;gt;&lt;br /&gt;
[1] Dale Skrien. Object-Oriented Design Using Java. pp. 196-197 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] [http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 Wikipedia - GRASP] &amp;lt;br&amp;gt;&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Delegation_pattern Wikipedia - Delegation Pattern] &amp;lt;br&amp;gt;&lt;br /&gt;
[4] Dave Thomas. Agile Web Development with Rails. pp. 11-14 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] Dr. Gehringer. Lecture notes 10. pp 12-13 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] [http://www.icaen.uiowa.edu/~kuhl/SoftEng/Slides10.pdf More GRASP Patterns]&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42344</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42344"/>
		<updated>2010-11-23T03:29:29Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change [6]. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Sources&amp;lt;/h1&amp;gt;&lt;br /&gt;
[1] Dale Skrien. Object-Oriented Design Using Java. pp. 196-197 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] [http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 Wikipedia - GRASP] &amp;lt;br&amp;gt;&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Delegation_pattern Wikipedia - Delegation Pattern] &amp;lt;br&amp;gt;&lt;br /&gt;
[4] Dave Thomas. Agile Web Development with Rails. pp. 11-14 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] Dr. Gehringer. Lecture notes 10. pp 12-13&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42343</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42343"/>
		<updated>2010-11-23T03:26:37Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Sources&amp;lt;/h1&amp;gt;&lt;br /&gt;
[1] Dale Skrien. Object-Oriented Design Using Java. pp. 196-197 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] [http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 Wikipedia - GRASP] &amp;lt;br&amp;gt;&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Delegation_pattern Wikipedia - Delegation Pattern] &amp;lt;br&amp;gt;&lt;br /&gt;
[4] Dave Thomas. Agile Web Development with Rails. pp. 11-14 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] Dr. Gehringer. Lecture notes 10. pp 12-13&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42342</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42342"/>
		<updated>2010-11-23T03:26:23Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Sources&amp;lt;/h1&amp;gt;&lt;br /&gt;
[1] Dale Skrien. Object-Oriented Design Using Java. pp. 196-197 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] [http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 Wikipedia - GRASP] &amp;lt;br&amp;gt;&lt;br /&gt;
[3] [http://en.wikipedia.org/wiki/Delegation_pattern Wikipedia - Delegation Pattern] &amp;lt;br&amp;gt;&lt;br /&gt;
[4] Dave Thomas. Agile Web Development with Rails. pp. 11-14&lt;br /&gt;
[5] Dr. Gehringer. Lecture notes 10. pp 12-13&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42341</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42341"/>
		<updated>2010-11-23T03:19:33Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42340</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42340"/>
		<updated>2010-11-23T03:18:57Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
&lt;br /&gt;
     private RobotController rc;&lt;br /&gt;
&lt;br /&gt;
     public SafeRobotController {&lt;br /&gt;
	rc = new RobotController();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
	rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
	rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42339</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42339"/>
		<updated>2010-11-23T03:16:45Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
	private RobotController rc;&lt;br /&gt;
	public SafeRobotController {&lt;br /&gt;
		rc = new RobotController();&lt;br /&gt;
	}&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
		rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
		rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
		rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42338</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42338"/>
		<updated>2010-11-23T03:14:56Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
	private RobotController rc;&lt;br /&gt;
	public SafeRobotController {&lt;br /&gt;
		rc = new RobotController();&lt;br /&gt;
	}&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
		rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
		rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
		rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42336</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42336"/>
		<updated>2010-11-23T03:14:01Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
	private RobotController rc;&lt;br /&gt;
	public SafeRobotController {&lt;br /&gt;
		rc = new RobotController();&lt;br /&gt;
	}&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
		rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
		rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
		rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42335</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42335"/>
		<updated>2010-11-23T03:12:50Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
&lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	 rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
	private RobotController rc;&lt;br /&gt;
	public SafeRobotController {&lt;br /&gt;
		rc = new RobotController();&lt;br /&gt;
	}&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
		rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
		rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
		rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42334</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42334"/>
		<updated>2010-11-23T03:11:37Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	   rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
	private RobotController rc;&lt;br /&gt;
	public SafeRobotController {&lt;br /&gt;
		rc = new RobotController();&lt;br /&gt;
	}&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
		rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
		rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
		rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42333</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42333"/>
		<updated>2010-11-23T03:11:02Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	   rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
	private RobotController rc;&lt;br /&gt;
	public SafeRobotController {&lt;br /&gt;
		rc = new RobotController();&lt;br /&gt;
	}&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
		rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
		rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
		rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42331</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42331"/>
		<updated>2010-11-23T03:10:07Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	   rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
	private RobotController rc;&lt;br /&gt;
	public SafeRobotController {&lt;br /&gt;
		rc = new RobotController();&lt;br /&gt;
	}&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
		rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
		rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
		rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42330</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42330"/>
		<updated>2010-11-23T03:08:29Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	   rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Indirection through Delegation&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Indirection can also be achieved through the use of delegation. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To see this, lets assume that we are building a robot and need to implement robot functions. An existing class called &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; already exists and has the functions we desire and then some that we don't want.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;UML diagram for existing RobotController class&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {     &lt;br /&gt;
     void say(String s) { &lt;br /&gt;
       // Some complex code for robot to say 's' &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
	 // Some complex code to walk&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
       // Some complex code to grab nearest item	&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Code to self destruct robot&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Code to fire weapon&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class contains all of the functionality that we desire, except we don't want &amp;lt;i&amp;gt;selfDestruct()&amp;lt;/i&amp;gt; nor &amp;lt;i&amp;gt;fireWeapon()&amp;lt;/i&amp;gt; methods! &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
One solution would be to copy over this class to create a new class, and just delete the unwanted methods. However, this involves copying over a lot of complex code, and if that code were to change, we would need to change it in the new class as well. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution would be to extend this class and just override the unwanted methods with error message or simply just do nothing and return. For example, the following implementation could be done.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController extends RobotController {     &lt;br /&gt;
     void selfDestruct()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void fireWeapon()&lt;br /&gt;
       // Don't want this, so just return&lt;br /&gt;
	 return;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Extending and overriding unsafe methods in RobotController&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This seems like an elegant solution, however, what happens when someone adds more functionality to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; such as &amp;lt;i&amp;gt;smashObject()&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;becomeSelfAware()&amp;lt;/i&amp;gt;. Unfortunately, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; will inherit these methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Will SafeRobotContoller still be “safe” when the RobotController class changes to the above?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Another solution is to use indirection and not directly involve the &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; class. Using delegation, we can isolate the class and selectively implement the desired functionality. The code below illustrates how this can be achieved.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SafeRobotController {     &lt;br /&gt;
	private RobotController rc;&lt;br /&gt;
	public SafeRobotController {&lt;br /&gt;
		rc = new RobotController();&lt;br /&gt;
	}&lt;br /&gt;
     void say(String s) { &lt;br /&gt;
		rc.say(s);&lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void walk() {&lt;br /&gt;
		rc.walk();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     void grab() {&lt;br /&gt;
		rc.grab();&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here, the &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; instantiates a &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; object and calls only the desired methods. Even if some evil programmer adds ill methods to &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt;, they cannot be accessed by &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Essentially, we have added indirection between &amp;lt;i&amp;gt;SafeRobotController&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; through the use of a mediator object. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Summary&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The indirection pattern is a powerful design pattern that helps the software designer implement good design by maximizing code reuse and extensibility. In the examples shown above, indirection reduces the coupling between classes or components that are likely to change. By doing so, one could reuse or extend one component without having to affect the other.  &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42328</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42328"/>
		<updated>2010-11-23T03:00:31Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	   rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that we implement an online music store using Rails and the MVC architecture. A typical use case is illustrated below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  User enters http://my.url.com and view is displayed. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  User enters “beatles” in search box. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Search parameter is passed to controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Controller accesses songs database in Model to search for query. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;5.  Model returns result to Controller. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;6.  Controller creates view to display results. &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From this use case, the separation between view and model is illustrated. The Model does not interact with the View, and the View does not directly interact with the Model. The Controller acts as a mediator between View and Model.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of this approach is extensibility and maintainability. When View and Models are separated, they can be reused again. Whereas, if the Model and View where combined, the implementation could only be used for this application. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, if we wanted to create an online bookstore, we could reuse the same View components from the online music store, with some minimal changes. On the other hand, if we did not use MVC and the indirection pattern, and the View was tied in with the music Model, we probably could not easily reuse the code.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42326</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42326"/>
		<updated>2010-11-23T02:56:42Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	   rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Another Example: Ruby on Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Another example of where the indirection pattern is used is the system design architecture used for web development with Ruby on Rails. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Rails uses the MVC architecture, where applications are separated into three components: model, view, and controller [4].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
To briefly describe the tasks of the components [4]:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Model&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Responsible for maintaining state of application. Includes data storage, generally in the form a database.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;View&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Provides the user interface for input and output.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Controller&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Orchestrates input from view to model and provide output to view.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The MVC architecture used in Rails&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42325</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42325"/>
		<updated>2010-11-23T02:52:53Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	   rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The RobotPhrases object instantiates a RobotController object to act as a mediator to access functionality in RobotController.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
From the main class, it appears that our new &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class has implemented the functionality to make the robot speak the specific words, when in fact, we have used a mediator object to do the work. Basically, we have passed of the duty to someone/something else [3].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Specifically, the mediator object is the &amp;lt;i&amp;gt;RobotController r&amp;lt;/i&amp;gt; object created in the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; that mediates the action between the two classes. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The advantage of the indirection pattern in this case, is that the &amp;lt;i&amp;gt;RobotPhrases&amp;lt;/i&amp;gt; class is much easier to implement since we did not have to put complex, low-level robot code in the class. Also, when the speaking functionality of the robot changes, we only need to modify the say function in &amp;lt;i&amp;gt;RobotController&amp;lt;/i&amp;gt; and not in the &amp;lt;i&amp;gt;sayHello&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;sayGoodbye&amp;lt;/i&amp;gt; functions in the new class. This makes maintainability and code reuse much greater.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42324</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42324"/>
		<updated>2010-11-23T02:49:18Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Example of Indirection Pattern&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Let's say we have a talking robot, and the functionality to get the robot to say a specified string has already been implemented in the code below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotController {&lt;br /&gt;
     void say(String s) {&lt;br /&gt;
       // Some complex code for robot to say 's'&lt;br /&gt;
     }&lt;br /&gt;
 }&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We want to create another class that has methods to say common phrases like “hello” and “goodbye”.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class RobotPhrases { &lt;br /&gt;
     RobotController r = new RobotController();&lt;br /&gt;
     &lt;br /&gt;
     void sayHello() { &lt;br /&gt;
       r.say(“Hello”); &lt;br /&gt;
     } &lt;br /&gt;
     &lt;br /&gt;
     void sayGoodbye() {&lt;br /&gt;
	 r.say(“Goodbye”);&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the main function, we would do the following:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         RobotPhrases rp = new RobotPhrases();&lt;br /&gt;
         rp.sayHello();&lt;br /&gt;
	   rp.sayGoodbye();&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42322</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42322"/>
		<updated>2010-11-23T02:42:41Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_7_fig1.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;The relationship between classes in indirection pattern.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the indirection pattern, a mediator class acts as a proxy in between the client and provider classes. The client interfaces with the mediator, not to the provider. The client accesses functionality from the provider through the mediator. Therefore, the relationship between the client and provider is indirect. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The indirection pattern is seen all of the time. Anytime the programmer accesses an API, be it for operating system call, graphics library call, the programmer is interfacing with a mediator that provides functionality on behalf of the client to some lower-level hardware functionality provided by the operating system or graphics library, etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A simple example of the indirection pattern is illustrated below.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig6.jpg&amp;diff=42321</id>
		<title>File:EC 7 fig6.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig6.jpg&amp;diff=42321"/>
		<updated>2010-11-23T02:40:35Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig5.jpg&amp;diff=42320</id>
		<title>File:EC 7 fig5.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig5.jpg&amp;diff=42320"/>
		<updated>2010-11-23T02:40:22Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig4.jpg&amp;diff=42319</id>
		<title>File:EC 7 fig4.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig4.jpg&amp;diff=42319"/>
		<updated>2010-11-23T02:40:13Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig3.jpg&amp;diff=42318</id>
		<title>File:EC 7 fig3.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig3.jpg&amp;diff=42318"/>
		<updated>2010-11-23T02:40:03Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig2.jpg&amp;diff=42317</id>
		<title>File:EC 7 fig2.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig2.jpg&amp;diff=42317"/>
		<updated>2010-11-23T02:39:53Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig1.jpg&amp;diff=42316</id>
		<title>File:EC 7 fig1.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:EC_7_fig1.jpg&amp;diff=42316"/>
		<updated>2010-11-23T02:39:40Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42315</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42315"/>
		<updated>2010-11-23T02:32:59Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;The Indirection Pattern&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The &amp;lt;b&amp;gt;Indirection Pattern&amp;lt;/b&amp;gt; supports low coupling and code reuse by using a mediator object in between two objects. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
A very simple diagram below illustrates the relationship between objects in the indirection pattern.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42313</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42313"/>
		<updated>2010-11-23T02:30:53Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;GRASP&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;GRASP&amp;lt;/b&amp;gt; stands for General Responsibility Assignment Software Patterns and consists of several design patterns for assigning responsibility and relationships between classes and objects in object-oriented design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some of the patterns in GRASP include Information Expert, Creator, Controller, Low Coupling, High Cohesion, Polymorphism, Pure Fabrication, Indirection, and Protected Variations [2].&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this article, we will focus on the Indirection Pattern.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42310</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7d EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7d_EC&amp;diff=42310"/>
		<updated>2010-11-23T02:27:53Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;Introduction to Design Patterns&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A &amp;lt;b&amp;gt;design pattern&amp;lt;/b&amp;gt; is a type of technique that developers use to create good software design. There are many design patterns available, and they share common benefits. The benefits of design patterns include [1] :&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;1.  Code is more accessible and understandable.&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2.  Use of common vocabulary to help collaboration among developers.&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;3.  Helps people understand the system more quickly.&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;4.  Easier to modify system.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35877</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35877"/>
		<updated>2010-09-21T20:33:06Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Figure 1: Simple case of inheritance&amp;lt;/i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Figure 2: Case of multiple inheritance&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Figure 3: Multiple inheritance allows more flexible hierarchies.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Figure 4: Single inheritance limits hierarchy.&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Disadvantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The reality is that several implementation issues keep it from being incorporated into some languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some key issues include name and method collision&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;, the diamond problem&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;, and conceptual difficulties and understandability problems&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Name collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A subclass that inherits from several parents classes with the same variable names will have name collisions.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Figure 5: Which x value will print_x() method print?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Method collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Figure 6: Which show() method will Package inherit?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;The Diamond Problem&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The diamond problem occurs when the direct base classes are themselves derived from another class&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Figure 7: What sound should the CowDuckHybird make?&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's define two classes called &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot;. They both inherit from &amp;quot;Animal&amp;quot;. The makeSound() method is inherited in both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; classes. The &amp;quot;Cow&amp;quot; class defines the method to return &amp;quot;moo&amp;quot;, while the &amp;quot;Duck&amp;quot; class redefines the method to return &amp;quot;quack&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that through some strange breeding, a cow and duck breed to create a &amp;quot;CowDuckHybird&amp;quot; classes which inherits from both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; parents. When the makeSound() method is called on a &amp;quot;CowDuckHybrid&amp;quot; object, what sound will make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In a multiple inheritance supporting language such as C++, the &amp;quot;CowDuckHybrid&amp;quot; class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The code implementation in C++ illustrates this kind of problem below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;quack&amp;quot;;	// redefine method	&amp;lt;br&amp;gt;	&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// illegal statement - compiler will complain&amp;lt;br&amp;gt;&lt;br /&gt;
a.Cow::makeSound();		// outputs &amp;quot;moo&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
a.Duck::makeSound()		// outputs &amp;quot;quack&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In C++, the programmer must explicitly specify which duplicate method to run. If not, the compiler will throw an error. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
There is also additional functionally in C++ to address the issue of diamond-like hierarchies. The concept of virtual base classes can make a class appear to be virtual or transparent. By making the two intermediate classes &amp;quot;Duck&amp;quot; and &amp;quot;Cow&amp;quot; virtual, we tell the compiler not to make duplicate variables or methods in &amp;quot;CowDuckHybrid&amp;quot; or any classes derived from the two virtual classes&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The implementation with virtual classes is shown below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;quack&amp;quot;;	// redefine method&amp;lt;br&amp;gt;		&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// now legal, outputs &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Conceptual Difficulties&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For large projects, where there are several layers of classes and inheritance from many classes, the amount of conflicts can become high and resolving those conflicts can become very tedious. In languages such as C++, where multiple inheritance is supported, the programmer must explicitly indicate which parent class method or variable to use, or resolve the conflict by using the virtual keyword functionality. This, however, requires the programmer to trace back to the parent classes to determine what course of action to take. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Without good organization of the classes and understanding of how they are organized, multiple inheritance can become more trouble than it is worth. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Alternatives to Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Many object-oriented languages do not use multiple inheritance for the reasons mentioned above. One of those languages is Java. Part of the philosophy of the Java language is to be simple and ease of use, and the creators of Java decided not to have support for multiple inheritance. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Alternative in Java&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Java can simulate multiple inheritance through the use of multiple interfaces&amp;lt;sup&amp;gt;6,7&amp;lt;/sup&amp;gt;. For example, the hierarchy below can be implemented in Java.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Figure 8: Implementing multiple inheritance in Java&amp;lt;/i&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
interface Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void shootWeb();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void climbWalls();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
interface Man {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void talk();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void run();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman implements Spider, Man {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;public void shootWeb() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;public void climbWalls() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;public void talk() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;public void run() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Spider s = new Spiderman();			// all of these statements are valid&amp;lt;br&amp;gt;&lt;br /&gt;
Man m = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
Spiderman sm = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Note that in this implementation, we would have to implement the &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; methods in the &amp;quot;Spiderman&amp;quot; class. We cannot include code in the interface, as this violates the concept of interfaces in Java. We loose code re-usability, but persevered polymorphic relationship. If we created another class called &amp;quot;Venom&amp;quot; and implemented &amp;quot;Spider&amp;quot; class, we would have to duplicate the code. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand, if we don't care about polymorphism and just don't want to duplicate code, we can just compost the code like the following.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void shootWeb { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void climbWalls { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Man {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void talk() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void run() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;Man m;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// Spiderman specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Venom {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// Venom specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here both &amp;quot;Spiderman&amp;quot; and &amp;quot;Venom&amp;quot; can reuse the &amp;quot;Spider&amp;quot; class code without having to implement it twice. However, &amp;quot;Spiderman&amp;quot; cannot be considered a &amp;quot;Spider&amp;quot; nor a &amp;quot;Man&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Both solutions shown are not really multiple inheritance in the true sense, but they can get the job done, depending on the specifications of the design.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Modules and Mixins&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Ruby can simulate multiple inheritance using modules. In Ruby the code appears as follows.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
module Spider&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;def shootWeb()&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;def climbWalls()&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
module Man&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;def talk()&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;def run()&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Spiderman&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;include Spider&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;include Man&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Ruby, the module code is simply &amp;quot;mixed in&amp;quot; to the specified class, basically a copy and paste function. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here are some key rules to modules in Ruby.&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	1. It is not a class&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	2. It cannot be instantiated&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	3. It can be mixed into a class&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When modules are mixed into a class&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;,&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	1. There is no hierarchy. Even though we included &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; in class &amp;quot;Spiderman&amp;quot;, &amp;quot;Spiderman&amp;quot; object cannot be polymorphized into &amp;quot;Spider&amp;quot; nor &amp;quot;Man&amp;quot;. That is, &amp;quot;Spiderman&amp;quot; is not a subclass of &amp;quot;Spider&amp;quot; or &amp;quot;Man&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	2. Multiple modules can be mixed-in to a class.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
1. Object-Oriented Design Using Java. Dale Skrien. 2009. pp. 10-30&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Multiple_inheritance Wikipedia - Multiple Inheritance]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. CSC517 Lec. 9 Notes. Dr. Gehringer. 2010 &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Diamond_problem Wikipedia - Diamond Problem]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
5. Beginning Visual C++. Ivor Horton. 1998. pp. 422-424.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
6. [http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html JavaWorld - Designing with interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
7. [http://www.java2s.com/Code/Java/Language-Basics/Implementmultipleinterfaces.htm Implement multiple interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
8. [http://www.rubyfleebie.com/an-introduction-to-modules-part-2/ An Introduction to modules]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35873</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35873"/>
		<updated>2010-09-21T20:29:36Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Disadvantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The reality is that several implementation issues keep it from being incorporated into some languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some key issues include name and method collision&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;, the diamond problem&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;, and conceptual difficulties and understandability problems&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Name collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A subclass that inherits from several parents classes with the same variable names will have name collisions.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 5: Which x value will print_x() method print?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Method collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 6: Which show() method will Package inherit?&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;The Diamond Problem&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The diamond problem occurs when the direct base classes are themselves derived from another class&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 7: What sound should the CowDuckHybird make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's define two classes called &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot;. They both inherit from &amp;quot;Animal&amp;quot;. The makeSound() method is inherited in both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; classes. The &amp;quot;Cow&amp;quot; class defines the method to return &amp;quot;moo&amp;quot;, while the &amp;quot;Duck&amp;quot; class redefines the method to return &amp;quot;quack&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that through some strange breeding, a cow and duck breed to create a &amp;quot;CowDuckHybird&amp;quot; classes which inherits from both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; parents. When the makeSound() method is called on a &amp;quot;CowDuckHybrid&amp;quot; object, what sound will make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In a multiple inheritance supporting language such as C++, the &amp;quot;CowDuckHybrid&amp;quot; class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The code implementation in C++ illustrates this kind of problem below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;quack&amp;quot;;	// redefine method	&amp;lt;br&amp;gt;	&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// illegal statement - compiler will complain&amp;lt;br&amp;gt;&lt;br /&gt;
a.Cow::makeSound();		// outputs &amp;quot;moo&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
a.Duck::makeSound()		// outputs &amp;quot;quack&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In C++, the programmer must explicitly specify which duplicate method to run. If not, the compiler will throw an error. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
There is also additional functionally in C++ to address the issue of diamond-like hierarchies. The concept of virtual base classes can make a class appear to be virtual or transparent. By making the two intermediate classes &amp;quot;Duck&amp;quot; and &amp;quot;Cow&amp;quot; virtual, we tell the compiler not to make duplicate variables or methods in &amp;quot;CowDuckHybrid&amp;quot; or any classes derived from the two virtual classes&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The implementation with virtual classes is shown below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;return &amp;quot;quack&amp;quot;;	// redefine method&amp;lt;br&amp;gt;		&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// now legal, outputs &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Conceptual Difficulties&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For large projects, where there are several layers of classes and inheritance from many classes, the amount of conflicts can become high and resolving those conflicts can become very tedious. In languages such as C++, where multiple inheritance is supported, the programmer must explicitly indicate which parent class method or variable to use, or resolve the conflict by using the virtual keyword functionality. This, however, requires the programmer to trace back to the parent classes to determine what course of action to take. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Without good organization of the classes and understanding of how they are organized, multiple inheritance can become more trouble than it is worth. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Alternatives to Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Many object-oriented languages do not use multiple inheritance for the reasons mentioned above. One of those languages is Java. Part of the philosophy of the Java language is to be simple and ease of use, and the creators of Java decided not to have support for multiple inheritance. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Alternative in Java&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Java can simulate multiple inheritance through the use of multiple interfaces&amp;lt;sup&amp;gt;6,7&amp;lt;/sup&amp;gt;. For example, the hierarchy below can be implemented in Java.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 8: Implementing multiple inheritance in Java&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
interface Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void shootWeb();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void climbWalls();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
interface Man {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void talk();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void run();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman implements Spider, Man {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;public void shootWeb() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;public void climbWalls() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;public void talk() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;public void run() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Spider s = new Spiderman();			// all of these statements are valid&amp;lt;br&amp;gt;&lt;br /&gt;
Man m = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
Spiderman sm = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Note that in this implementation, we would have to implement the &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; methods in the &amp;quot;Spiderman&amp;quot; class. We cannot include code in the interface, as this violates the concept of interfaces in Java. We loose code re-usability, but persevered polymorphic relationship. If we created another class called &amp;quot;Venom&amp;quot; and implemented &amp;quot;Spider&amp;quot; class, we would have to duplicate the code. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand, if we don't care about polymorphism and just don't want to duplicate code, we can just compost the code like the following.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void shootWeb { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void climbWalls { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Man {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void talk() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;void run() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;Man m;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// Spiderman specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Venom {&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// Venom specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here both &amp;quot;Spiderman&amp;quot; and &amp;quot;Venom&amp;quot; can reuse the &amp;quot;Spider&amp;quot; class code without having to implement it twice. However, &amp;quot;Spiderman&amp;quot; cannot be considered a &amp;quot;Spider&amp;quot; nor a &amp;quot;Man&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Both solutions shown are not really multiple inheritance in the true sense, but they can get the job done, depending on the specifications of the design.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Modules and Mixins&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Ruby can simulate multiple inheritance using modules. In Ruby the code appears as follows.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
module Spider&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;def shootWeb()&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;def climbWalls()&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
module Man&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;def talk()&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;def run()&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Spiderman&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;include Spider&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;include Man&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// ...&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Ruby, the module code is simply &amp;quot;mixed in&amp;quot; to the specified class, basically a copy and paste function. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here are some key rules to modules in Ruby.&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	1. It is not a class&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	2. It cannot be instantiated&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	3. It can be mixed into a class&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When modules are mixed into a class&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;,&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	1. There is no hierarchy. Even though we included &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; in class &amp;quot;Spiderman&amp;quot;, &amp;quot;Spiderman&amp;quot; object cannot be polymorphized into &amp;quot;Spider&amp;quot; nor &amp;quot;Man&amp;quot;. That is, &amp;quot;Spiderman&amp;quot; is not a subclass of &amp;quot;Spider&amp;quot; or &amp;quot;Man&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	2. Multiple modules can be mixed-in to a class.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
1. Object-Oriented Design Using Java. Dale Skrien. 2009. pp. 10-30&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Multiple_inheritance Wikipedia - Multiple Inheritance]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. CSC517 Lec. 9 Notes. Dr. Gehringer. 2010 &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Diamond_problem Wikipedia - Diamond Problem]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
5. Beginning Visual C++. Ivor Horton. 1998. pp. 422-424.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
6. [http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html JavaWorld - Designing with interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
7. [http://www.java2s.com/Code/Java/Language-Basics/Implementmultipleinterfaces.htm Implement multiple interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
8. [http://www.rubyfleebie.com/an-introduction-to-modules-part-2/ An Introduction to modules]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35871</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35871"/>
		<updated>2010-09-21T20:23:57Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Disadvantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The reality is that several implementation issues keep it from being incorporated into some languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some key issues include name and method collision&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;, the diamond problem&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;, and conceptual difficulties and understandability problems&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Name collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A subclass that inherits from several parents classes with the same variable names will have name collisions.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 5: Which x value will print_x() method print?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Method collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 6: Which show() method will Package inherit?&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;The Diamond Problem&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The diamond problem occurs when the direct base classes are themselves derived from another class&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 7: What sound should the CowDuckHybird make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's define two classes called &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot;. They both inherit from &amp;quot;Animal&amp;quot;. The makeSound() method is inherited in both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; classes. The &amp;quot;Cow&amp;quot; class defines the method to return &amp;quot;moo&amp;quot;, while the &amp;quot;Duck&amp;quot; class redefines the method to return &amp;quot;quack&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that through some strange breeding, a cow and duck breed to create a &amp;quot;CowDuckHybird&amp;quot; classes which inherits from both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; parents. When the makeSound() method is called on a &amp;quot;CowDuckHybrid&amp;quot; object, what sound will make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In a multiple inheritance supporting language such as C++, the &amp;quot;CowDuckHybrid&amp;quot; class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The code implementation in C++ illustrates this kind of problem below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method	&amp;lt;br&amp;gt;	&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// illegal statement - compiler will complain&amp;lt;br&amp;gt;&lt;br /&gt;
a.Cow::makeSound();		// outputs &amp;quot;moo&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
a.Duck::makeSound()		// outputs &amp;quot;quack&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In C++, the programmer must explicitly specify which duplicate method to run. If not, the compiler will throw an error. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
There is also additional functionally in C++ to address the issue of diamond-like hierarchies. The concept of virtual base classes can make a class appear to be virtual or transparent. By making the two intermediate classes &amp;quot;Duck&amp;quot; and &amp;quot;Cow&amp;quot; virtual, we tell the compiler not to make duplicate variables or methods in &amp;quot;CowDuckHybrid&amp;quot; or any classes derived from the two virtual classes&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The implementation with virtual classes is shown below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method&amp;lt;br&amp;gt;		&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// now legal, outputs &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Conceptual Difficulties&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For large projects, where there are several layers of classes and inheritance from many classes, the amount of conflicts can become high and resolving those conflicts can become very tedious. In languages such as C++, where multiple inheritance is supported, the programmer must explicitly indicate which parent class method or variable to use, or resolve the conflict by using the virtual keyword functionality. This, however, requires the programmer to trace back to the parent classes to determine what course of action to take. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Without good organization of the classes and understanding of how they are organized, multiple inheritance can become more trouble than it is worth. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Alternatives to Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Many object-oriented languages do not use multiple inheritance for the reasons mentioned above. One of those languages is Java. Part of the philosophy of the Java language is to be simple and ease of use, and the creators of Java decided not to have support for multiple inheritance. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Alternative in Java&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Java can simulate multiple inheritance through the use of multiple interfaces&amp;lt;sup&amp;gt;6,7&amp;lt;/sup&amp;gt;. For example, the hierarchy below can be implemented in Java.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 8: Implementing multiple inheritance in Java&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
interface Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb();&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
interface Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk();&amp;lt;br&amp;gt;&lt;br /&gt;
	void run();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman implements Spider, Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	public void shootWeb() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void climbWalls() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void talk() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void run() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Spider s = new Spiderman();			// all of these statements are valid&amp;lt;br&amp;gt;&lt;br /&gt;
Man m = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
Spiderman sm = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Note that in this implementation, we would have to implement the &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; methods in the &amp;quot;Spiderman&amp;quot; class. We cannot include code in the interface, as this violates the concept of interfaces in Java. We loose code re-usability, but persevered polymorphic relationship. If we created another class called &amp;quot;Venom&amp;quot; and implemented &amp;quot;Spider&amp;quot; class, we would have to duplicate the code. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand, if we don't care about polymorphism and just don't want to duplicate code, we can just compost the code like the following.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void run() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	Man m;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Spiderman specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Venom {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Venom specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here both &amp;quot;Spiderman&amp;quot; and &amp;quot;Venom&amp;quot; can reuse the &amp;quot;Spider&amp;quot; class code without having to implement it twice. However, &amp;quot;Spiderman&amp;quot; cannot be considered a &amp;quot;Spider&amp;quot; nor a &amp;quot;Man&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Both solutions shown are not really multiple inheritance in the true sense, but they can get the job done, depending on the specifications of the design.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Modules and Mixins&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Ruby can simulate multiple inheritance using modules. In Ruby the code appears as follows.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
module Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	def shootWeb()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def climbWalls()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
module Man&amp;lt;br&amp;gt;&lt;br /&gt;
	def talk()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def run()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Spiderman&amp;lt;br&amp;gt;&lt;br /&gt;
	include Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	include Man&amp;lt;br&amp;gt;&lt;br /&gt;
	// ...&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Ruby, the module code is simply &amp;quot;mixed in&amp;quot; to the specified class, basically a copy and paste function. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here are some key rules to modules in Ruby.&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
	1. It is not a class&amp;lt;br&amp;gt;&lt;br /&gt;
	2. It cannot be instantiated&amp;lt;br&amp;gt;&lt;br /&gt;
	3. It can be mixed into a class&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When modules are mixed into a class&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;,&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
	1. There is no hierarchy. Even though we included &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; in class &amp;quot;Spiderman&amp;quot;, &amp;quot;Spiderman&amp;quot; object cannot be polymorphized into &amp;quot;Spider&amp;quot; nor &amp;quot;Man&amp;quot;. That is, &amp;quot;Spiderman&amp;quot; is not a subclass of &amp;quot;Spider&amp;quot; or &amp;quot;Man&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
	2. Multiple modules can be mixed-in to a class.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
1. Object-Oriented Design Using Java. Dale Skrien. 2009. pp. 10-30&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Multiple_inheritance Wikipedia - Multiple Inheritance]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. CSC517 Lec. 9 Notes. Dr. Gehringer. 2010 &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Diamond_problem Wikipedia - Diamond Problem]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
5. Beginning Visual C++. Ivor Horton. 1998. pp. 422-424.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
6. [http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html JavaWorld - Designing with interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
7. [http://www.java2s.com/Code/Java/Language-Basics/Implementmultipleinterfaces.htm Implement multiple interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
8. [http://www.rubyfleebie.com/an-introduction-to-modules-part-2/ An Introduction to modules]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35867</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35867"/>
		<updated>2010-09-21T20:20:52Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Disadvantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The reality is that several implementation issues keep it from being incorporated into some languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some key issues include name and method collision&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;, the diamond problem&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;, and conceptual difficulties and understandability problems&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Name collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A subclass that inherits from several parents classes with the same variable names will have name collisions.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 5: Which x value will print_x() method print?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Method collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 6: Which show() method will Package inherit?&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;The Diamond Problem&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The diamond problem occurs when the direct base classes are themselves derived from another class&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 7: What sound should the CowDuckHybird make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's define two classes called &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot;. They both inherit from &amp;quot;Animal&amp;quot;. The makeSound() method is inherited in both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; classes. The &amp;quot;Cow&amp;quot; class defines the method to return &amp;quot;moo&amp;quot;, while the &amp;quot;Duck&amp;quot; class redefines the method to return &amp;quot;quack&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that through some strange breeding, a cow and duck breed to create a &amp;quot;CowDuckHybird&amp;quot; classes which inherits from both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; parents. When the makeSound() method is called on a &amp;quot;CowDuckHybrid&amp;quot; object, what sound will make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In a multiple inheritance supporting language such as C++, the &amp;quot;CowDuckHybrid&amp;quot; class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The code implementation in C++ illustrates this kind of problem below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method	&amp;lt;br&amp;gt;	&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// illegal statement - compiler will complain&amp;lt;br&amp;gt;&lt;br /&gt;
a.Cow::makeSound();		// outputs &amp;quot;moo&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
a.Duck::makeSound()		// outputs &amp;quot;quack&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In C++, the programmer must explicitly specify which duplicate method to run. If not, the compiler will throw an error. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
There is also additional functionally in C++ to address the issue of diamond-like hierarchies. The concept of virtual base classes can make a class appear to be virtual or transparent. By making the two intermediate classes &amp;quot;Duck&amp;quot; and &amp;quot;Cow&amp;quot; virtual, we tell the compiler not to make duplicate variables or methods in &amp;quot;CowDuckHybrid&amp;quot; or any classes derived from the two virtual classes&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The implementation with virtual classes is shown below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method&amp;lt;br&amp;gt;		&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// now legal, outputs &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Conceptual Difficulties&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For large projects, where there are several layers of classes and inheritance from many classes, the amount of conflicts can become high and resolving those conflicts can become very tedious. In languages such as C++, where multiple inheritance is supported, the programmer must explicitly indicate which parent class method or variable to use, or resolve the conflict by using the virtual keyword functionality. This, however, requires the programmer to trace back to the parent classes to determine what course of action to take. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Without good organization of the classes and understanding of how they are organized, multiple inheritance can become more trouble than it is worth. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Alternatives to Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Many object-oriented languages do not use multiple inheritance for the reasons mentioned above. One of those languages is Java. Part of the philosophy of the Java language is to be simple and ease of use, and the creators of Java decided not to have support for multiple inheritance. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Alternative in Java&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Java can simulate multiple inheritance through the use of multiple interfaces&amp;lt;sup&amp;gt;6,7&amp;lt;/sup&amp;gt;. For example, the hierarchy below can be implemented in Java.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 8: Implementing multiple inheritance in Java&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
interface Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb();&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
interface Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk();&amp;lt;br&amp;gt;&lt;br /&gt;
	void run();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman implements Spider, Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	public void shootWeb() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void climbWalls() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void talk() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void run() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Spider s = new Spiderman();			// all of these statements are valid&amp;lt;br&amp;gt;&lt;br /&gt;
Man m = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
Spiderman sm = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Note that in this implementation, we would have to implement the &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; methods in the &amp;quot;Spiderman&amp;quot; class. We cannot include code in the interface, as this violates the concept of interfaces in Java. We loose code re-usability, but persevered polymorphic relationship. If we created another class called &amp;quot;Venom&amp;quot; and implemented &amp;quot;Spider&amp;quot; class, we would have to duplicate the code. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand, if we don't care about polymorphism and just don't want to duplicate code, we can just compost the code like the following.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void run() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	Man m;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Spiderman specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Venom {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Venom specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here both &amp;quot;Spiderman&amp;quot; and &amp;quot;Venom&amp;quot; can reuse the &amp;quot;Spider&amp;quot; class code without having to implement it twice. However, &amp;quot;Spiderman&amp;quot; cannot be considered a &amp;quot;Spider&amp;quot; nor a &amp;quot;Man&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Both solutions shown are not really multiple inheritance in the true sense, but they can get the job done, depending on the specifications of the design.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Modules and Mixins&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Ruby can simulate multiple inheritance using modules. In Ruby the code appears as follows.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
module Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	def shootWeb()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def climbWalls()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
module Man&amp;lt;br&amp;gt;&lt;br /&gt;
	def talk()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def run()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Spiderman&amp;lt;br&amp;gt;&lt;br /&gt;
	include Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	include Man&amp;lt;br&amp;gt;&lt;br /&gt;
	// ...&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Ruby, the module code is simply &amp;quot;mixed in&amp;quot; to the specified class, basically a copy and paste function. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here are some key rules to modules in Ruby.&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
	1. It is not a class&amp;lt;br&amp;gt;&lt;br /&gt;
	2. It cannot be instantiated&amp;lt;br&amp;gt;&lt;br /&gt;
	3. It can be mixed into a class&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When modules are mixed into a class&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;,&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
	1. There is no hierarchy. Even though we included &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; in class &amp;quot;Spiderman&amp;quot;, &amp;quot;Spiderman&amp;quot; object cannot be polymorphized into &amp;quot;Spider&amp;quot; nor &amp;quot;Man&amp;quot;. That is, &amp;quot;Spiderman&amp;quot; is not a subclass of &amp;quot;Spider&amp;quot; or &amp;quot;Man&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
	2. Multiple modules can be mixed-in to a class.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
1. Object-Oriented Design Using Java. Dale Skrien. 2009. pp. 10-30&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Multiple_inheritance Wikipedia - Multiple Inheritance]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. CSC517 Lec. 9 Notes. Dr. Gehringer. 2010 &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Diamond_problem Wikipedia - Diamond Problem]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
5. Beginning Visual C++. Ivor Horton. 1998. pp. 422-424.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
6. [http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html JavaWorld - Designing with interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
7. [http://www.java2s.com/Code/Java/Language-Basics/Implementmultipleinterfaces.htm Implement multiple interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
8. [http://www.rubyfleebie.com/an-introduction-to-modules-part-2/ An Introduction to modules]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35860</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35860"/>
		<updated>2010-09-21T20:16:24Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Disadvantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The reality is that several implementation issues keep it from being incorporated into some languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some key issues include name and method collision&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;, the diamond problem&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;, and conceptual difficulties and understandability problems&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Name collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A subclass that inherits from several parents classes with the same variable names will have name collisions.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 5: Which x value will print_x() method print?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Method collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 6: Which show() method will Package inherit?&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;The Diamond Problem&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The diamond problem occurs when the direct base classes are themselves derived from another class&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 7: What sound should the CowDuckHybird make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's define two classes called &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot;. They both inherit from &amp;quot;Animal&amp;quot;. The makeSound() method is inherited in both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; classes. The &amp;quot;Cow&amp;quot; class defines the method to return &amp;quot;moo&amp;quot;, while the &amp;quot;Duck&amp;quot; class redefines the method to return &amp;quot;quack&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that through some strange breeding, a cow and duck breed to create a &amp;quot;CowDuckHybird&amp;quot; classes which inherits from both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; parents. When the makeSound() method is called on a &amp;quot;CowDuckHybrid&amp;quot; object, what sound will make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In a multiple inheritance supporting language such as C++, the &amp;quot;CowDuckHybrid&amp;quot; class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The code implementation in C++ illustrates this kind of problem below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method	&amp;lt;br&amp;gt;	&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// illegal statement - compiler will complain&amp;lt;br&amp;gt;&lt;br /&gt;
a.Cow::makeSound();		// outputs &amp;quot;moo&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
a.Duck::makeSound()		// outputs &amp;quot;quack&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In C++, the programmer must explicitly specify which duplicate method to run. If not, the compiler will throw an error. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
There is also additional functionally in C++ to address the issue of diamond-like hierarchies. The concept of virtual base classes can make a class appear to be virtual or transparent. By making the two intermediate classes &amp;quot;Duck&amp;quot; and &amp;quot;Cow&amp;quot; virtual, we tell the compiler not to make duplicate variables or methods in &amp;quot;CowDuckHybrid&amp;quot; or any classes derived from the two virtual classes&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The implementation with virtual classes is shown below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method&amp;lt;br&amp;gt;		&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// now legal, outputs &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Conceptual Difficulties&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For large projects, where there are several layers of classes and inheritance from many classes, the amount of conflicts can become high and resolving those conflicts can become very tedious. In languages such as C++, where multiple inheritance is supported, the programmer must explicitly indicate which parent class method or variable to use, or resolve the conflict by using the virtual keyword functionality. This, however, requires the programmer to trace back to the parent classes to determine what course of action to take. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Without good organization of the classes and understanding of how they are organized, multiple inheritance can become more trouble than it is worth. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Alternatives to Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Many object-oriented languages do not use multiple inheritance for the reasons mentioned above. One of those languages is Java. Part of the philosophy of the Java language is to be simple and ease of use, and the creators of Java decided not to have support for multiple inheritance. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Alternative in Java&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Java can simulate multiple inheritance through the use of multiple interfaces&amp;lt;sup&amp;gt;6,7&amp;lt;/sup&amp;gt;. For example, the hierarchy below can be implemented in Java.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 8: Implementing multiple inheritance in Java&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
interface Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb();&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
interface Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk();&amp;lt;br&amp;gt;&lt;br /&gt;
	void run();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman implements Spider, Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	public void shootWeb() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void climbWalls() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void talk() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void run() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Spider s = new Spiderman();			// all of these statements are valid&amp;lt;br&amp;gt;&lt;br /&gt;
Man m = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
Spiderman sm = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Note that in this implementation, we would have to implement the &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; methods in the &amp;quot;Spiderman&amp;quot; class. We cannot include code in the interface, as this violates the concept of interfaces in Java. We loose code re-usability, but persevered polymorphic relationship. If we created another class called &amp;quot;Venom&amp;quot; and implemented &amp;quot;Spider&amp;quot; class, we would have to duplicate the code. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand, if we don't care about polymorphism and just don't want to duplicate code, we can just compost the code like the following.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void run() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	Man m;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Spiderman specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Venom {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Venom specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here both &amp;quot;Spiderman&amp;quot; and &amp;quot;Venom&amp;quot; can reuse the &amp;quot;Spider&amp;quot; class code without having to implement it twice. However, &amp;quot;Spiderman&amp;quot; cannot be considered a &amp;quot;Spider&amp;quot; nor a &amp;quot;Man&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Both solutions shown are not really multiple inheritance in the true sense, but they can get the job done, depending on the specifications of the design.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Modules and Mixins&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Ruby can simulate multiple inheritance using modules. In Ruby the code appears as follows.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
module Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	def shootWeb()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def climbWalls()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
module Man&amp;lt;br&amp;gt;&lt;br /&gt;
	def talk()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def run()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Spiderman&amp;lt;br&amp;gt;&lt;br /&gt;
	include Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	include Man&amp;lt;br&amp;gt;&lt;br /&gt;
	// ...&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Ruby, the module code is simply &amp;quot;mixed in&amp;quot; to the specified class, basically a copy and paste function. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here are some key rules to modules in Ruby.&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
	1. It is not a class&amp;lt;br&amp;gt;&lt;br /&gt;
	2. It cannot be instantiated&amp;lt;br&amp;gt;&lt;br /&gt;
	3. It can be mixed into a class&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When modules are mixed into a class&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;,&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
	1. There is no hierarchy. Even though we included &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; in class &amp;quot;Spiderman&amp;quot;, &amp;quot;Spiderman&amp;quot; object cannot be polymorphized into &amp;quot;Spider&amp;quot; nor &amp;quot;Man&amp;quot;. That is, &amp;quot;Spiderman&amp;quot; is not a subclass of &amp;quot;Spider&amp;quot; or &amp;quot;Man&amp;quot;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
	2. Multiple modules can be mixed-in to a class.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;References&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
1. Object-Oriented Design Using Java. Dale Skrien. 2009. pp. 10-30&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
2. [http://en.wikipedia.org/wiki/Multiple_inheritance Wikipedia - Multiple Inheritance]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
3. CSC517 Lec. 9 Notes. Dr. Gehringer. 2010 &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Diamond_problem Wikipedia - Diamond Problem]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
5. Beginning Visual C++. Ivor Horton. 1998. pp. 422-424.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
6. [http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html JavaWorld - Designing with interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
7. [http://www.java2s.com/Code/Java/Language-Basics/Implementmultipleinterfaces.htm Implement multiple interfaces]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
8. [http://www.rubyfleebie.com/an-introduction-to-modules-part-2/ An Introduction to modules]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35857</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35857"/>
		<updated>2010-09-21T20:10:14Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Disadvantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The reality is that several implementation issues keep it from being incorporated into some languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some key issues include name and method collision&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;, the diamond problem&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;, and conceptual difficulties and understandability problems&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Name collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A subclass that inherits from several parents classes with the same variable names will have name collisions.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 5: Which x value will print_x() method print?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Method collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 6: Which show() method will Package inherit?&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;The Diamond Problem&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The diamond problem occurs when the direct base classes are themselves derived from another class&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 7: What sound should the CowDuckHybird make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's define two classes called &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot;. They both inherit from &amp;quot;Animal&amp;quot;. The makeSound() method is inherited in both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; classes. The &amp;quot;Cow&amp;quot; class defines the method to return &amp;quot;moo&amp;quot;, while the &amp;quot;Duck&amp;quot; class redefines the method to return &amp;quot;quack&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that through some strange breeding, a cow and duck breed to create a &amp;quot;CowDuckHybird&amp;quot; classes which inherits from both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; parents. When the makeSound() method is called on a &amp;quot;CowDuckHybrid&amp;quot; object, what sound will make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In a multiple inheritance supporting language such as C++, the &amp;quot;CowDuckHybrid&amp;quot; class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The code implementation in C++ illustrates this kind of problem below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method	&amp;lt;br&amp;gt;	&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// illegal statement - compiler will complain&amp;lt;br&amp;gt;&lt;br /&gt;
a.Cow::makeSound();		// outputs &amp;quot;moo&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
a.Duck::makeSound()		// outputs &amp;quot;quack&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In C++, the programmer must explicitly specify which duplicate method to run. If not, the compiler will throw an error. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
There is also additional functionally in C++ to address the issue of diamond-like hierarchies. The concept of virtual base classes can make a class appear to be virtual or transparent. By making the two intermediate classes &amp;quot;Duck&amp;quot; and &amp;quot;Cow&amp;quot; virtual, we tell the compiler not to make duplicate variables or methods in &amp;quot;CowDuckHybrid&amp;quot; or any classes derived from the two virtual classes&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The implementation with virtual classes is shown below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method&amp;lt;br&amp;gt;		&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// now legal, outputs &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Conceptual Difficulties&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For large projects, where there are several layers of classes and inheritance from many classes, the amount of conflicts can become high and resolving those conflicts can become very tedious. In languages such as C++, where multiple inheritance is supported, the programmer must explicitly indicate which parent class method or variable to use, or resolve the conflict by using the virtual keyword functionality. This, however, requires the programmer to trace back to the parent classes to determine what course of action to take. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Without good organization of the classes and understanding of how they are organized, multiple inheritance can become more trouble than it is worth. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Alternatives to Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Many object-oriented languages do not use multiple inheritance for the reasons mentioned above. One of those languages is Java. Part of the philosophy of the Java language is to be simple and ease of use, and the creators of Java decided not to have support for multiple inheritance. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Alternative in Java&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Java can simulate multiple inheritance through the use of multiple interfaces&amp;lt;sup&amp;gt;6,7&amp;lt;/sup&amp;gt;. For example, the hierarchy below can be implemented in Java.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 8: Implementing multiple inheritance in Java&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
interface Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb();&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
interface Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk();&amp;lt;br&amp;gt;&lt;br /&gt;
	void run();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman implements Spider, Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	public void shootWeb() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void climbWalls() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void talk() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void run() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Spider s = new Spiderman();			// all of these statements are valid&amp;lt;br&amp;gt;&lt;br /&gt;
Man m = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
Spiderman sm = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Note that in this implementation, we would have to implement the &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; methods in the &amp;quot;Spiderman&amp;quot; class. We cannot include code in the interface, as this violates the concept of interfaces in Java. We loose code re-usability, but persevered polymorphic relationship. If we created another class called &amp;quot;Venom&amp;quot; and implemented &amp;quot;Spider&amp;quot; class, we would have to duplicate the code. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand, if we don't care about polymorphism and just don't want to duplicate code, we can just compost the code like the following.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void run() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	Man m;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Spiderman specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Venom {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Venom specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here both &amp;quot;Spiderman&amp;quot; and &amp;quot;Venom&amp;quot; can reuse the &amp;quot;Spider&amp;quot; class code without having to implement it twice. However, &amp;quot;Spiderman&amp;quot; cannot be considered a &amp;quot;Spider&amp;quot; nor a &amp;quot;Man&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Both solutions shown are not really multiple inheritance in the true sense, but they can get the job done, depending on the specifications of the design.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Modules and Mixins&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Ruby can simulate multiple inheritance using modules. In Ruby the code appears as follows.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
module Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	def shootWeb()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def climbWalls()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
module Man&amp;lt;br&amp;gt;&lt;br /&gt;
	def talk()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def run()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Spiderman&amp;lt;br&amp;gt;&lt;br /&gt;
	include Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	include Man&amp;lt;br&amp;gt;&lt;br /&gt;
	// ...&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Ruby, the module code is simply &amp;quot;mixed in&amp;quot; to the specified class, basically a copy and paste function. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here are some key rules to modules in Ruby.&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;&lt;br /&gt;
	1. It is not a class&amp;lt;br&amp;gt;&lt;br /&gt;
	2. It cannot be instantiated&amp;lt;br&amp;gt;&lt;br /&gt;
	3. It can be mixed into a class&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When modules are mixed into a class&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;,&amp;lt;br&amp;gt;&lt;br /&gt;
	1. There is no hierarchy. Even though we included &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; in class &amp;quot;Spiderman&amp;quot;, &amp;quot;Spiderman&amp;quot; object cannot be polymorphized into &amp;quot;Spider&amp;quot; nor &amp;quot;Man&amp;quot;. That is, &amp;quot;Spiderman&amp;quot; is not a subclass of &amp;quot;Spider&amp;quot; or &amp;quot;Man&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	2. Multiple modules can be mixed-in to a class.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35854</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35854"/>
		<updated>2010-09-21T20:02:14Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Disadvantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The reality is that several implementation issues keep it from being incorporated into some languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some key issues include name and method collision&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;, the diamond problem&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;, and conceptual difficulties and understandability problems&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Name collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A subclass that inherits from several parents classes with the same variable names will have name collisions.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 5: Which x value will print_x() method print?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Method collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 6: Which show() method will Package inherit?&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;The Diamond Problem&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The diamond problem occurs when the direct base classes are themselves derived from another class&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 7: What sound should the CowDuckHybird make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's define two classes called &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot;. They both inherit from &amp;quot;Animal&amp;quot;. The makeSound() method is inherited in both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; classes. The &amp;quot;Cow&amp;quot; class defines the method to return &amp;quot;moo&amp;quot;, while the &amp;quot;Duck&amp;quot; class redefines the method to return &amp;quot;quack&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that through some strange breeding, a cow and duck breed to create a &amp;quot;CowDuckHybird&amp;quot; classes which inherits from both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; parents. When the makeSound() method is called on a &amp;quot;CowDuckHybrid&amp;quot; object, what sound will make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In a multiple inheritance supporting language such as C++, the &amp;quot;CowDuckHybrid&amp;quot; class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The code implementation in C++ illustrates this kind of problem below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method	&amp;lt;br&amp;gt;	&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// illegal statement - compiler will complain&amp;lt;br&amp;gt;&lt;br /&gt;
a.Cow::makeSound();		// outputs &amp;quot;moo&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
a.Duck::makeSound()		// outputs &amp;quot;quack&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In C++, the programmer must explicitly specify which duplicate method to run. If not, the compiler will throw an error. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
There is also additional functionally in C++ to address the issue of diamond-like hierarchies. The concept of virtual base classes can make a class appear to be virtual or transparent. By making the two intermediate classes &amp;quot;Duck&amp;quot; and &amp;quot;Cow&amp;quot; virtual, we tell the compiler not to make duplicate variables or methods in &amp;quot;CowDuckHybrid&amp;quot; or any classes derived from the two virtual classes&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The implementation with virtual classes is shown below.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public virtual Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method&amp;lt;br&amp;gt;		&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// now legal, outputs &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Conceptual Difficulties&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For large projects, where there are several layers of classes and inheritance from many classes, the amount of conflicts can become high and resolving those conflicts can become very tedious. In languages such as C++, where multiple inheritance is supported, the programmer must explicitly indicate which parent class method or variable to use, or resolve the conflict by using the virtual keyword functionality. This, however, requires the programmer to trace back to the parent classes to determine what course of action to take. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Without good organization of the classes and understanding of how they are organized, multiple inheritance can become more trouble than it is worth. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;Alternatives to Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Many object-oriented languages do not use multiple inheritance for the reasons mentioned above. One of those languages is Java. Part of the philosophy of the Java language is to be simple and ease of use, and the creators of Java decided not to have support for multiple inheritance. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Alternative in Java&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Java can simulate multiple inheritance through the use of multiple interfaces&amp;lt;sup&amp;gt;6,7&amp;lt;/sup&amp;gt;. For example, the hierarchy below can be implemented in Java.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig8.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 8: Implementing multiple inheritance in Java&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
interface Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb();&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
interface Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk();&amp;lt;br&amp;gt;&lt;br /&gt;
	void run();&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman implements Spider, Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	public void shootWeb() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void climbWalls() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void talk() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
	public void run() {...}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Spider s = new Spiderman();			// all of these statements are valid&amp;lt;br&amp;gt;&lt;br /&gt;
Man m = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
Spiderman sm = new Spiderman();&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Note that in this implementation, we would have to implement the &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; methods in the &amp;quot;Spiderman&amp;quot; class. We cannot include code in the interface, as this violates the concept of interfaces in Java. We loose code re-usability, but persevered polymorphic relationship. If we created another class called &amp;quot;Venom&amp;quot; and implemented &amp;quot;Spider&amp;quot; class, we would have to duplicate the code. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand, if we don't care about polymorphism and just don't want to duplicate code, we can just compost the code like the following.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Spider {&amp;lt;br&amp;gt;&lt;br /&gt;
	void shootWeb { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void climbWalls { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Man {&amp;lt;br&amp;gt;&lt;br /&gt;
	void talk() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
	void run() { ... }&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Spiderman {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	Man m;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Spiderman specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class Venom {&amp;lt;br&amp;gt;&lt;br /&gt;
	Spider s;&amp;lt;br&amp;gt;&lt;br /&gt;
	&amp;lt;br&amp;gt;&lt;br /&gt;
	// Venom specific stuff&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here both &amp;quot;Spiderman&amp;quot; and &amp;quot;Venom&amp;quot; can reuse the &amp;quot;Spider&amp;quot; class code without having to implement it twice. However, &amp;quot;Spiderman&amp;quot; cannot be considered a &amp;quot;Spider&amp;quot; nor a &amp;quot;Man&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Both solutions shown are not really multiple inheritance in the true sense, but they can get the job done, depending on the specifications of the design.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Modules and Mixins&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Ruby can simulate multiple inheritance using modules. In Ruby the code appears as follows.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
module Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	def shootWeb()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def climbWalls()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
module Man&amp;lt;br&amp;gt;&lt;br /&gt;
	def talk()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
	def run()&amp;lt;br&amp;gt;&lt;br /&gt;
		// ...&amp;lt;br&amp;gt;&lt;br /&gt;
	end&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Spiderman&amp;lt;br&amp;gt;&lt;br /&gt;
	include Spider&amp;lt;br&amp;gt;&lt;br /&gt;
	include Man&amp;lt;br&amp;gt;&lt;br /&gt;
	// ...&amp;lt;br&amp;gt;&lt;br /&gt;
end&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In Ruby, the module code is simply &amp;quot;mixed in&amp;quot; to the specified class, basically a copy and paste function. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Here are some key rules to modules in Ruby.&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;&lt;br /&gt;
	1. It is not a class&amp;lt;br&amp;gt;&lt;br /&gt;
	2. It cannot be instantiated&amp;lt;br&amp;gt;&lt;br /&gt;
	3. It can be mixed into a class&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When modules are mixed into a class&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;,&amp;lt;br&amp;gt;&lt;br /&gt;
	1. There is no hierarchy. Even though we included &amp;quot;Spider&amp;quot; and &amp;quot;Man&amp;quot; in class &amp;quot;Spiderman&amp;quot;, &amp;quot;Spiderman&amp;quot; object cannot be polymorphized into &amp;quot;Spider&amp;quot; nor &amp;quot;Man&amp;quot;. That is, &amp;quot;Spiderman&amp;quot; is not a subclass of &amp;quot;Spider&amp;quot; or &amp;quot;Man&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	2. Multiple modules can be mixed-in to a class.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35852</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35852"/>
		<updated>2010-09-21T19:50:38Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Disadvantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The reality is that several implementation issues keep it from being incorporated into some languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some key issues include name and method collision&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;, the diamond problem&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;, and conceptual difficulties and understandability problems&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Name collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A subclass that inherits from several parents classes with the same variable names will have name collisions.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 5: Which x value will print_x() method print?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Method collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 6: Which show() method will Package inherit?&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;The Diamond Problem&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The diamond problem occurs when the direct base classes are themselves derived from another class&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 7: What sound should the CowDuckHybird make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's define two classes called &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot;. They both inherit from &amp;quot;Animal&amp;quot;. The makeSound() method is inherited in both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; classes. The &amp;quot;Cow&amp;quot; class defines the method to return &amp;quot;moo&amp;quot;, while the &amp;quot;Duck&amp;quot; class redefines the method to return &amp;quot;quack&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that through some strange breeding, a cow and duck breed to create a &amp;quot;CowDuckHybird&amp;quot; classes which inherits from both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; parents. When the makeSound() method is called on a &amp;quot;CowDuckHybrid&amp;quot; object, what sound will make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In a multiple inheritance supporting language such as C++, the &amp;quot;CowDuckHybrid&amp;quot; class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The code implementation in C++ illustrates this kind of problem below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound () {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;silence&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Cow: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;moo&amp;quot;;		// redefine method&amp;lt;br&amp;gt;&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Duck: public Animal {&amp;lt;br&amp;gt;&lt;br /&gt;
	String makeSound() {&amp;lt;br&amp;gt;&lt;br /&gt;
		return &amp;quot;quack&amp;quot;;	// redefine method	&amp;lt;br&amp;gt;	&lt;br /&gt;
	}&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class CowDuckHybrid: public Cow, public Duck {&amp;lt;br&amp;gt;&lt;br /&gt;
	// automatically inherit makeSound() from Cow and Duck&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
// Main code&amp;lt;br&amp;gt;&lt;br /&gt;
Animal a;&amp;lt;br&amp;gt;&lt;br /&gt;
a.makeSound();			// illegal statement - compiler will complain&amp;lt;br&amp;gt;&lt;br /&gt;
a.Cow::makeSound();		// outputs &amp;quot;moo&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
a.Duck::makeSound()		// outputs &amp;quot;quack&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35851</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35851"/>
		<updated>2010-09-21T19:46:59Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Disadvantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The reality is that several implementation issues keep it from being incorporated into some languages.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Some key issues include name and method collision&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;, the diamond problem&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;, and conceptual difficulties and understandability problems&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Name collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A subclass that inherits from several parents classes with the same variable names will have name collisions.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig5.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 5: Which x value will print_x() method print?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently. &lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;Method collision&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig6.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 6: Which show() method will Package inherit?&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;h2&amp;gt;The Diamond Problem&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
The diamond problem occurs when the direct base classes are themselves derived from another class&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig7.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 7: What sound should the CowDuckHybird make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's define two classes called &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot;. They both inherit from &amp;quot;Animal&amp;quot;. The makeSound() method is inherited in both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; classes. The &amp;quot;Cow&amp;quot; class defines the method to return &amp;quot;moo&amp;quot;, while the &amp;quot;Duck&amp;quot; class redefines the method to return &amp;quot;quack&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's assume that through some strange breeding, a cow and duck breed to create a &amp;quot;CowDuckHybird&amp;quot; classes which inherits from both &amp;quot;Cow&amp;quot; and &amp;quot;Duck&amp;quot; parents. When the makeSound() method is called on a &amp;quot;CowDuckHybrid&amp;quot; object, what sound will make?&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In a multiple inheritance supporting language such as C++, the &amp;quot;CowDuckHybrid&amp;quot; class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35850</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35850"/>
		<updated>2010-09-21T19:39:49Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35849</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35849"/>
		<updated>2010-09-21T19:38:56Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Multiple Inheritance&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35848</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35848"/>
		<updated>2010-09-21T19:36:04Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1&amp;gt;Advantages of Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case below where we have &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; parent classes and two subclasses &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Sedan&amp;quot; class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the &amp;quot;Truck&amp;quot; class. The &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Assume that we need polymorphic functionality. For example, we need to be able to run the following code:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Sedan s = new Sedan();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Then we pass object 's' to a method somewhere requiring an &amp;quot;Engine&amp;quot; type such as &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int testEngine(Engine e);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Since our sedan has an engine we want to do:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
testEngine(s);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the &amp;quot;Engine&amp;quot; class.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig3.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 3: Multiple inheritance allows more flexible hierarchies.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three &amp;quot;Engine&amp;quot;, &amp;quot;Chassis&amp;quot;, and &amp;quot;Body&amp;quot; classes into a single class, perhaps called &amp;quot;Assembly&amp;quot;. Then the &amp;quot;Sedan&amp;quot; and &amp;quot;Truck&amp;quot; classes would inherit from this combined class. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig4.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 4: Single inheritance limits hierarchy.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The &amp;quot;Lawnmower&amp;quot; class now posses a dilemma. Should we inherit from the &amp;quot;Assembly&amp;quot; class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the &amp;quot;Assembly&amp;quot; class and copied into the &amp;quot;Lawnmower&amp;quot; classes. What happens when engine specifications change, we would need to modify engine code in two places?  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore, as the example demonstrates the structure for single inheritance is more constrained.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35847</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35847"/>
		<updated>2010-09-21T19:31:20Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Multiple Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class &amp;quot;Bike&amp;quot; could have been made up from two classes called &amp;quot;Frame&amp;quot; and &amp;quot;Wheels&amp;quot;. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:EC_fig2.jpg]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 2: Case of multiple inheritance&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The case where a subclass inherits from multiple parent classes is called &amp;lt;b&amp;gt;multiple inheritance&amp;lt;/b&amp;gt;. This feature is not supported in all objected oriented languages. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The C++ implementation of the example is shown below. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
class Frame {&amp;lt;br&amp;gt;&lt;br /&gt;
	// frame of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Wheels {&amp;lt;br&amp;gt;&lt;br /&gt;
	// wheels of a bike&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
class Bike: public Frame, public Wheels&amp;lt;br&amp;gt;&lt;br /&gt;
	// create bike by putting together frame and wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35846</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35846"/>
		<updated>2010-09-21T19:27:48Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
public class Bike { &amp;lt;br&amp;gt;&lt;br /&gt;
	// Implementation of generic bike class &amp;lt;br&amp;gt;&lt;br /&gt;
	// ie. frame, wheels&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class MountainBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add shocks, fat tires, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
public class RoadBike extends Bike {&amp;lt;br&amp;gt;&lt;br /&gt;
	// inherit generic bike components&amp;lt;br&amp;gt;&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&amp;lt;br&amp;gt;&lt;br /&gt;
}&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35845</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2f EC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2f_EC&amp;diff=35845"/>
		<updated>2010-09-21T19:25:33Z</updated>

		<summary type="html">&lt;p&gt;Jechen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h1&amp;gt;What is inheritance?&amp;lt;/h1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt; is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, if the class &amp;quot;Bike&amp;quot; already exists and we wanted to create specific type of bicycle such as &amp;quot;MountainBike&amp;quot; or &amp;quot;RoadBike&amp;quot;, we could just create classes that inherit from the &amp;quot;Bike&amp;quot; class and add the additional functionality we need.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:EC_fig1.jpg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1: Simple case of inheritance&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h2&amp;gt;Single Inheritance&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
For the example above, where a subclass inherits from a single parent class, this is called &amp;lt;b&amp;gt;single inheritance&amp;lt;/b&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;samp&amp;gt;&lt;br /&gt;
public class Bike {&lt;br /&gt;
	// Implementation of generic bike class&lt;br /&gt;
	// ie. frame, wheels&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class MountainBike extends Bike {&lt;br /&gt;
	// inherit generic bike components&lt;br /&gt;
	// add shocks, fat tires, etc.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class RoadBike extends Bike {&lt;br /&gt;
	// inherit generic bike components&lt;br /&gt;
	// add narrow tires, drop-style handlebars, etc.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/samp&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jechen</name></author>
	</entry>
</feed>