<?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=Sgurram</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=Sgurram"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Sgurram"/>
	<updated>2026-05-16T09:09:22Z</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_2011/ch4_5a_sp&amp;diff=54583</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54583"/>
		<updated>2011-11-01T20:41:22Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
User&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
FileSystem&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookController&amp;lt;br&amp;gt;&lt;br /&gt;
Person&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookGUI&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
'''Person'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
*Create a new object,given an individual's name,address,city,state,ZIP, and phone&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's first name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's last name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's address&amp;lt;br&amp;gt;&lt;br /&gt;
*Update the stored information(except the name) about an individual&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are no collaborators for this class&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''File System'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
Read a stored address book from a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
Save an adddress book to a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The collaborator for both the responsibilities is&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
stop&amp;lt;br&amp;gt;&lt;br /&gt;
map&amp;lt;br&amp;gt;&lt;br /&gt;
segment&amp;lt;br&amp;gt;&lt;br /&gt;
walk&amp;lt;br&amp;gt;&lt;br /&gt;
bus&amp;lt;br&amp;gt;&lt;br /&gt;
Trip&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of the Trip class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
Know your Stops(R)&lt;br /&gt;
Know your Stopping Times(R)&lt;br /&gt;
&lt;br /&gt;
Stop(C)&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54582</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54582"/>
		<updated>2011-11-01T20:40:08Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 4: Other Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
User&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
FileSystem&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookController&amp;lt;br&amp;gt;&lt;br /&gt;
Person&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookGUI&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
'''Person'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
*Create a new object,given an individual's name,address,city,state,ZIP, and phone&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's first name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's last name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's address&amp;lt;br&amp;gt;&lt;br /&gt;
*Update the stored information(except the name) about an individual&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are no collaborators for this class&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''File System'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
Read a stored address book from a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
Save an adddress book to a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The collaborator for both the responsibilities is&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
stop&amp;lt;br&amp;gt;&lt;br /&gt;
map&amp;lt;br&amp;gt;&lt;br /&gt;
segment&amp;lt;br&amp;gt;&lt;br /&gt;
walk&amp;lt;br&amp;gt;&lt;br /&gt;
bus&amp;lt;br&amp;gt;&lt;br /&gt;
Trip&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of the Trip class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
Know your Stops(R)&lt;br /&gt;
Know your Stopping Times(R)&lt;br /&gt;
&lt;br /&gt;
Stop(C)&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54581</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54581"/>
		<updated>2011-11-01T20:39:46Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 4: Other Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
User&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
FileSystem&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookController&amp;lt;br&amp;gt;&lt;br /&gt;
Person&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookGUI&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
'''Person'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
*Create a new object,given an individual's name,address,city,state,ZIP, and phone&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's first name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's last name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's address&amp;lt;br&amp;gt;&lt;br /&gt;
*Update the stored information(except the name) about an individual&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are no collaborators for this class&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''File System'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
Read a stored address book from a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
Save an adddress book to a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The collaborator for both the responsibilities is&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
stop&amp;lt;br&amp;gt;&lt;br /&gt;
map&amp;lt;br&amp;gt;&lt;br /&gt;
segment&amp;lt;br&amp;gt;&lt;br /&gt;
walk&amp;lt;br&amp;gt;&lt;br /&gt;
bus&amp;lt;br&amp;gt;&lt;br /&gt;
Trip&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of the ''Trip'' class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
Know your Stops(R)&lt;br /&gt;
Know your Stopping Times(R)&lt;br /&gt;
&lt;br /&gt;
Stop(C)&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54580</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54580"/>
		<updated>2011-11-01T20:39:13Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 3: Course Registration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
User&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
FileSystem&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookController&amp;lt;br&amp;gt;&lt;br /&gt;
Person&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookGUI&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
'''Person'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
*Create a new object,given an individual's name,address,city,state,ZIP, and phone&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's first name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's last name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's address&amp;lt;br&amp;gt;&lt;br /&gt;
*Update the stored information(except the name) about an individual&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are no collaborators for this class&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''File System'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
Read a stored address book from a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
Save an adddress book to a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The collaborator for both the responsibilities is&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
stop&amp;lt;br&amp;gt;&lt;br /&gt;
map&amp;lt;br&amp;gt;&lt;br /&gt;
segment&amp;lt;br&amp;gt;&lt;br /&gt;
walk&amp;lt;br&amp;gt;&lt;br /&gt;
bus&amp;lt;br&amp;gt;&lt;br /&gt;
Trip&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of the ''Trip''classe and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
Know your Stops(R)&lt;br /&gt;
Know your Stopping Times(R)&lt;br /&gt;
&lt;br /&gt;
Stop(C)&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54579</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54579"/>
		<updated>2011-11-01T20:38:49Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 2: Address Book */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
User&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
FileSystem&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookController&amp;lt;br&amp;gt;&lt;br /&gt;
Person&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookGUI&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
'''Person'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
*Create a new object,given an individual's name,address,city,state,ZIP, and phone&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's first name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's last name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's address&amp;lt;br&amp;gt;&lt;br /&gt;
*Update the stored information(except the name) about an individual&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are no collaborators for this class&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''File System'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
Read a stored address book from a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
Save an adddress book to a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The collaborator for both the responsibilities is&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
stop&amp;lt;br&amp;gt;&lt;br /&gt;
map&amp;lt;br&amp;gt;&lt;br /&gt;
segment&amp;lt;br&amp;gt;&lt;br /&gt;
walk&amp;lt;br&amp;gt;&lt;br /&gt;
bus&amp;lt;br&amp;gt;&lt;br /&gt;
Trip&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of the ''Trip''classe and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
Know your Stops(R)&lt;br /&gt;
Know your Stopping Times(R)&lt;br /&gt;
&lt;br /&gt;
Stop(C)&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54578</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54578"/>
		<updated>2011-11-01T20:38:32Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 2: Address Book */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
User&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
FileSystem&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookController&amp;lt;br&amp;gt;&lt;br /&gt;
Person&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBookGUI&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
'''Person'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
*Create a new object,given an individual's name,address,city,state,ZIP, and phone&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's first name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's last name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's address&amp;lt;br&amp;gt;&lt;br /&gt;
*Update the stored information(except the name) about an individual&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are no collaborators for this class&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''File System'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
Read a stored address book from a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
Save an adddress book to a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The collaborator for both the responsibilities is&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
stop&amp;lt;br&amp;gt;&lt;br /&gt;
map&amp;lt;br&amp;gt;&lt;br /&gt;
segment&amp;lt;br&amp;gt;&lt;br /&gt;
walk&amp;lt;br&amp;gt;&lt;br /&gt;
bus&amp;lt;br&amp;gt;&lt;br /&gt;
Trip&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of the ''Trip''classe and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
Know your Stops(R)&lt;br /&gt;
Know your Stopping Times(R)&lt;br /&gt;
&lt;br /&gt;
Stop(C)&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54577</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54577"/>
		<updated>2011-11-01T20:37:50Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 1: Flight reservation system */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
User&lt;br /&gt;
AddressBook&lt;br /&gt;
FileSystem&lt;br /&gt;
AddressBookController&lt;br /&gt;
Person&lt;br /&gt;
AddressBookGUI&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
'''Person'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
*Create a new object,given an individual's name,address,city,state,ZIP, and phone&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's first name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's last name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's address&amp;lt;br&amp;gt;&lt;br /&gt;
*Update the stored information(except the name) about an individual&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are no collaborators for this class&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''File System'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
Read a stored address book from a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
Save an adddress book to a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The collaborator for both the responsibilities is&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
stop&amp;lt;br&amp;gt;&lt;br /&gt;
map&amp;lt;br&amp;gt;&lt;br /&gt;
segment&amp;lt;br&amp;gt;&lt;br /&gt;
walk&amp;lt;br&amp;gt;&lt;br /&gt;
bus&amp;lt;br&amp;gt;&lt;br /&gt;
Trip&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of the ''Trip''classe and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
Know your Stops(R)&lt;br /&gt;
Know your Stopping Times(R)&lt;br /&gt;
&lt;br /&gt;
Stop(C)&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54576</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54576"/>
		<updated>2011-11-01T20:14:23Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 2: Address Book */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
User&lt;br /&gt;
AddressBook&lt;br /&gt;
FileSystem&lt;br /&gt;
AddressBookController&lt;br /&gt;
Person&lt;br /&gt;
AddressBookGUI&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
'''Person'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
*Create a new object,given an individual's name,address,city,state,ZIP, and phone&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's first name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's last name&amp;lt;br&amp;gt;&lt;br /&gt;
*Furnish the individual's address&amp;lt;br&amp;gt;&lt;br /&gt;
*Update the stored information(except the name) about an individual&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are no collaborators for this class&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''File System'''&amp;lt;br&amp;gt;&lt;br /&gt;
Responsibilities&amp;lt;br&amp;gt;&lt;br /&gt;
Read a stored address book from a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
Save an adddress book to a file,given its file name&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The collaborator for both the responsibilities is&amp;lt;br&amp;gt;&lt;br /&gt;
AddressBook&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
stop&amp;lt;br&amp;gt;&lt;br /&gt;
map&amp;lt;br&amp;gt;&lt;br /&gt;
segment&amp;lt;br&amp;gt;&lt;br /&gt;
walk&amp;lt;br&amp;gt;&lt;br /&gt;
bus&amp;lt;br&amp;gt;&lt;br /&gt;
Trip&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of the ''Trip''classe and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
Know your Stops(R)&lt;br /&gt;
Know your Stopping Times(R)&lt;br /&gt;
&lt;br /&gt;
Stop(C)&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54575</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54575"/>
		<updated>2011-11-01T20:03:03Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 4: Other Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
stop&amp;lt;br&amp;gt;&lt;br /&gt;
map&amp;lt;br&amp;gt;&lt;br /&gt;
segment&amp;lt;br&amp;gt;&lt;br /&gt;
walk&amp;lt;br&amp;gt;&lt;br /&gt;
bus&amp;lt;br&amp;gt;&lt;br /&gt;
Trip&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of the ''Trip''classe and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
Know your Stops(R)&lt;br /&gt;
Know your Stopping Times(R)&lt;br /&gt;
&lt;br /&gt;
Stop(C)&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54574</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54574"/>
		<updated>2011-11-01T19:38:51Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 3: Course Registration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
Instructor(C)	offer course(sections)(R)&lt;br /&gt;
&lt;br /&gt;
Student(C)	takesCourse(R)	Course(Colloborators)&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54573</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54573"/>
		<updated>2011-11-01T18:58:19Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 3: Course Registration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt; &lt;br /&gt;
Instructor&amp;lt;br&amp;gt;&lt;br /&gt;
Student&amp;lt;br&amp;gt;&lt;br /&gt;
schedule&amp;lt;br&amp;gt;&lt;br /&gt;
class&amp;lt;br&amp;gt;&lt;br /&gt;
Course&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54572</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54572"/>
		<updated>2011-11-01T18:26:09Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 1: Flight reservation system */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Customer&amp;lt;br&amp;gt;&lt;br /&gt;
Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Airport&amp;lt;br&amp;gt;	&lt;br /&gt;
Segment&amp;lt;br&amp;gt;&lt;br /&gt;
Ticket&amp;lt;br&amp;gt;&lt;br /&gt;
Cost&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
Seats &amp;lt;br&amp;gt;&lt;br /&gt;
Alternate Flights &amp;lt;br&amp;gt;&lt;br /&gt;
Outbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
Inbound Flight&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the responsibilities of the Customer class and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
* to make payment(R)      shopping cart(C)&lt;br /&gt;
* to choose the flight(R) Flight Segment(C)&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54560</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54560"/>
		<updated>2011-11-01T17:46:15Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 3: Course Registration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54559</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54559"/>
		<updated>2011-11-01T17:45:33Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 4: Other Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54558</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54558"/>
		<updated>2011-11-01T17:45:17Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 2: Address Book */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54557</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54557"/>
		<updated>2011-11-01T17:45:00Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 1: Flight reservation system */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The class names for the present scenario are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The nouns that are not classes are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The responsibilities of some of the classes and their collaborators are &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54541</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54541"/>
		<updated>2011-11-01T14:34:44Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 4: Other Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
''Requirements for the bus navigator system'':&amp;lt;br&amp;gt;&lt;br /&gt;
* The mission is to allow ''students'' to plan trips around NCSU and Raleigh by bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each ''rider'' specifies an origination ''stop'' and a destination ''stop'' .&amp;lt;br&amp;gt;&lt;br /&gt;
* The system presents a list of departure times, arrival times, and transfer points, and a ''map'' of the ''trip''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each item on the list consists of one or more of trip ''segments''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each trip segment consists of a ''departure point'' and ''time'', and an ''arrival point'' and ''time''.&amp;lt;br&amp;gt;&lt;br /&gt;
* If it would significantly reduce the time of the trip, the system may suggest ''walking'' between two nearby stops.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each segment may be on a ''Wolfline bus'', a ''CAT bus'', or a ''TTA bus''.&amp;lt;br&amp;gt;&lt;br /&gt;
* Each bus has a ''schedule'', which is a set of ''trips'', with each trip consisting of a set of ''stops'' and ''times''.&amp;lt;br&amp;gt;&lt;br /&gt;
* The ''fare'' is 0 on a Wolfline bus, $1 on a CAT bus, and $2 ($2.50 for express) on a TTA bus.&amp;lt;br&amp;gt;&lt;br /&gt;
* The system calculates the total ''cost'' of the trip by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54540</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54540"/>
		<updated>2011-11-01T14:12:29Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 3: Course Registration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
''Requirements for the Course Registration System'' &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to automate registration of students for courses at a university.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''student'' takes one or more courses, which are identified by name and course number.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''course'' is worth one or more ''credit hours''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	A student can register for up to 21 credit hours.&amp;lt;br&amp;gt;&lt;br /&gt;
*	For each course that is taught, the students are placed into ''sections'', which may meet at different times and be taught by different instructors.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If more than 50 students want to take the same ''class'', another section will be added.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If fewer than 5 students register for a section, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	If no ''instructor'' can be persuaded (or hired) to teach a class, it will be canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Students may specify alternate courses or sections, in case of a time ''conflict'', or if their first-choice class is canceled.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a student has registered for courses, (s)he will be e-mailed a class ''schedule''.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54539</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54539"/>
		<updated>2011-11-01T14:04:16Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 2: Address Book */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html Address Book Example]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54538</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54538"/>
		<updated>2011-11-01T14:03:18Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 2: Address Book */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&amp;lt;ref name=&amp;quot;Address Book Example&amp;quot;&amp;gt;[http://www.cs.gordon.edu/courses/cs211/AddressBookExample/Requirements.html]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54537</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54537"/>
		<updated>2011-11-01T14:00:47Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 2: Address Book */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
Requirements Statements &amp;lt;br&amp;gt;&lt;br /&gt;
The software to be designed is a program that can be used to maintain an address book. An address book holds a collection of entries, each recording a person's first and last names, address, city, state, zip, and phone number.&lt;br /&gt;
&lt;br /&gt;
It must be possible to add a new person to an address book, to edit existing information about a person (except the person's name), and to delete a person. It must be possible to sort the entries in the address book alphabetically by last name (with ties broken by first name if necessary), or by ZIP code (with ties broken by name if necessary). It must be possible to print out all the entries in the address book in &amp;quot;mailing label&amp;quot; format.&lt;br /&gt;
&lt;br /&gt;
It must be possible to create a new address book, to open a disk file containing an existing address book to close an address book, and to save an address book to a disk file, using standard New, Open, Close, Save and Save As ... File menu options. The program's File menu will also have a Quit option to allow closing all open address books and terminating the program.&lt;br /&gt;
&lt;br /&gt;
The initial requirements call for the program to only be able to work with a single address book at a time; therefore, if the user chooses the New or Open menu option, any current address book will be closed before creating/opening a new one. A later extension might allow for multiple address books to be open, each with its own window which can be closed separately, with closing the last open window resulting in terminating the program. In this case, New and Open will result in creating a new window, without affecting the current window.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of whether any changes have been made to an address book since it was last saved, and will offer the user the opportunity to save changes when an address book is closed either explicitly or as a result of choosing to create/open another or to quit the program.&lt;br /&gt;
&lt;br /&gt;
The program will keep track of the file that the current address book was read from or most recently saved to, will display the file's name as the title of the main window, and will use that file when executing the Save option. When a New address book is initially created, its window will be titled &amp;quot;Untitled&amp;quot;, and a Save operation will be converted to Save As ... - i.e. the user will be required to specify a file.&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54536</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54536"/>
		<updated>2011-11-01T13:55:21Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 1: Flight reservation system */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
''Requirements for the Flight Reservation System ''&amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline ''tickets'' to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each ''customer'' specifies an origination ''airport'', a destination airport, and dates for outbound and return ''flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one ''outbound flight'' and one ''return flight'' from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight ''segments'' (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more ''passengers''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are ''seats'' on the ''plane''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total ''cost'' of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select ''alternate flights''.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a ''confirmation''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54535</id>
		<title>CSC/ECE 517 Fall 2011/ch4 5a sp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_5a_sp&amp;diff=54535"/>
		<updated>2011-11-01T13:44:51Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example 1: Flight reservation system */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Introduction to Object Oriented Design(OOD)'''=&lt;br /&gt;
&lt;br /&gt;
This page serves as a knowledge source for understanding the concepts covered in Lecture14- [http://en.wikipedia.org/wiki/Object-oriented_design Introduction to Object Oriented Design(OOD)].&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
Lecture 14 &amp;lt;ref name=&amp;quot;CSC517&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517//common/lectures/notes/lec14.pdf OOD], CSC 517 Lecture14&amp;lt;/ref&amp;gt; of CSC 517 covered various aspects of the [http://en.wikipedia.org/wiki/Object-oriented_design object oriented design] including the basics of OOD principles along with the design criteria and the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class Responsibility Collaboration card] tool.&lt;br /&gt;
&lt;br /&gt;
Object-oriented design can be defined as the process of planning a 'system of interacting objects' for the purpose of solving a software problem. It is one approach to software design that revolves around the basic entity called [http://en.wikipedia.org/wiki/Object_(computer_science) object]. An object contains data and procedures grouped together to represent an entity(class). Object-oriented design is the discipline of defining the objects and their interactions to solve a problem that was identified and documented during object-oriented analysis.&lt;br /&gt;
&lt;br /&gt;
= Basic Object Oriented Concepts =&lt;br /&gt;
The five basic concepts of object-oriented design are the implementation level features that are built into the programming language. They can be considered as the general vocabulary required to implement an object oriented design solution &amp;lt;ref name=&amp;quot;OOD vocabulary&amp;quot;&amp;gt;[http://www.beroux.com/english/articles/oop_uml_and_rup.php OOD Vocabulary], OOD Vocabulary&amp;lt;/ref&amp;gt;.  These terms are explained in brief:&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Object_(computer_science) Object]: A ''class'' is a tight coupling or association of data structures with the methods or functions that act on the data. An ''object'' is an instance of the class serving a separate function. An object is defined by its properties.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Information_hiding Information Hiding/Encapsulation]: The ability to protect some components of the object from external entities i.e. protecting parts of the program from extensive modification if the design decision is changed. This is achieved by language keywords to enable a variable to be declared as ''private'' or ''protected'' to the owning ''class''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance]: inheritance is a way to reuse code of existing objects and thus ability for a ''class'' to extend or override functionality of another ''class''. Inheritance is realized by establishing a subtype from an existing object, or both, depending upon programming language support. &lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism]: The ability to replace an ''object'' with its ''subobjects''. The ability of an ''object-variable'' to contain, not only that ''object'', but also all of its ''subobjects''.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Interface_(computer_science) Interface]: Interface can be defined as the ability to defer the implementation of a ''method'' or the process to define the ''functions'' or ''methods'' signatures without implementing them.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Elegant Software Design =&lt;br /&gt;
Listed below are the different criteria for elegant software design. There are nine different design considerations, out of which the first five address correctness of the software and the rest consider the performance.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Usability Usability]: Usability can be measured by the ease of use for the client.&lt;br /&gt;
&lt;br /&gt;
'''Completeness''': A software program/application is complete if it solves all the client needs.&lt;br /&gt;
&lt;br /&gt;
'''Robustness''': A good design is one that can deal with unusual situations gracefully and avoid crashing.&lt;br /&gt;
&lt;br /&gt;
'''Efficiency''': The application performs the necessary computations in a reasonable amount of time.&lt;br /&gt;
&lt;br /&gt;
'''Scalability''': An application is scalable if it performs correctly and efficiently when the problems grow in size by several orders of magnitude.&lt;br /&gt;
&lt;br /&gt;
'''Readability''': If it is easy for another programmer to read and understand the design and code, the design is readable.&lt;br /&gt;
&lt;br /&gt;
'''Modifiability''': If the software can easily be enhanced or restricted by adding new features or removing old features without breaking existing code, the design is modifiable.&lt;br /&gt;
&lt;br /&gt;
'''Reusability''': Resuability checks the ability to be reused in another completely different setting.&lt;br /&gt;
&lt;br /&gt;
'''Simplicity''': The simplicity criteria check if the design and/or the implementation unnecessarily complex.&lt;br /&gt;
&lt;br /&gt;
= Design Concepts =&lt;br /&gt;
Listed below are the major components for building a design solution for a problem:&lt;br /&gt;
* Defining objects, creating [http://en.wikipedia.org/wiki/Class_diagram class diagram] from conceptual models and map the entities to classes.&lt;br /&gt;
&lt;br /&gt;
* Identifying attributes for each class/entity.&lt;br /&gt;
&lt;br /&gt;
* Use [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design patterns] &amp;lt;ref name=&amp;quot;Design Patterns&amp;quot;&amp;gt;[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf Design Patterns Elements of Reusable Object Oriented Software by Erich Gamma]&amp;lt;/ref&amp;gt; to solve  a problem that can be used in many different situations and/or applications.&lt;br /&gt;
&lt;br /&gt;
* Identifying persistent objects i.e. to know objects that last longer than a single runtime.&lt;br /&gt;
&lt;br /&gt;
* Identify the [http://en.wikipedia.org/wiki/Application_framework application framework] to take advantage of the existing code and reuse whatever possible.&lt;br /&gt;
&lt;br /&gt;
=Design Principles =&lt;br /&gt;
Listed below are the main principles that need to be followed for building an effective design solution&amp;lt;ref name=&amp;quot;Design Principles&amp;quot;&amp;gt;[http://www.oodesign.com/design-principles.html Object Oriented Design Principles]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Open/closed_principle Open Close Principle]: This principle states that software entities like classes, modules and functions should be open for extension but closed for modifications.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle]: This principle states that high-level modules should not depend on low-level modules and both should depend on abstractions.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Interface_segregation_principle Interface Segregation Principle]: This principle states that clients should not be forced to depend upon interfaces that they do not use.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Single_responsibility_principle Single Responsibility Principle]: This principle states that a class should have only reason to change.&lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov's Substitution Principle]: This principle states that derived types must be completely substitutable for their base types.&lt;br /&gt;
&lt;br /&gt;
= OOD: The CRC Method =&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card Class-Responsibility-Collaboration card] is a very useful tool for software design that determine the [http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card classes] needed along with the types of interactions.&lt;br /&gt;
&lt;br /&gt;
CRC cards are created from [http://en.wikipedia.org/wiki/Index_card index card]s on which are written:&lt;br /&gt;
&lt;br /&gt;
# The class name&lt;br /&gt;
# Its Super and Sub classes (if applicable)&lt;br /&gt;
# The responsibilities of the class.&lt;br /&gt;
# The names of other classes with which the class will collaborate to fulfill its responsibilities.&lt;br /&gt;
# Author&lt;br /&gt;
&lt;br /&gt;
The use of small card minimizes the complexity of the design. It forces the designer to refrain from giving the class too many responsibilities. These cards are portable and thus can easily be laid out on a table and re-arranged while discussing the design with other people.&lt;br /&gt;
&lt;br /&gt;
A specification for the program being designed is studied and then the cards that need to be created are designed. The cards contain the class names(generally nouns) along with the responsibilities(verbs) and the collaborators associated with the class. When choosing classes, we need to make sure that the class name,&lt;br /&gt;
&lt;br /&gt;
#is a singular noun,&lt;br /&gt;
#does not really have the same functionality as some other class,&lt;br /&gt;
#is not simply a primitive type,(because u need not write code for primitive types)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example 1: Flight reservation system ==&lt;br /&gt;
&lt;br /&gt;
Requirements for the Flight Reservation System &amp;lt;br&amp;gt;&lt;br /&gt;
*	The mission is to allow round-trip airline tickets to be bought over the Web.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each customer specifies an origination airport, a destination airport, and dates for outbound and return flights.&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer reserves one outbound flight and one return flight from a menu presented by the system.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each choice that the system presents consists of one or more flight segments (there may be a change of planes).&amp;lt;br&amp;gt;&lt;br /&gt;
*	The customer may buy tickets for one or more passengers.&amp;lt;br&amp;gt;&lt;br /&gt;
*	No more tickets can be sold for a flight than there are seats on the plane.&amp;lt;br&amp;gt;&lt;br /&gt;
*	Each passenger is assigned to a specific seat. &amp;lt;br&amp;gt;&lt;br /&gt;
*	The system calculates the total cost of the tickets by adding the cost of the individual segments.&amp;lt;br&amp;gt;&lt;br /&gt;
*If dissatisfied with the cost, the customer may select alternate flights.&amp;lt;br&amp;gt;&lt;br /&gt;
*	After a customer has bought a ticket, (s)he will be e-mailed a confirmation&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example 2: Address Book ==&lt;br /&gt;
&lt;br /&gt;
== Example 3: Course Registration ==&lt;br /&gt;
&lt;br /&gt;
== Example 4: Other Example ==&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=51050</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=51050"/>
		<updated>2011-09-26T00:53:20Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Scheme's way of handling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between [http://en.wikipedia.org/wiki/Type_system#Static_typing  statically] and [http://en.wikipedia.org/wiki/Type_system#Dynamic_typing dynamically] typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed.: [http://en.wikipedia.org/wiki/C_(programming_language) C],[http://en.wikipedia.org/wiki/C%2B%2B_(programming_language) C++],[http://en.wikipedia.org/wiki/Java_(programming_language) Java],[http://en.wikipedia.org/wiki/JADE_(programming_language) JADE],[http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal] etc&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : [http://en.wikipedia.org/wiki/PHP PHP],[http://en.wikipedia.org/wiki/Prolog Prolog],[http://en.wikipedia.org/wiki/Python_(programming_language) Python],[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby],[http://en.wikipedia.org/wiki/Smalltalk Small talk] etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val) do |i|&lt;br /&gt;
          $arr[i] = Proc.new{print i}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value. &amp;lt;ref&amp;gt;http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Scheme_(programming_language) Scheme] is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=51040</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=51040"/>
		<updated>2011-09-26T00:49:24Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* What exactly is Closure */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between [http://en.wikipedia.org/wiki/Type_system#Static_typing  statically] and [http://en.wikipedia.org/wiki/Type_system#Dynamic_typing dynamically] typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed.: [http://en.wikipedia.org/wiki/C_(programming_language) C],[http://en.wikipedia.org/wiki/C%2B%2B_(programming_language) C++],[http://en.wikipedia.org/wiki/Java_(programming_language) Java],[http://en.wikipedia.org/wiki/JADE_(programming_language) JADE],[http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal] etc&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : [http://en.wikipedia.org/wiki/PHP PHP],[http://en.wikipedia.org/wiki/Prolog Prolog],[http://en.wikipedia.org/wiki/Python_(programming_language) Python],[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby],[http://en.wikipedia.org/wiki/Smalltalk Small talk] etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A [http://en.wikipedia.org/wiki/Closure_(computer_science) closure] is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val) do |i|&lt;br /&gt;
          $arr[i] = Proc.new{print i}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value. &amp;lt;ref&amp;gt;http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=51032</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=51032"/>
		<updated>2011-09-26T00:46:33Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Statically vs Dynamically typed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between [http://en.wikipedia.org/wiki/Type_system#Static_typing  statically] and [http://en.wikipedia.org/wiki/Type_system#Dynamic_typing dynamically] typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed.: [http://en.wikipedia.org/wiki/C_(programming_language) C],[http://en.wikipedia.org/wiki/C%2B%2B_(programming_language) C++],[http://en.wikipedia.org/wiki/Java_(programming_language) Java],[http://en.wikipedia.org/wiki/JADE_(programming_language) JADE],[http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal] etc&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : [http://en.wikipedia.org/wiki/PHP PHP],[http://en.wikipedia.org/wiki/Prolog Prolog],[http://en.wikipedia.org/wiki/Python_(programming_language) Python],[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby],[http://en.wikipedia.org/wiki/Smalltalk Small talk] etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val) do |i|&lt;br /&gt;
          $arr[i] = Proc.new{print i}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value. &amp;lt;ref&amp;gt;http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=51030</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=51030"/>
		<updated>2011-09-26T00:42:07Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between [http://en.wikipedia.org/wiki/Type_system#Static_typing  statically] and [http://en.wikipedia.org/wiki/Type_system#Dynamic_typing dynamically] typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed.: [http://en.wikipedia.org/wiki/C_(programming_language) C],C++,Java,JADE,Pascal etc&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val) do |i|&lt;br /&gt;
          $arr[i] = Proc.new{print i}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value. &amp;lt;ref&amp;gt;http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50991</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50991"/>
		<updated>2011-09-26T00:08:57Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Statically vs Dynamically typed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed.: C,C++,Java,JADE,Pascal etc&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val) do |i|&lt;br /&gt;
          $arr[i] = Proc.new{print i}&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value. &amp;lt;ref&amp;gt;http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50542</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50542"/>
		<updated>2011-09-24T21:30:55Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Closures and Static Scoping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value. &amp;lt;ref&amp;gt;http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50541</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50541"/>
		<updated>2011-09-24T21:29:15Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Closures and Static Scoping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50540</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50540"/>
		<updated>2011-09-24T21:28:38Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Closures and Static Scoping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50539</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50539"/>
		<updated>2011-09-24T21:27:55Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50538</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50538"/>
		<updated>2011-09-24T21:27:24Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example in C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50537</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50537"/>
		<updated>2011-09-24T21:26:49Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Statically typed languages, difficulty */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.&amp;lt;ref&amp;gt;http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  &amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;/ref&amp;gt;&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50536</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50536"/>
		<updated>2011-09-24T21:25:40Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example in Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.&amp;lt;ref&amp;gt;http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50535</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50535"/>
		<updated>2011-09-24T21:24:59Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example in JS */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50534</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50534"/>
		<updated>2011-09-24T21:22:18Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Example in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. &amp;lt;ref&amp;gt;http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50533</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50533"/>
		<updated>2011-09-24T21:19:20Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* What exactly is Closure */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt;http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50532</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50532"/>
		<updated>2011-09-24T21:18:40Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Closures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''&amp;lt;ref&amp;gt;http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt; http://www.artima.com/intv/closures2.html  Blocks and Closures &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50531</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50531"/>
		<updated>2011-09-24T21:16:30Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Closures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
&amp;lt;ref&amp;gt; http://www.artima.com/intv/closures2.html  Blocks and Closures &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50530</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50530"/>
		<updated>2011-09-24T21:11:42Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Statically vs Dynamically typed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref&amp;gt; http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50529</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50529"/>
		<updated>2011-09-24T21:10:21Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref name=&amp;quot;Static Typing&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50528</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50528"/>
		<updated>2011-09-24T21:10:10Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Statically vs Dynamically typed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref name=&amp;quot;Static Typing&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50527</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50527"/>
		<updated>2011-09-24T21:04:14Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref name=&amp;quot;Static Typing&amp;quot;/&amp;gt;{{cite web&lt;br /&gt;
url = http://en.wikipedia.org/wiki/Type_system#Static_typing&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50526</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50526"/>
		<updated>2011-09-24T21:03:12Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Statically vs Dynamically typed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref name=&amp;quot;Static Typing&amp;quot;/&amp;gt;{{cite web&lt;br /&gt;
url = http://en.wikipedia.org/wiki/Type_system#Static_typing&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing&amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++ &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;br&amp;gt;&lt;br /&gt;
[11] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[12] http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;br&amp;gt;&lt;br /&gt;
[13] http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;br&amp;gt;&lt;br /&gt;
[14] http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50525</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50525"/>
		<updated>2011-09-24T20:57:50Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* Statically vs Dynamically typed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.&amp;lt;ref name=&amp;quot;Static Typing&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing&amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++ &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;br&amp;gt;&lt;br /&gt;
[11] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[12] http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;br&amp;gt;&lt;br /&gt;
[13] http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;br&amp;gt;&lt;br /&gt;
[14] http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50524</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50524"/>
		<updated>2011-09-24T20:54:14Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* statically typed languages, difficulty */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.[http://en.wikipedia.org/wiki/Type_system#Static_typing]&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== Statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing&amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++ &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;br&amp;gt;&lt;br /&gt;
[11] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[12] http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;br&amp;gt;&lt;br /&gt;
[13] http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;br&amp;gt;&lt;br /&gt;
[14] http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50523</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50523"/>
		<updated>2011-09-24T20:53:58Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* what exactly is Closure */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.[http://en.wikipedia.org/wiki/Type_system#Static_typing]&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== What exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing&amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++ &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;br&amp;gt;&lt;br /&gt;
[11] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[12] http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;br&amp;gt;&lt;br /&gt;
[13] http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;br&amp;gt;&lt;br /&gt;
[14] http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50522</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50522"/>
		<updated>2011-09-24T20:53:39Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* statically vs dynamically typed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== Statically vs Dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of Statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of Dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.[http://en.wikipedia.org/wiki/Type_system#Static_typing]&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== what exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing&amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++ &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;br&amp;gt;&lt;br /&gt;
[11] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[12] http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;br&amp;gt;&lt;br /&gt;
[13] http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;br&amp;gt;&lt;br /&gt;
[14] http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50521</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1d ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1d_ss&amp;diff=50521"/>
		<updated>2011-09-24T20:33:49Z</updated>

		<summary type="html">&lt;p&gt;Sgurram: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Closures in statically typed languages.  Most languages that implement closures are dynamically typed.  It is a challenge to implement closures in a statically typed language.  Explain why, and cover attempts to mix the two.  Consider also closures and static scoping, as in Scheme.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
We start off with a brief difference between statically and dynamically typed languages.We try to explain what a closure is, its advantages, how it can be implemented in various typed languages, why is it easier or difficult in some languages.Finally we conclude with static scoping.&lt;br /&gt;
&lt;br /&gt;
=== statically vs dynamically typed ===&lt;br /&gt;
One simple way to differentiate between the two is:&amp;lt;br&amp;gt;In statically typed languages,type checking is done at the compile time where as in dynamically typed languages, type checking is done at run-time.&lt;br /&gt;
					&lt;br /&gt;
Examples of statically typed : C,C++,Java,JADE,Pascal etc&amp;lt;br&amp;gt;&lt;br /&gt;
Examples of dynamically typed : PHP,Prolog,Python,Ruby,Small talk etc.[http://en.wikipedia.org/wiki/Type_system#Static_typing]&lt;br /&gt;
&lt;br /&gt;
== Closures ==&lt;br /&gt;
In the words of Matsumoto, the creator of Ruby language: ''A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away. ''[http://www.artima.com/intv/closures2.html]&lt;br /&gt;
&lt;br /&gt;
=== what exactly is Closure ===&lt;br /&gt;
*A closure is a first-class block of code that has access to the environment in which it was defined. &lt;br /&gt;
**A first class function is one that can be passed around just like any other object. &lt;br /&gt;
**The function can access ‘free variables’, ie, variables that are not an argument to, or a local variable of that function.  For example, if we have a function that returns a lambda function:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 public Func&amp;lt;int,int&amp;gt; GetFunc()&lt;br /&gt;
 {&lt;br /&gt;
       int y = 4;&lt;br /&gt;
       Func&amp;lt;int, int&amp;gt; myfunc = x =&amp;gt; { return x+y; };&lt;br /&gt;
       return myfunc;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
y is neither an input parameter nor a local variable declared within myfunc.  The free variable is bound in the lexical environment.  This means that even when GetFunc goes out of scope, myfunc will still have access to it.&lt;br /&gt;
[http://www.codethinked.com/c-closures-explained]&lt;br /&gt;
&lt;br /&gt;
=== Advantages of Closures ===&lt;br /&gt;
*It allows access to contextual information that wouldn’t be available inside a standard method of a class.  &lt;br /&gt;
*It can be passed around as an object and retain that context, even if the variables are outside their original scope.&amp;lt;br&amp;gt;&lt;br /&gt;
*There is no need to pass around internal variables for every call.&lt;br /&gt;
*Offers a more concise and clean method of programming operations.&lt;br /&gt;
&lt;br /&gt;
== Implementation of Closures ==&lt;br /&gt;
&lt;br /&gt;
=== In dynamically typed languages ===&lt;br /&gt;
We give the implementations of Closures in some of the dynamic languages.&lt;br /&gt;
==== Example in Ruby ====&lt;br /&gt;
Ruby implements closures with the support of procs and lambdas.These constructs are similar with subtle differences.Here is an example of closure in Ruby.&lt;br /&gt;
 class SomeClass&lt;br /&gt;
 def initialize(value1)&lt;br /&gt;
     @value1 = value1&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
   def value_printer(value2)&lt;br /&gt;
     lambda {puts &amp;quot;Value1: #{@value1}, Value2: #{value2}&amp;quot;}&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 def caller(some_closure)&lt;br /&gt;
   some_closure.call&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 some_class = SomeClass.new(5)&lt;br /&gt;
 printer = some_class.value_printer(&amp;quot;some value&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 caller(printer)&lt;br /&gt;
&lt;br /&gt;
when executed, we get the following ,value1 =5 and value2 = some value&lt;br /&gt;
&amp;lt;br&amp;gt;		 &lt;br /&gt;
As you can see, the value_printer function creates a closure, using the lambda construct, and then returns it. We then assign our closure to a variable and pass that variable to another function, which then calls our closure. This satisfies the first property of a closure – we can pass it around. Notice also that when we called our closure, we printed out &amp;quot;5&amp;quot; and &amp;quot;some value&amp;quot;. Even though both the @value1 and value2 variables were both well and truly out of scope in the rest of the program when we finally called the closure; inside the closure they were still in scope as it retained the state of all the variables that were in scope when it was defined. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]&lt;br /&gt;
&lt;br /&gt;
==== Example in JS ====&lt;br /&gt;
we can access private members via closures in JS.As private members can only be accessed from inside your function,this comes handy.The same holds true for private methods(or functions,since we are in JavaScript here).This can be easily done with closure.&lt;br /&gt;
		&lt;br /&gt;
 var namespace = {};&lt;br /&gt;
 (namespace.someClass = function() {&lt;br /&gt;
     var _privateProperty = &amp;quot;some value&amp;quot;;&lt;br /&gt;
     function _privateMethod() {&lt;br /&gt;
         //perform some stuff&lt;br /&gt;
     }&lt;br /&gt;
     return {&lt;br /&gt;
         publicProperty: &amp;quot;some value&amp;quot;,&lt;br /&gt;
         publicMethod: function publicMethod() {&lt;br /&gt;
             //perform some stuff&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }()); &lt;br /&gt;
&lt;br /&gt;
By looking closer at what exactly is happening, you'll notice that namespace.someClass is equal to an auto invoked function that returns an object literal.when executed an object that is assigned to namespace.someClass._privateProperty and _privateMethod() get closured and are now available to someClass,but since their containing function is no longer in scope, they are not available to anyone else.&lt;br /&gt;
[http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/]&lt;br /&gt;
&lt;br /&gt;
==== Example in Python ====&lt;br /&gt;
 def makeInc(x):&lt;br /&gt;
   def inc(y):&lt;br /&gt;
      # x is &amp;quot;closed&amp;quot; in the definition of inc&lt;br /&gt;
      return y + x&lt;br /&gt;
  return inc&lt;br /&gt;
 &lt;br /&gt;
 inc5 = makeInc(5)&lt;br /&gt;
 inc10 = makeInc(10)&lt;br /&gt;
 &lt;br /&gt;
 inc5 (5) # returns 10&lt;br /&gt;
 inc10(5) # returns 15&lt;br /&gt;
&lt;br /&gt;
Closures in python are created by function calls. Here, the call to makeInc creates a binding for x that is referenced inside the function inc. Each call to makeInc creates a new instance of this function, but each instance has a link to a different binding of x. The example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.[http://ynniv.com/blog/2007/08/closures-in-python.html]&lt;br /&gt;
&lt;br /&gt;
=== statically typed languages, difficulty ===&lt;br /&gt;
We can explain this via an example.&lt;br /&gt;
Method setBlock is defined as&lt;br /&gt;
		&lt;br /&gt;
 |n|&lt;br /&gt;
 ...&lt;br /&gt;
 b := [ n &amp;gt;= 0 ifTrue: ^1 ifFalse: ^0 ].&lt;br /&gt;
		&lt;br /&gt;
where b is an instance variable.After calling setBlock, a method of the same class may be called and may execute the above block - b value.&lt;br /&gt;
		&lt;br /&gt;
Inside the block there will be a return (^1 or ^0) from a method call of setBlock that no longer exists — a runtime error.&lt;br /&gt;
&amp;lt;br&amp;gt;		&lt;br /&gt;
It seems impossible to add true closures to statically typed languages because of the above described run time errors.Although these errors may be considered as &amp;quot;type errors&amp;quot;, they make the language unsafe which is against the norm of statically typed languages.[http://dl.acm.org/citation.cfm?id=1026483]&lt;br /&gt;
*Closures will need access to the lexical scope, ie, the scope in which the routine was generated as mentioned above.  This is a problem for statically typed languages as they keep track of local variables in the stack. Once those variables go out of scope and the routine tries to access them, it will throw an exception.  This problem must be overcome as closures must have access to that environment, even when it's out of scope.  [http://en.wikipedia.org/wiki/Static_scope]&lt;br /&gt;
*In addition, since they must be first class functions, ie, the ability to use them as a normal data type, the statically typed language must have a specific type defined for functions.  In the case of Java and C#, the garbage collector must keep those objects around, as well as their associated lexical environments, until there are no remaining references to that object.&lt;br /&gt;
&lt;br /&gt;
=== In statically typed languages ===&lt;br /&gt;
Some of the object oriented languages simulate some of the features of closures.&lt;br /&gt;
==== Example in C++ ====&lt;br /&gt;
For example:&lt;br /&gt;
*C++ defines function objects by overloading operator().They may be created at run time and may implicitly capture the state but they don't have the access to local variables as closures do.&lt;br /&gt;
			&lt;br /&gt;
*Two proposals to introduce C++ language support for closures (both proposals call them lambda functions) are being considered by the C++ Standards Committee.The main difference between these proposals is that one stores a copy of all the local variables in a closure by default, and another stores references to original variables. Both provide functionality to override the default behavior. If some form of these proposals is accepted, one would be able to write&lt;br /&gt;
&lt;br /&gt;
 void foo(string myname) {&lt;br /&gt;
     typedef vector&amp;lt;string&amp;gt; names;&lt;br /&gt;
     int y;&lt;br /&gt;
     names n;&lt;br /&gt;
     // ...&lt;br /&gt;
     names::iterator i = std::find_if(n.begin(), n.end(), [&amp;amp;](const string&amp;amp; s) { &lt;br /&gt;
             return s != myname &amp;amp;&amp;amp; s.size() &amp;gt; y; &lt;br /&gt;
         });&lt;br /&gt;
     // 'i' is now either 'n.end()' or points to the first string in 'n'&lt;br /&gt;
     // which is not equal to 'myname' and whose length is greater than 'y'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
At least two C++ compilers, Visual C++ 2010 and GCC 4.5, already support this notation.[http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example in Java ====&lt;br /&gt;
&lt;br /&gt;
JAVA allows functionality similar to closures via anonymous classes; an anonymous class may refer to names in lexically enclosing classes, or read-only variables (marked as final) in the lexically enclosing method.&lt;br /&gt;
		&lt;br /&gt;
         class CalculationWindow extends JFrame {&lt;br /&gt;
         private volatile int result;&lt;br /&gt;
         ...&lt;br /&gt;
         public void calculateInSeparateThread(final URI uri) {&lt;br /&gt;
                 // The expression &amp;quot;new Runnable() { ... }&amp;quot; is an anonymous class.&lt;br /&gt;
                 new Thread(&lt;br /&gt;
                         new Runnable() {&lt;br /&gt;
                                 void run() {&lt;br /&gt;
                                         // It can read final local variables:&lt;br /&gt;
                                         calculate(uri);&lt;br /&gt;
                                         // It can access private fields of the enclosing class:&lt;br /&gt;
                                         result = result + 10;&lt;br /&gt;
                                 }&lt;br /&gt;
                         }&lt;br /&gt;
                 ).start();&lt;br /&gt;
         }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some features of full closures can be emulated by using a final reference to a mutable container, for example, a single-element array. The inner class will not be able to change the value of the container reference itself, but it will be able to change the contents of the container. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java]&lt;br /&gt;
&lt;br /&gt;
== Closures and Static Scoping ==&lt;br /&gt;
Static scoping (also known as Lexical scoping) is a property of the runtime text and is independent of the runtime call stack. [http://en.wikipedia.org/wiki/Static_scope]  Implementing closures in a statically scoped language can be very challenging.  &lt;br /&gt;
*It requires each function to maintain the variables in the scope in which the closure was declared, even after they have dropped out of scope.  &lt;br /&gt;
*When using static scoping languages that implement closures there is some question as to whether to have access to the actual mutable variable or to a copy of the value at the time it was declared.  In other words, do we close over variables or values?  This is something that developers must ascertain when using closures.&lt;br /&gt;
For example in Ruby if we have this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
  $arr = Array.new&lt;br /&gt;
  def funcGen(val)&lt;br /&gt;
    0.upto(val)&lt;br /&gt;
    do |i|&lt;br /&gt;
      $arr[i] = Proc.new {                &lt;br /&gt;
                  print i &lt;br /&gt;
                  }&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  funcGen(4)&lt;br /&gt;
  $arr.each&lt;br /&gt;
  do |val|&lt;br /&gt;
    val.call&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have an output similiar to this:&lt;br /&gt;
01234&lt;br /&gt;
&lt;br /&gt;
It uses a copy of the value, not the variable.&lt;br /&gt;
&lt;br /&gt;
However, in C# if we have code does something similiar:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    delegate void Func();&lt;br /&gt;
    class Program&lt;br /&gt;
    {&lt;br /&gt;
        static Func[] funcArr = new Func[4];&lt;br /&gt;
        static void Main(string[] args)&lt;br /&gt;
        {&lt;br /&gt;
            fillFunc(funcArr.Length); &lt;br /&gt;
            for (int i = 0; i &amp;lt; funcArr.Length; i++) &lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i](); &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        static void fillFunc(int count) &lt;br /&gt;
        { &lt;br /&gt;
            for (int i = 0; i &amp;lt; count; i++)&lt;br /&gt;
            { &lt;br /&gt;
                funcArr[i] = delegate() { Console.Write(&amp;quot;{0}&amp;quot;, i); };      &lt;br /&gt;
            } &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will have this output.&lt;br /&gt;
4444&lt;br /&gt;
&lt;br /&gt;
The last value set in the environment is used.  This means that in this case, it closes over the variable, not the value, whereas Ruby closes over the value.  [http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx]&lt;br /&gt;
&lt;br /&gt;
===Scheme's way of handling===&lt;br /&gt;
&lt;br /&gt;
Scheme is one of the two main dialects of programming language LISP.It's design policy is to provide small core and powerful tools for language extension.Compactness and Elegance are some of the few features what made Scheme popular in various communities of researchers.&lt;br /&gt;
&lt;br /&gt;
Scheme is lexically scoped like most of the modern programming languages.all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called.&lt;br /&gt;
&lt;br /&gt;
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, &amp;quot;Scheme: An Interpreter for Extended Lambda Calculus&amp;quot; where they adopted the concept of the lexical closure.[http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope]&lt;br /&gt;
&lt;br /&gt;
An example in Scheme&lt;br /&gt;
 (define (makem)&lt;br /&gt;
    (define x 34)&lt;br /&gt;
    (list (lambda () (set! x (+ x 1)) x)&lt;br /&gt;
          (lambda () (set! x (+ x 1)) x))&lt;br /&gt;
 )&lt;br /&gt;
 (define f (car (makem)))&lt;br /&gt;
 (define f2 (car (cdr (makem))))&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 35&lt;br /&gt;
 &amp;gt; (f2)&lt;br /&gt;
 35            &lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 36&lt;br /&gt;
 &amp;gt; (f)&lt;br /&gt;
 37&lt;br /&gt;
 &amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://en.wikipedia.org/wiki/Type_system#Static_typing  Static Typing&amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.artima.com/intv/closures2.html  Blocks and Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.codethinked.com/c-closures-explained  C# Closures&amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/  Closures in Ruby&amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://tinyhippos.com/2010/07/05/closure-in-javascript-with-examples/  Closures in JS&amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://ynniv.com/blog/2007/08/closures-in-python.html  Closures in Python&amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://dl.acm.org/citation.cfm?id=1026483 Guimar˜aes, Jos´e de Oliveira. &amp;quot;Closures for Statically-Typed Object-Oriented Languages&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#C.2B.2B_.28operator_overloading.29 Closure like implementation in C++ &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Java Closures like implementation in Java&amp;lt;br&amp;gt;&lt;br /&gt;
[11] http://en.wikipedia.org/wiki/Static_scope Static Scope&amp;lt;br&amp;gt;&lt;br /&gt;
[12] http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx C# Anonymous methods&amp;lt;br&amp;gt;&lt;br /&gt;
[13] http://en.wikipedia.org/wiki/Scheme_(programming_language)#Lexical_scope Scheme &amp;lt;br&amp;gt;&lt;br /&gt;
[14] http://stackoverflow.com/questions/5608325/how-do-closures-in-scheme-work Closures in scheme &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sgurram</name></author>
	</entry>
</feed>