<?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=Sdkshirs</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=Sdkshirs"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Sdkshirs"/>
	<updated>2026-05-16T18:24:34Z</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_2012/ch2b_2w69_as&amp;diff=70364</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70364"/>
		<updated>2012-11-18T23:16:26Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Conventions to follow */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note that in the above solution, if we want to extend the behavior of the DrawAllShapes function in to draw a new kind of shape, all we need do is add a new derivative of the Shape class. The DrawAllShapes function does not need to change. Thus DrawAllShapes conforms to the open-closed principle. Its behavior can be extended without modifying it.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
It should be clear that no significant program can be 100% closed. For example, consider the above DrawAllShapes example. What would happen to the DrawAllShapes function if we decided that all Circles should be drawn before any Squares. The DrawAllShapes function is not closed against a change like this. In general, no matter how “closed” a module is, there will always be some kind of change against which it is not closed. Since closure cannot be complete, it must be strategic. That is, the designer must choose the kinds of changes against which to close his design. This takes a certain amount of prescience derived from experience. The experienced designer knows the users and the industry well enough to judge the probability of different kinds of changes. He then makes sure that the open-closed principle is invoked for the most probable changes.&lt;br /&gt;
&lt;br /&gt;
==== Conventions to follow ====&lt;br /&gt;
In order to obey open-closed principle it is good to follow some conventions&amp;lt;ref name=pdf&amp;gt;&amp;lt;/ref&amp;gt;.&lt;br /&gt;
* &amp;lt;b&amp;gt;Make all Member Variables Private:&amp;lt;/b&amp;gt; Member variables of classes should be known only to the methods of the class that defines them. Member variables should never be known to any other class, including derived classes. Thus they should be declared private, rather than public or protected. In light of the open-closed principle, the reason for this convention ought to be clear. When the member variables of a class change, every function that depends upon those variables must be changed. Thus, no function that depends upon a variable can be closed with respect to that variable. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;No global variables:&amp;lt;/b&amp;gt; The argument against global variables is similar to the argument against pubic member variables. No module that depends upon a global variable can be closed against any other module that might write to that variable. Any module that uses the variable in a way that the other modules don’t expect, will break those other modules. It is too risky to have many modules be subject to the whim of one badly behaved one. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Avoid run time type identification:&amp;lt;/b&amp;gt; Another very common proscription is the one against dynamic_cast. It is often claimed that dynamic_cast, or any form of run time type identification is intrinsically dangerous and should be avoided to comply with the open-closed principle.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70361</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70361"/>
		<updated>2012-11-18T23:13:33Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Conventions to follow */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note that in the above solution, if we want to extend the behavior of the DrawAllShapes function in to draw a new kind of shape, all we need do is add a new derivative of the Shape class. The DrawAllShapes function does not need to change. Thus DrawAllShapes conforms to the open-closed principle. Its behavior can be extended without modifying it.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
It should be clear that no significant program can be 100% closed. For example, consider the above DrawAllShapes example. What would happen to the DrawAllShapes function if we decided that all Circles should be drawn before any Squares. The DrawAllShapes function is not closed against a change like this. In general, no matter how “closed” a module is, there will always be some kind of change against which it is not closed. Since closure cannot be complete, it must be strategic. That is, the designer must choose the kinds of changes against which to close his design. This takes a certain amount of prescience derived from experience. The experienced designer knows the users and the industry well enough to judge the probability of different kinds of changes. He then makes sure that the open-closed principle is invoked for the most probable changes.&lt;br /&gt;
&lt;br /&gt;
==== Conventions to follow ====&lt;br /&gt;
In order to obey open-closed principle it is good to follow some conventions.&lt;br /&gt;
* &amp;lt;b&amp;gt;Make all Member Variables Private:&amp;lt;/b&amp;gt; Member variables of classes should be known only to the methods of the class that defines them. Member variables should never be known to any other class, including derived classes. Thus they should be declared private, rather than public or protected. In light of the open-closed principle, the reason for this convention ought to be clear. When the member variables of a class change, every function that depends upon those variables must be changed. Thus, no function that depends upon a variable can be closed with respect to that variable. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;No global variables:&amp;lt;/b&amp;gt; The argument against global variables is similar to the argument against pubic member variables. No module that depends upon a global variable can be closed against any other module that might write to that variable. Any module that uses the variable in a way that the other modules don’t expect, will break those other modules. It is too risky to have many modules be subject to the whim of one badly behaved one. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Avoid run time type identification:&amp;lt;/b&amp;gt; Another very common proscription is the one against dynamic_cast. It is often claimed that dynamic_cast, or any form of run time type identification is intrinsically dangerous and should be avoided to comply with the open-closed principle.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70359</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70359"/>
		<updated>2012-11-18T23:10:08Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Conventions to follow */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note that in the above solution, if we want to extend the behavior of the DrawAllShapes function in to draw a new kind of shape, all we need do is add a new derivative of the Shape class. The DrawAllShapes function does not need to change. Thus DrawAllShapes conforms to the open-closed principle. Its behavior can be extended without modifying it.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
It should be clear that no significant program can be 100% closed. For example, consider the above DrawAllShapes example. What would happen to the DrawAllShapes function if we decided that all Circles should be drawn before any Squares. The DrawAllShapes function is not closed against a change like this. In general, no matter how “closed” a module is, there will always be some kind of change against which it is not closed. Since closure cannot be complete, it must be strategic. That is, the designer must choose the kinds of changes against which to close his design. This takes a certain amount of prescience derived from experience. The experienced designer knows the users and the industry well enough to judge the probability of different kinds of changes. He then makes sure that the open-closed principle is invoked for the most probable changes.&lt;br /&gt;
&lt;br /&gt;
==== Conventions to follow ====&lt;br /&gt;
In order to obey open-closed principle it is good to follow some conventions.&lt;br /&gt;
* &amp;lt;b&amp;gt;Make all Member Variables Private:&amp;lt;/b&amp;gt; Member variables of classes should be known only to the methods of the class that defines them. Member&lt;br /&gt;
variables should never be known to any other class, including derived classes. Thus they should be declared private, rather than public or protected. In light of the open-closed principle, the reason for this convention ought to be clear. When the member variables of a class change, every function that depends upon those variables must be changed. Thus, no function that depends upon a variable can be closed with respect to that variable. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;No global variables:&amp;lt;/b&amp;gt; The argument against global variables is similar to the argument against pubic member variables. No module that depends upon a global variable can be closed against any other module that might write to that variable. Any module that uses the variable in a way that the other modules don’t expect, will break those other modules. It is too risky to have many modules be subject to the whim of one badly behaved one. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Avoid run time type identification:&amp;lt;/b&amp;gt; Another very common proscription is the one against dynamic_cast. It is often claimed that dynamic_cast, or any form of run time type identification is intrinsically dangerous and should be avoided.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70358</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70358"/>
		<updated>2012-11-18T23:09:30Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Conventions to follow */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note that in the above solution, if we want to extend the behavior of the DrawAllShapes function in to draw a new kind of shape, all we need do is add a new derivative of the Shape class. The DrawAllShapes function does not need to change. Thus DrawAllShapes conforms to the open-closed principle. Its behavior can be extended without modifying it.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
It should be clear that no significant program can be 100% closed. For example, consider the above DrawAllShapes example. What would happen to the DrawAllShapes function if we decided that all Circles should be drawn before any Squares. The DrawAllShapes function is not closed against a change like this. In general, no matter how “closed” a module is, there will always be some kind of change against which it is not closed. Since closure cannot be complete, it must be strategic. That is, the designer must choose the kinds of changes against which to close his design. This takes a certain amount of prescience derived from experience. The experienced designer knows the users and the industry well enough to judge the probability of different kinds of changes. He then makes sure that the open-closed principle is invoked for the most probable changes.&lt;br /&gt;
&lt;br /&gt;
==== Conventions to follow ====&lt;br /&gt;
In order to obey open-closed principle it is good to follow some conventions.&lt;br /&gt;
* &amp;lt;b&amp;gt;Make all Member Variables Private:&amp;lt;/b&amp;gt; Member variables of classes should be known only to the methods of the class that defines them. Member&lt;br /&gt;
variables should never be known to any other class, including derived classes. Thus they should be declared private, rather than public or protected. In light of the open-closed principle, the reason for this convention ought to be clear. When the member variables of a class change, every function that depends upon those variables must be changed. Thus, no function that depends upon a variable can be closed with respect to that variable. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;No global variables:&amp;lt;/b&amp;gt; The argument against global variables is similar to the argument against pubic member variables. No module that depends upon a global variable can be closed against any other module that might write to that variable. Any module that uses the variable in a way that&lt;br /&gt;
the other modules don’t expect, will break those other modules. It is too risky to have many modules be subject to the whim of one badly behaved one. &amp;lt;br&amp;gt;&lt;br /&gt;
* &amp;lt;b&amp;gt;Avoid run time type identification:&amp;lt;/b&amp;gt; Another very common proscription is the one against dynamic_cast. It is often claimed that dynamic_cast, or any form of run time type identification is intrinsically dangerous and should be avoided.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70350</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70350"/>
		<updated>2012-11-18T21:35:20Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Conventions to follow */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note that in the above solution, if we want to extend the behavior of the DrawAllShapes function in to draw a new kind of shape, all we need do is add a new derivative of the Shape class. The DrawAllShapes function does not need to change. Thus DrawAllShapes conforms to the open-closed principle. Its behavior can be extended without modifying it.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
It should be clear that no significant program can be 100% closed. For example, consider the above DrawAllShapes example. What would happen to the DrawAllShapes function if we decided that all Circles should be drawn before any Squares. The DrawAllShapes function is not closed against a change like this. In general, no matter how “closed” a module is, there will always be some kind of change against which it is not closed. Since closure cannot be complete, it must be strategic. That is, the designer must choose the kinds of changes against which to close his design. This takes a certain amount of prescience derived from experience. The experienced designer knows the users and the industry well enough to judge the probability of different kinds of changes. He then makes sure that the open-closed principle is invoked for the most probable changes.&lt;br /&gt;
&lt;br /&gt;
==== Conventions to follow ====&lt;br /&gt;
In order to obey open-closed principle it is good to follow some conventions.&lt;br /&gt;
# Make all Member Variables Private: Member variables of classes should be known only to the methods of the class that defines them. Member&lt;br /&gt;
variables should never be known to any other class, including derived classes. Thus they should be declared private, rather than public or protected. In light of the open-closed principle, the reason for this convention ought to be clear. When the member variables of a class change, every function that depends upon those variables must be changed. Thus, no function that depends upon a variable can be closed with respect to that variable. &amp;lt;br&amp;gt;&lt;br /&gt;
# No global variables: The argument against global variables is similar to the argument against pubic member variables. No module that depends upon a global variable can be closed against any other module that might write to that variable. Any module that uses the variable in a way that&lt;br /&gt;
the other modules don’t expect, will break those other modules. It is too risky to have many modules be subject to the whim of one badly behaved one. &amp;lt;br&amp;gt;&lt;br /&gt;
# Avoid run time type identification:&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70346</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70346"/>
		<updated>2012-11-18T21:28:16Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Conventions to follow */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note that in the above solution, if we want to extend the behavior of the DrawAllShapes function in to draw a new kind of shape, all we need do is add a new derivative of the Shape class. The DrawAllShapes function does not need to change. Thus DrawAllShapes conforms to the open-closed principle. Its behavior can be extended without modifying it.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
It should be clear that no significant program can be 100% closed. For example, consider the above DrawAllShapes example. What would happen to the DrawAllShapes function if we decided that all Circles should be drawn before any Squares. The DrawAllShapes function is not closed against a change like this. In general, no matter how “closed” a module is, there will always be some kind of change against which it is not closed. Since closure cannot be complete, it must be strategic. That is, the designer must choose the kinds of changes against which to close his design. This takes a certain amount of prescience derived from experience. The experienced designer knows the users and the industry well enough to judge the probability of different kinds of changes. He then makes sure that the open-closed principle is invoked for the most probable changes.&lt;br /&gt;
&lt;br /&gt;
==== Conventions to follow ====&lt;br /&gt;
In order to obey open-closed principle it is good to follow some conventions.&lt;br /&gt;
# Make all Member Variables Private: &lt;br /&gt;
# No global variables:&lt;br /&gt;
# Avoid run time type identification:&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70345</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70345"/>
		<updated>2012-11-18T21:27:03Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note that in the above solution, if we want to extend the behavior of the DrawAllShapes function in to draw a new kind of shape, all we need do is add a new derivative of the Shape class. The DrawAllShapes function does not need to change. Thus DrawAllShapes conforms to the open-closed principle. Its behavior can be extended without modifying it.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
It should be clear that no significant program can be 100% closed. For example, consider the above DrawAllShapes example. What would happen to the DrawAllShapes function if we decided that all Circles should be drawn before any Squares. The DrawAllShapes function is not closed against a change like this. In general, no matter how “closed” a module is, there will always be some kind of change against which it is not closed. Since closure cannot be complete, it must be strategic. That is, the designer must choose the kinds of changes against which to close his design. This takes a certain amount of prescience derived from experience. The experienced designer knows the users and the industry well enough to judge the probability of different kinds of changes. He then makes sure that the open-closed principle is invoked for the most probable changes.&lt;br /&gt;
&lt;br /&gt;
== Conventions to follow ==&lt;br /&gt;
In order to obey open-closed principle it is good to follow some conventions.&lt;br /&gt;
# Make all Member Variables Private: &lt;br /&gt;
# No global variables:&lt;br /&gt;
# Avoid run time type identification:&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70319</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70319"/>
		<updated>2012-11-18T19:58:49Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Strategic Closure */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note that in the above solution, if we want to extend the behavior of the DrawAllShapes function in to draw a new kind of shape, all we need do is add a new derivative of the Shape class. The DrawAllShapes function does not need to change. Thus DrawAllShapes conforms to the open-closed principle. Its behavior can be extended without modifying it.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
It should be clear that no significant program can be 100% closed. For example, consider the above DrawAllShapes example. What would happen to the DrawAllShapes function if we decided that all Circles should be drawn before any Squares. The DrawAllShapes function is not closed against a change like this. In general, no matter how “closed” a module is, there will always be some kind of change against which it is not closed. Since closure cannot be complete, it must be strategic. That is, the designer must choose the kinds of changes against which to close his design. This takes a certain amount of prescience derived from experience. The experienced designer knows the users and the industry well enough to judge the probability of different kinds of changes. He then makes sure that the open-closed principle is invoked for the most probable changes.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70305</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70305"/>
		<updated>2012-11-18T19:42:46Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Abstraction Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note that in the above solution, if we want to extend the behavior of the DrawAllShapes function in to draw a new kind of shape, all we need do is add a new derivative of the Shape class. The DrawAllShapes function does not need to change. Thus DrawAllShapes conforms to the open-closed principle. Its behavior can be extended without modifying it.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70303</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70303"/>
		<updated>2012-11-18T19:40:57Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Abstraction Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw. Now let us look at a more better solution for this problem.&lt;br /&gt;
&lt;br /&gt;
  class Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const = 0;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  class Square : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  class Circle : public Shape&lt;br /&gt;
  {&lt;br /&gt;
    public:&lt;br /&gt;
    virtual void Draw() const;&lt;br /&gt;
  }; &amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawAllShapes(Set&amp;lt;Shape*&amp;gt;&amp;amp; list)&lt;br /&gt;
  {&lt;br /&gt;
    for (Iterator&amp;lt;Shape*&amp;gt;i(list); i; i++)&lt;br /&gt;
    (*i)-&amp;gt;Draw();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70301</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70301"/>
		<updated>2012-11-18T19:38:53Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Abstraction Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the above example, the function DrawAllShapes does not conform to the open-closed principle because it cannot be closed against new kinds of shapes. If we wanted to extend this function to be able to draw a list of shapes that included triangles, we would have to modify the function. In fact, we would have to modify the function for any new type of shape that we needed to draw.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70299</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70299"/>
		<updated>2012-11-18T19:37:20Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Abstraction Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
===== Abstraction Example =====&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70298</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70298"/>
		<updated>2012-11-18T19:37:04Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Abstraction Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
====== Abstraction Example ======&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&amp;lt;br&amp;gt;&lt;br /&gt;
  //&lt;br /&gt;
  // These functions are implemented elsewhere&lt;br /&gt;
  //&amp;lt;br&amp;gt;&lt;br /&gt;
  void DrawSquare(struct Square*)&lt;br /&gt;
  void DrawCircle(struct Circle*);&lt;br /&gt;
  typedef struct Shape *ShapePointer;&lt;br /&gt;
  void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
  {&lt;br /&gt;
    int i;&lt;br /&gt;
    for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
    {&lt;br /&gt;
      struct Shape* s = list[i];&lt;br /&gt;
      switch (s-&amp;gt;itsType)&lt;br /&gt;
      {&lt;br /&gt;
        case square:&lt;br /&gt;
          DrawSquare((struct Square*)s);&lt;br /&gt;
          break; &amp;lt;br&amp;gt;&lt;br /&gt;
        case circle:&lt;br /&gt;
          DrawCircle((struct Circle*)s);&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70296</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70296"/>
		<updated>2012-11-18T19:35:27Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Abstraction Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
====== Abstraction Example ======&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
    ShapeType itsType;&lt;br /&gt;
    double itsSide;&lt;br /&gt;
    Point itsTopLeft;&lt;br /&gt;
  };&lt;br /&gt;
//&lt;br /&gt;
// These functions are implemented elsewhere&lt;br /&gt;
//&lt;br /&gt;
void DrawSquare(struct Square*)&lt;br /&gt;
void DrawCircle(struct Circle*);&lt;br /&gt;
typedef struct Shape *ShapePointer;&lt;br /&gt;
void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
{&lt;br /&gt;
int i;&lt;br /&gt;
for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
{&lt;br /&gt;
struct Shape* s = list[i];&lt;br /&gt;
switch (s-&amp;gt;itsType)&lt;br /&gt;
{&lt;br /&gt;
case square:&lt;br /&gt;
DrawSquare((struct Square*)s);&lt;br /&gt;
break;&lt;br /&gt;
case circle:&lt;br /&gt;
DrawCircle((struct Circle*)s);&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70295</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70295"/>
		<updated>2012-11-18T19:35:10Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Abstraction Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
====== Abstraction Example ======&lt;br /&gt;
&lt;br /&gt;
  struct Square&lt;br /&gt;
  {&lt;br /&gt;
  ShapeType itsType;&lt;br /&gt;
  double itsSide;&lt;br /&gt;
  Point itsTopLeft;&lt;br /&gt;
  };&lt;br /&gt;
//&lt;br /&gt;
// These functions are implemented elsewhere&lt;br /&gt;
//&lt;br /&gt;
void DrawSquare(struct Square*)&lt;br /&gt;
void DrawCircle(struct Circle*);&lt;br /&gt;
typedef struct Shape *ShapePointer;&lt;br /&gt;
void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
{&lt;br /&gt;
int i;&lt;br /&gt;
for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
{&lt;br /&gt;
struct Shape* s = list[i];&lt;br /&gt;
switch (s-&amp;gt;itsType)&lt;br /&gt;
{&lt;br /&gt;
case square:&lt;br /&gt;
DrawSquare((struct Square*)s);&lt;br /&gt;
break;&lt;br /&gt;
case circle:&lt;br /&gt;
DrawCircle((struct Circle*)s);&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70291</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70291"/>
		<updated>2012-11-18T19:33:10Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Abstraction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
====== Abstraction Example ======&lt;br /&gt;
&lt;br /&gt;
struct Square&lt;br /&gt;
{&lt;br /&gt;
ShapeType itsType;&lt;br /&gt;
double itsSide;&lt;br /&gt;
Point itsTopLeft;&lt;br /&gt;
};&lt;br /&gt;
//&lt;br /&gt;
// These functions are implemented elsewhere&lt;br /&gt;
//&lt;br /&gt;
void DrawSquare(struct Square*)&lt;br /&gt;
void DrawCircle(struct Circle*);&lt;br /&gt;
typedef struct Shape *ShapePointer;&lt;br /&gt;
void DrawAllShapes(ShapePointer list[], int n)&lt;br /&gt;
{&lt;br /&gt;
int i;&lt;br /&gt;
for (i=0; i&amp;lt;n; i++)&lt;br /&gt;
{&lt;br /&gt;
struct Shape* s = list[i];&lt;br /&gt;
switch (s-&amp;gt;itsType)&lt;br /&gt;
{&lt;br /&gt;
case square:&lt;br /&gt;
DrawSquare((struct Square*)s);&lt;br /&gt;
break;&lt;br /&gt;
case circle:&lt;br /&gt;
DrawCircle((struct Circle*)s);&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70287</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70287"/>
		<updated>2012-11-18T19:29:09Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Abstraction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
In object oriented design, it is possible to create abstractions that are fixed and yet represent an unbounded group of possible behaviors. The abstractions are abstract base classes, and the unbounded group of possible behaviors is represented by all the possible derivative classes. It is possible for a module to manipulate an abstraction. Such a module can be closed for modification since it depends upon an abstraction that is fixed. Yet the behavior of that module can be extended by creating new derivatives of the abstraction.&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70281</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70281"/>
		<updated>2012-11-18T19:17:57Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Abstraction ====&lt;br /&gt;
&lt;br /&gt;
==== Strategic Closure ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70279</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70279"/>
		<updated>2012-11-18T19:17:16Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====Abstraction ====&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70278</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70278"/>
		<updated>2012-11-18T19:16:04Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt; &amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70275</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70275"/>
		<updated>2012-11-18T19:15:11Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction]&amp;lt;ref name = abstraction_wiki&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70274</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70274"/>
		<updated>2012-11-18T19:14:49Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction].&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = abstraction_wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70273</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70273"/>
		<updated>2012-11-18T19:13:47Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is [http://en.wikipedia.org/wiki/Abstraction_(computer_science) abstraction].&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70271</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70271"/>
		<updated>2012-11-18T19:12:25Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction wiki] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70270</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70270"/>
		<updated>2012-11-18T19:10:46Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70267</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70267"/>
		<updated>2012-11-18T19:06:26Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science)] Abstraction Wiki &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70266</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70266"/>
		<updated>2012-11-18T19:05:25Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx] Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; [http://ruby-toolbox.com/categories/testing_frameworks.html] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; [http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = wiki&amp;gt; [http://en.wikipedia.org/wiki/Abstraction_(computer_science)] Abstraction Wiki&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70265</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70265"/>
		<updated>2012-11-18T19:03:26Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt; This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt; The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
At starting, these two points seem to be very contradictory to each other. How can the module be extensible when it is not supposed to me modified? The key idea to go about this is abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70261</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70261"/>
		<updated>2012-11-18T18:57:14Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt;- This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt;- The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70260</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70260"/>
		<updated>2012-11-18T18:56:23Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Open For Extension”.&amp;lt;/b&amp;gt;&lt;br /&gt;
# This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
* &amp;lt;b&amp;gt;They are “Closed for Modification”.&amp;lt;/b&amp;gt;&lt;br /&gt;
# The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70259</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70259"/>
		<updated>2012-11-18T18:50:51Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* They are “Open For Extension”.&lt;br /&gt;
{{This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.}}&lt;br /&gt;
*They are “Closed for Modification”.&lt;br /&gt;
{{The source code of such a module is inviolate. No one is allowed to make source code changes to it.}}&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70255</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=70255"/>
		<updated>2012-11-18T18:42:29Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming] the [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle] states&amp;lt;ref name = meyer /&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification.''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code. All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the initial version.  What you want to avoid is to have one simple change ripple through the various classes of your application. That makes the system fragile, prone to regression problems, and expensive to extend. To isolate the changes, you want to write classes and methods in such a way that they never need to change once they are written&amp;lt;ref name = microsoft /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Open/Closed principle is one of the five principles basic of [http://en.wikipedia.org/wiki/OOD object-oriented design(OOD)] which are defined as the [http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 S.O.L.I.D.]. The principles of SOLID are guidelines that can be applied while working on software to remove [http://en.wikipedia.org/wiki/Code_smell code smells] by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with [http://en.wikipedia.org/wiki/Test-driven_development test-driven development], and is part of an overall strategy of [http://en.wikipedia.org/wiki/Agile_software_development agile] and [http://en.wikipedia.org/wiki/Adaptive_Software_Development adaptive programming]&amp;lt;ref name = openclosed_video /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
Every module obeying open-closed principle have the following key attributes:&lt;br /&gt;
* They are “Open For Extension”.&lt;br /&gt;
This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.&lt;br /&gt;
*They are “Closed for Modification”.&lt;br /&gt;
The source code of such a module is inviolate. No one is allowed to make source code changes to it.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Suppose you are writing a module to approve personal loans and before doing that you want to validate the personal information, code wise we can depict the situation as:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
   public void approveLoan(PersonalValidator validator)&amp;lt;br /&amp;gt;&lt;br /&gt;
   {&lt;br /&gt;
     if ( validator.isValid())&lt;br /&gt;
     {&lt;br /&gt;
        //Process the loan.&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
   public boolean isValid()&lt;br /&gt;
   {&lt;br /&gt;
     //Validation logic&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So far so good. As we have already discussed, the requirements always change and now we are required to approve vehicle loans as well. So one approach to solve this requirement is to:&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approvePersonalLoan (PersonalLoanValidator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void approveVehicleLoan (VehicleLoanValidator validator )&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Method for approving other loans.&lt;br /&gt;
 }&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
We have edited the existing class to accommodate the new requirement and in the process we ended up changing the name of the existing method and also adding new methods for different types of loan approval. This clearly violates the Open/Closed principle. Lets try to implement the requirement in a different way&lt;br /&gt;
&lt;br /&gt;
 /**&lt;br /&gt;
 * Interface Validator class Extended to add different validators for different loan type&lt;br /&gt;
 */&lt;br /&gt;
 public interface Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid();&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Personal loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class PersonalLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /**&lt;br /&gt;
 * Vehicle loan validator&lt;br /&gt;
 */&lt;br /&gt;
 public class VehicleLoanValidator&lt;br /&gt;
  implements Validator&lt;br /&gt;
 {&lt;br /&gt;
  public boolean isValid()&lt;br /&gt;
  {&lt;br /&gt;
    //Validation logic.&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 /*&lt;br /&gt;
 * Similarly any new type of validation can&lt;br /&gt;
 * be accommodated by creating a new subclass&lt;br /&gt;
 * of Validator&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
Now using the above validators we can write a LoanApprovalHandler to use the Validator abstraction.&lt;br /&gt;
&lt;br /&gt;
 public class LoanApprovalHandler&lt;br /&gt;
 {&lt;br /&gt;
  public void approveLoan(Validator validator)&lt;br /&gt;
  {&lt;br /&gt;
    if ( validator.isValid())&lt;br /&gt;
    {&lt;br /&gt;
      //Process the loan.&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
So to accommodate any type of loan validators we would just have create a subclass of Validator and then pass it to the approveLoan method. That way the class is CLOSED for modification but OPEN for extension.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In reality, achieving Open/Closed principle-compliance system-wide is not possible. There will always exist some change that the system is not closed against. Therefore, this principle must be applied judiciously to the areas of the system that are most complex and dynamic. Even partial Open Closed principle compliance results in more resilient systems. Isolating violations of Open Closed principles to specific classes, such as object factories, certainly serves to reduce the overall maintenance efforts.&amp;lt;ref name = journal/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
This principle is at the heart of object oriented design in many ways, it motivated many heuristics associated with object oriented design&amp;lt;ref name = pdf/&amp;gt;. For example, “all member variables should be private”, or “global variables should be avoided”. Conformance to this principle yields some of the great benefits such as the application will be more robust because we are not changing already tested code, flexible because we can easily accommodate new requirements. Yet conformance to this principle is not achieved simply by using an object oriented programming language. Rather, it requires a dedication on the part of the designer to apply abstraction to those parts of the program that the designer feels are going to be subject to change.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
Below are some of the patterns and principles that are used to structure code to isolate changes.&lt;br /&gt;
&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility Pattern]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Liskov_substitution_principle Liskov Substitution Principle ]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
*[http://css.dzone.com/articles/openclosed-principle-real A real world example of Open/Closed Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = meyer&amp;gt; [http://en.wikipedia.org/wiki/Bertrand_Meyer Meyer, Bertrand] (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pdf&amp;gt;  Martin, R.C. (2000). &amp;quot;Design Principles and Design Patterns.&amp;quot; http://www.objectmentor.com &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = microsoft&amp;gt; http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Patterns in Practice: The Open Closed Principle &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = openclosed_video&amp;gt; http://ruby-toolbox.com/categories/testing_frameworks.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = journal&amp;gt; http://www2.sys-con.com/ITSG/VirtualCD_Spring05/Java/archives/0702/knoernschild/index.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69639</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69639"/>
		<updated>2012-11-16T22:15:14Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Motivation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In object-oriented programming the open/closed principle states,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code&lt;br /&gt;
&lt;br /&gt;
All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the first version.”&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus following open-closed principle leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Open/closed_principle Open/closed principle]&lt;br /&gt;
&lt;br /&gt;
[2] [http://www.objectmentor.com/resources/articles/ocp.pdf &amp;quot;The Open-Closed Principle&amp;quot;, C++ Report, January 1996&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[3] [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx &amp;quot;Patterns in Practice: The Open Closed Principle&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69637</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69637"/>
		<updated>2012-11-16T22:14:18Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Motivation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In object-oriented programming the open/closed principle states,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code&lt;br /&gt;
&lt;br /&gt;
All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the first version.”&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. Thus it leads to good software design.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Open/closed_principle Open/closed principle]&lt;br /&gt;
&lt;br /&gt;
[2] [http://www.objectmentor.com/resources/articles/ocp.pdf &amp;quot;The Open-Closed Principle&amp;quot;, C++ Report, January 1996&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[3] [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx &amp;quot;Patterns in Practice: The Open Closed Principle&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69635</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69635"/>
		<updated>2012-11-16T22:11:30Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In object-oriented programming the open/closed principle states,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code&lt;br /&gt;
&lt;br /&gt;
All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the first version.”&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) SOLID - Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Open/closed_principle Open/closed principle]&lt;br /&gt;
&lt;br /&gt;
[2] [http://www.objectmentor.com/resources/articles/ocp.pdf &amp;quot;The Open-Closed Principle&amp;quot;, C++ Report, January 1996&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[3] [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx &amp;quot;Patterns in Practice: The Open Closed Principle&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69634</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69634"/>
		<updated>2012-11-16T22:09:47Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Motivation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In object-oriented programming the open/closed principle states,&lt;br /&gt;
&lt;br /&gt;
''Software entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification''&lt;br /&gt;
&lt;br /&gt;
It sounds like a contradiction in terms, but it's not. All it means is that you should structure an application so that you can add new functionality with minimal modification to existing code&lt;br /&gt;
&lt;br /&gt;
All systems change during their life cycles. One should keep this in mind when designing systems that are expected to last longer than the first version.”&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
Most of the times it easier to write all new code rather thank making changes to existing code. Modifying old code adds the risk of breaking existing functionality. With new code you generally only have to test the new functionality. When you modify old code you have to both test your changes and then perform a set of regression tests to make sure you did not break any of the existing code.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Open/closed_principle Open/closed principle]&lt;br /&gt;
&lt;br /&gt;
[2] [http://www.objectmentor.com/resources/articles/ocp.pdf &amp;quot;The Open-Closed Principle&amp;quot;, C++ Report, January 1996&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[3] [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx &amp;quot;Patterns in Practice: The Open Closed Principle&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69630</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69630"/>
		<updated>2012-11-16T22:02:36Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Motivation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In object-oriented programming the open/closed principle states,&lt;br /&gt;
&lt;br /&gt;
 ''Software entities (Classes, Modules, Functions, etc.)should be open for extension, but closed for modification''&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Open/closed_principle Open/closed principle]&lt;br /&gt;
&lt;br /&gt;
[2] [http://www.objectmentor.com/resources/articles/ocp.pdf &amp;quot;The Open-Closed Principle&amp;quot;, C++ Report, January 1996&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[3] [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx &amp;quot;Patterns in Practice: The Open Closed Principle&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69629</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69629"/>
		<updated>2012-11-16T22:02:23Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Motivation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In object-oriented programming the open/closed principle states,&lt;br /&gt;
&lt;br /&gt;
 ''Software entities (Classes, Modules, Functions, etc.)should be open for extension, but closed for modification''&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
  All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way. Open-closed principle states that the code should be designed in such a way that it should not change. Whenever requirements change, the existing code should be extended by adding new code and leave the old working code intact.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Open/closed_principle Open/closed principle]&lt;br /&gt;
&lt;br /&gt;
[2] [http://www.objectmentor.com/resources/articles/ocp.pdf &amp;quot;The Open-Closed Principle&amp;quot;, C++ Report, January 1996&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[3] [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx &amp;quot;Patterns in Practice: The Open Closed Principle&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69624</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69624"/>
		<updated>2012-11-16T21:54:41Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Motivation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In object-oriented programming the open/closed principle states,&lt;br /&gt;
&lt;br /&gt;
 ''Software entities (Classes, Modules, Functions, etc.)should be open for extension, but closed for modification''&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
All software systems are subject to change. Thus designing a system which is stable is a very crucial task. When a single change to a program results in a cascade of changes to dependent modules, that program exhibits the undesirable attributes that we have come to associate with “bad”&lt;br /&gt;
design. The program becomes fragile, rigid, unpredictable and unreusable. The open-closed principle attacks this in a very straightforward way.&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Open/closed_principle Open/closed principle]&lt;br /&gt;
&lt;br /&gt;
[2] [http://www.objectmentor.com/resources/articles/ocp.pdf &amp;quot;The Open-Closed Principle&amp;quot;, C++ Report, January 1996&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[3] [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx &amp;quot;Patterns in Practice: The Open Closed Principle&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69615</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69615"/>
		<updated>2012-11-16T21:24:47Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Intent */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
== Motivation ==&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Open/closed_principle Open/closed principle]&lt;br /&gt;
&lt;br /&gt;
[2] [http://www.objectmentor.com/resources/articles/ocp.pdf &amp;quot;The Open-Closed Principle&amp;quot;, C++ Report, January 1996&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[3] [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx &amp;quot;Patterns in Practice: The Open Closed Principle&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69612</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69612"/>
		<updated>2012-11-16T21:21:30Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* The Open/Closed principle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
== Intent ==&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Single_responsibility_principle Single-Responsibility Principle]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Open/closed_principle Open/closed principle]&lt;br /&gt;
&lt;br /&gt;
[2] [http://www.objectmentor.com/resources/articles/ocp.pdf &amp;quot;The Open-Closed Principle&amp;quot;, C++ Report, January 1996&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[3] [http://msdn.microsoft.com/en-us/magazine/cc546578.aspx &amp;quot;Patterns in Practice: The Open Closed Principle&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69606</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w69 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w69_as&amp;diff=69606"/>
		<updated>2012-11-16T21:06:47Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: Created page with &amp;quot;=The Open/Closed principle= __TOC__   == Introduction ==  == Intent ==  == Description ==  == Examples ==  == Conclusion ==  == References ==&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The Open/Closed principle=&lt;br /&gt;
__TOC__ &lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
== Intent ==&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=69603</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=69603"/>
		<updated>2012-11-16T21:00:55Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: Open/close principle&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/Table_Of_Contents]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w29 st]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w3_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w30 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w17 pt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w31 up]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w9 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w19 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w26 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w16 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w8 vp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w3 jm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w23 sr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w11_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w15 rr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w33 pv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w20_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w14_bb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w21_ap]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w13_sm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w4_sa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w25_nr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w12_sv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w7_ma]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w6_ar]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w32_mk]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w10_rc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w70_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w67_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w40_sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w22_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w-1w65_am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w59_bc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w60_ns]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w69_as]]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65477</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w27 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65477"/>
		<updated>2012-09-21T19:21:35Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Aspect Oriented Programming (AOP): AspectJ and AspectR=&lt;br /&gt;
&lt;br /&gt;
Aspect Oriented Programming (AOP) refers to a new way of designing software. It aims to increase modularity by allowing the separation of cross-cutting concerns. AOP includes programming methods and tools that support the modularization of concerns at the level of the source code. It is not a replacement to popular Object Oriented Programming (OOP), but is complimentary to it.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Aspect-oriented programming entails breaking down program logic into distinct parts, called concerns. Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they &amp;quot;cut across&amp;quot; multiple abstractions in a program. An example to this is logging. In an normal object oriented language program we might need to invoke a logger function from everywhere we this functionality.&lt;br /&gt;
&lt;br /&gt;
==Why do we need AOP? The banking example==&lt;br /&gt;
Typically, an aspect is scattered or tangled as code, making it harder to understand and maintain. It is scattered by virtue of the function (such as logging) being spread over a number of unrelated functions that might use its function, possibly in entirely unrelated systems, different source languages, etc. That means to change logging can require modifying all affected modules. Aspects become tangled not only with the mainline function of the systems in which they are expressed but also with each other. That means changing one concern entails understanding all the tangled concerns or having some means by which the effect of changes can be inferred.&lt;br /&gt;
&lt;br /&gt;
Consider a simple banking application written in Ruby.&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds'&lt;br /&gt;
 class BankAccount&amp;lt;br/&amp;gt;&lt;br /&gt;
   attr_accessor :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = 0.0&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFunds&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
This is simple enough. But is not very useful. What if we need persistence too?&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds_error'&amp;lt;br/&amp;gt;&lt;br /&gt;
 class BankAccount&lt;br /&gt;
   attr_reader :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def balance=(amount)&lt;br /&gt;
     @balance = amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = retrieve_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFundsError&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def retrieve_balance&lt;br /&gt;
     # Get from database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def persist_balance&lt;br /&gt;
     # Save to database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
We can see that now other interests have become tangled with the basic functionality (sometimes called the business logic concern). Now add:&lt;br /&gt;
&lt;br /&gt;
* Transactional behavior&lt;br /&gt;
* Authentication and authorization&lt;br /&gt;
* “Observers”, like a UI&lt;br /&gt;
&lt;br /&gt;
Also this code will be repeated across other domain objects which need the same behavior. Finally, attempt to reuse this code in a new application with a different persistence store, transaction engine, security model etc.&lt;br /&gt;
&lt;br /&gt;
===The Problem===&lt;br /&gt;
&lt;br /&gt;
* Persistence itself is already modular&lt;br /&gt;
** But the code that uses it isn’t!&lt;br /&gt;
** Persistence invocation code is scattered&lt;br /&gt;
** BankAccount code is tangled with code from other concerns&lt;br /&gt;
** BankAccount’s modularity is compromised&lt;br /&gt;
* Also, similar persistence code is added to other components&lt;br /&gt;
** Redundancy:&lt;br /&gt;
*** Hard to change all occurrences consistently.&lt;br /&gt;
*** Hard to replace the Persistence solution.&lt;br /&gt;
*** Breaks Don’t Repeat Yourself! (DRY)&lt;br /&gt;
** Reuse is hard.&lt;br /&gt;
*** The persistence solution may be different in another context.&lt;br /&gt;
&lt;br /&gt;
===The Solution: Aspect Oriented Programming===&lt;br /&gt;
AOP attempts to solve this problem by allowing the programmer to express cross-cutting concerns in stand-alone modules called aspects. Then it provides mechanisms for fine-grained modification of target component behavior to incorporate the aspects’ behaviors. Aspects can contain advice (code joined to specified points in the program) and inter-type declarations (structural members added to other classes). For example, a security module can include advice that performs a security check before accessing a bank account.&lt;br /&gt;
&lt;br /&gt;
For example in the above program, the behavior is modified by adding a 'Persistence aspect' to the program. We specify separately where these aspect should be invoked in the banking account application. Now the banking account remains agnostic and the aspect code is maintained in one place. Also, now it is easy to change to a different solution or to remove the concern altogether.&lt;br /&gt;
&lt;br /&gt;
We will see how this is implemented exactly in various implementation of AOP such as AspectJ and AspectR below.&lt;br /&gt;
&lt;br /&gt;
==AOP Concepts==&lt;br /&gt;
The advice-related component of an aspect-oriented language defines a join point model (JPM). A JPM defines three things:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Joinpoint&amp;lt;/b&amp;gt; - a well defined event in the execution of a program (such as the core functionality provided by the persistence class). eg: A call to a method persist_balance from inside the withdraw method.&lt;br /&gt;
* &amp;lt;b&amp;gt;Pointcut&amp;lt;/b&amp;gt; - A collection of join points. eg: A collection of all mutator methods in BankingAccount.&lt;br /&gt;
* &amp;lt;b&amp;gt;Advice&amp;lt;/b&amp;gt; - A block of code that specifies some behavior to be executed before/after/around a certain joinpoint. &lt;br /&gt;
&lt;br /&gt;
Join-point models can be compared based on the join points exposed, how join points are specified, the operations permitted at the join points, and the structural enhancements that can be expressed.&lt;br /&gt;
&lt;br /&gt;
==AspectJ: AOP in Java==&lt;br /&gt;
AspectJ extend the Java programming language with constructs to support AOP. It is a superset of Java such that each valid Java program is also a valid AspectJ program. Also, it is designed as a general purpose language as opposed to a domain specific language. AspectJ is currently the most notable AOP technology.&lt;br /&gt;
&lt;br /&gt;
AspectJ was created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become the widely used de-facto standard for AOP by emphasizing simplicity and usability for end users. It has included IDE integrations for displaying crosscutting structure since its initial public release in 2001.&lt;br /&gt;
&lt;br /&gt;
===History and contributors===&lt;br /&gt;
Gregor Kiczales started and led the Xerox PARC team that eventually developed AspectJ; he coined the term &amp;quot;crosscutting&amp;quot;. Fourth on the team, Chris Maeda coined the term &amp;quot;aspect-oriented programming.&amp;quot; Jim Hugunin and Erik Hilsdale (Xerox PARC team members 12 and 13) were the original compiler and weaver engineers, Mik Kersten implemented the IDE integration and started the Eclipse AJDT project with Adrian Colyer (current lead of the AspectJ project) and Andrew Clement (current compiler engineer).&lt;br /&gt;
&lt;br /&gt;
The AspectBench Compiler was developed and is being maintained as a joint effort of the Programming Tools Group at the Oxford University Computing Laboratory, the Sable Research Group at McGill University and the Institute for Basic Research in Computer Science (BRICS).&lt;br /&gt;
&lt;br /&gt;
===Lanugage description===&lt;br /&gt;
All valid Java programs are also valid AspectJ programs, but AspectJ also allows programmers to define special constructs called aspects. Aspects can contain several entities unavailable to standard classes. These are:&lt;br /&gt;
&lt;br /&gt;
====Inter-type declarations==== &lt;br /&gt;
These allow a programmer to add methods, fields, or interfaces to existing classes from within the aspect. This example adds an acceptVisitor (see visitor pattern) method to the Point class:&lt;br /&gt;
&lt;br /&gt;
  aspect VisitAspect {&lt;br /&gt;
    void Point.acceptVisitor(Visitor v) {&lt;br /&gt;
      v.visit(this);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
====Pointcuts==== &lt;br /&gt;
Pointcuts allow a programmer to specify join points. All pointcuts are expressions (quantifications) that determine whether a given join point matches. A call joinpoint captures an execution event after it evaluates a method calls' arguments, but before it calls the method itself.&lt;br /&gt;
&lt;br /&gt;
For example, this point-cut specifies all calls to GetBalance method in the class BankingAccount that takes no arguments and returns long&lt;br /&gt;
&lt;br /&gt;
  call(public long BankingAccount.GetBalance())&lt;br /&gt;
&lt;br /&gt;
We may also use logical operators in the definition of pointcuts inorder to combine pointcut expressions.&lt;br /&gt;
&lt;br /&gt;
# || (OR operator) - Matches a joinpoint if either left pointcut expression or right pointcut expression matches&lt;br /&gt;
# &amp;amp;&amp;amp; (AND operator) - Matches a joinpoint if both left pointcut expression and right pointcut expression matches&lt;br /&gt;
# ! (NOT operator) - Matches all joinpoints NOT specified by the pointcut expression&lt;br /&gt;
&lt;br /&gt;
Example of a pointcut which matches both mutator methods of the BankingAccount class is&lt;br /&gt;
&lt;br /&gt;
  pointcut mutators(): call(public long BankingAccount.Withdraw(long)) || call(public long BankingAccount.Deposit(long))&lt;br /&gt;
&lt;br /&gt;
Various types of joinpoints that are possible in AspectJ are:&lt;br /&gt;
# Calls to methods and constructors&lt;br /&gt;
# Execution of methods and constructors&lt;br /&gt;
# Field access&lt;br /&gt;
# Exception handling&lt;br /&gt;
# Class initializaiton&lt;br /&gt;
# Lexical structure&lt;br /&gt;
# Control flow&lt;br /&gt;
# Self-target and argument type&lt;br /&gt;
# Conditional test&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Patterns in pointcuts&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Patterns can also be used to specify events in pointcuts. This would be useful to specify a set of events concisely.&lt;br /&gt;
&lt;br /&gt;
The use of * character is highly overloaded.&lt;br /&gt;
* Using * operator where a type is expected matches any type&lt;br /&gt;
* Using * where an identifier is expected matches any identifier&lt;br /&gt;
* * can also be used in identifier patterns&lt;br /&gt;
** the expression foo* would match any identifier that starts with foo&lt;br /&gt;
** the expression *foo* would match any identifier that has foo in it&lt;br /&gt;
* We can add + to the end of any identifier pattern to indicate that we wish to match a class and all of its subclasses&lt;br /&gt;
&lt;br /&gt;
Examples&lt;br /&gt;
* foo - class foo&lt;br /&gt;
* foo+ - class foo and all of its subclasses&lt;br /&gt;
* foo* - all classes starting with foo&lt;br /&gt;
* foo*+ - all classes starting with foo and all of their subclasses&lt;br /&gt;
* *foo* - all classes with foo in it&lt;br /&gt;
&lt;br /&gt;
====Advice==== &lt;br /&gt;
Advice allows a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. An advice must be define with respect to a pointcut. Given below is an advice defined with respect to the mutators pointcut.&lt;br /&gt;
&lt;br /&gt;
  before() : mutators() {&lt;br /&gt;
    System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This is a special type of mutator called the before mutator which is called before the joinpoint specified by the pointcut is executed.&lt;br /&gt;
&lt;br /&gt;
There are three ways to associate an advice with a pointcut:&lt;br /&gt;
# Before - run just before the pointcut&lt;br /&gt;
# After - run just after the pointcut&lt;br /&gt;
# Around - Runs instead of the pointcut with the provision for the pointcut to resume execution through a method called proceed().&lt;br /&gt;
&lt;br /&gt;
AspectJ also supports limited forms of pointcut-based static checking and aspect reuse (by inheritance). Pointcuts and advices together define composition (weaving) rules.&lt;br /&gt;
&lt;br /&gt;
====Aspect====&lt;br /&gt;
Much like a class, an Aspect is a unit of modularity. It is defined in terms of pointcuts, advices and ordinary java fields and methods. Pointcuts say which event to match and advice body says what to execute when it matches.&lt;br /&gt;
&lt;br /&gt;
  public aspect Tracer {&lt;br /&gt;
    pointcut mutators(): call(public long BankingAccount.Withdraw(long)) ||&lt;br /&gt;
                           call(public long BankingAccount.Deposit(long));&amp;lt;br/&amp;gt;&lt;br /&gt;
    before() : mutators() {&lt;br /&gt;
      System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Banking example in Java using AOP===&lt;br /&gt;
Now let us look at how persistence may be added to the BankingAccount example using AspectJ&lt;br /&gt;
&lt;br /&gt;
  class BankAccount {&amp;lt;br/&amp;gt;&lt;br /&gt;
    float balance;&amp;lt;br/&amp;gt;&lt;br /&gt;
    public BankAccount(float balance) {&lt;br /&gt;
      this.balance = balance;&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void deposit(float amount) {&lt;br /&gt;
      balance += amount&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void withdraw(float amount) {&lt;br /&gt;
      if (balance &amp;lt; amount) {&lt;br /&gt;
        throw new InsufficientFundsException();&lt;br /&gt;
      }&lt;br /&gt;
      balance -= amount;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  aspect PersistenceAspect {&amp;lt;br/&amp;gt;&lt;br /&gt;
    pointcut stateChange(BankAccount ba):&lt;br /&gt;
      (call(void BankAccount.deposit(*)) ||&lt;br /&gt;
      call(void BankAccount.withdraw(*)))&lt;br /&gt;
      &amp;amp;&amp;amp; target(ba);&amp;lt;br/&amp;gt;&lt;br /&gt;
    after(BankAccount ba) returning: stateChange(ba) {&lt;br /&gt;
      // persist ba.getBalance() value to database&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We define the pointcut stateChange as calls to the mutator functions of the BankAccount class, which are the deposit and withdraw functions in the PersistenceAspect. The persistence action should be performed after the above mentioned pointcut. So we define an advice returning, after the pointcut stateChange. So this code will execute everytime after the withdraw and deposit functions and the balance will be persisted.&lt;br /&gt;
&lt;br /&gt;
Thus the BankAccount class contains code only related to the BankAccount. It is completely free of any cross-cutting concerns such as persistence. Other concerns such as user authentication might be implemented similarly.&lt;br /&gt;
&lt;br /&gt;
==AOP in Ruby==&lt;br /&gt;
Ruby does not have native AOP support. But Ruby's metaprogramming features cover many of the aspect requirements. 'method_missing' and reflection in Ruby are classis examples of that. 'method_missing' allows the code to inspect the method call at runtime and redirect to some default method or define a new method if the called method does not exists. Similarly Ruby provides naive support to implement pointcuts and wrapper methods. They are discussed below.&lt;br /&gt;
&lt;br /&gt;
===Pointcut equivalent in Ruby===&lt;br /&gt;
There are no pointcuts in Ruby. But they can be simulated by the inspect method that Ruby provides. &lt;br /&gt;
&lt;br /&gt;
  # List all the classes&lt;br /&gt;
  ObjectSpace.each_object(Class) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the modules&lt;br /&gt;
  ObjectSpace.each_object(Module) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the instances of class String&lt;br /&gt;
  ObjectSpace.each_object(String) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Adding advice in Ruby===&lt;br /&gt;
In Ruby, alias can be used to wrap methods with advice.&lt;br /&gt;
&lt;br /&gt;
  foo.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;bar&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  foo_advice.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
  alias old_bar bar&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;{[&amp;lt; &amp;quot;+ old_bar+ &amp;quot; &amp;gt;]}”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In the above code, the alias will be executed eveytime a call to method 'bar' is made. The output of the above code will be as follows:&lt;br /&gt;
&lt;br /&gt;
  puts Foo.new.bar&lt;br /&gt;
  =&amp;gt; &amp;quot;{[&amp;lt; bar &amp;gt;]}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==AspectR==&lt;br /&gt;
We saw how AOP can be implemented in Ruby without any extra support in the above sections. AspectR is free library which provides API's to the same for Ruby. Essentially it allows you to wrap code around existing methods in your classes. This can be a good thing (and even affect how you design your code) since it separates different concerns into different parts of the code. The code for AspectR can be downloaded from [http://prdownloads.sf.net/aspectr/aspectr-0-3-5.tar.gz here]. AspectR was originally called advice.rb and written by Avi Bryant. Later Robert Feldt changed/tweaked/extended it.&lt;br /&gt;
&lt;br /&gt;
===Main features of AspectR===&lt;br /&gt;
*Joinpoints: Ruby does not have the notion of Jointpoints. But it can be considered as method or constructor calls or even exception handlers. &lt;br /&gt;
*Advices: It contains before (pre) advice, after returning and after throwing (post) advice, which indicates what is to be done before/after the jointpoint is called. &lt;br /&gt;
*Aspects: They are the classes that inherit from Aspect. It supports &amp;quot;abstract&amp;quot; Aspects and overriding between advices. &lt;br /&gt;
*Wildcards: RegExp can be used in pointcut designators, ie. to specify classes and methods to wrap advice's to. &lt;br /&gt;
*Pointcut parameters advices can access: object and method receiving call, arguments to call, return values, and exceptions raised.&lt;br /&gt;
&lt;br /&gt;
===AspectR Syntax===&lt;br /&gt;
Advice methods are passed a variable number of parameters:&lt;br /&gt;
*the first is the name of the method currently being wrapped&lt;br /&gt;
*the second is the object receiving the method call&lt;br /&gt;
*the third is the exit/return status:&lt;br /&gt;
**Array with return value(s) if the method exited normally&lt;br /&gt;
**true if the method exited with an exception&lt;br /&gt;
**nil if the method hasn't yet exited (for preAdvice)&lt;br /&gt;
*the rest are the arguments that were passed to the wrapped method.&lt;br /&gt;
&lt;br /&gt;
Here is the sample syntax:&lt;br /&gt;
  require 'aspectr'&lt;br /&gt;
  include AspectR&lt;br /&gt;
  class MyAspect &amp;lt; Aspect&lt;br /&gt;
    def someAdviceMethod(method, object, exitstatus, *args)&lt;br /&gt;
      ...&lt;br /&gt;
    end&lt;br /&gt;
      ... some other advice methods ...&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  ma = MyAspect.new&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, ... methods to wrap...)&lt;br /&gt;
  or&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, /patternToWrap/)&lt;br /&gt;
  or&lt;br /&gt;
  AspectR.wrap_classes(ma, :preAdvice, :postAdvice,&lt;br /&gt;
                      [Class1, Class2], ...methods to wrap...)&lt;br /&gt;
===Banking account example in AspectR===&lt;br /&gt;
&lt;br /&gt;
  class BankAccount&lt;br /&gt;
    attr_reader :balance&lt;br /&gt;
    def balance=(amount)&lt;br /&gt;
      @balance = amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def initialize&lt;br /&gt;
      @balance = retrieve_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def deposit(amount)&lt;br /&gt;
      @balance += amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def withdraw(amount)&lt;br /&gt;
      if @balance &amp;lt; amount&lt;br /&gt;
        raise InsufficientFundsError&lt;br /&gt;
      end&lt;br /&gt;
      @balance -= amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  class Persist &amp;lt; Aspect&lt;br /&gt;
    def retrieve_balance &lt;br /&gt;
      # Get from database&lt;br /&gt;
         ..&lt;br /&gt;
    end&lt;br /&gt;
    def persist_balance &lt;br /&gt;
      # Save to database&lt;br /&gt;
        ..&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  Persist.new.wrap(BankingAccount, :retrive_balance, :persist_balance, /[withdraw|deposit]/)&lt;br /&gt;
&lt;br /&gt;
In the above code class persists is a subclass of Aspect and has the before(retrive_balance) and after(persist_advice) advice defined in it. Then the last line of the code wraps the this persist class to the BankingAccount's withdraw and deposit methods. It also specifies what pre-advice and what post-advice should be used. The API for wrap class is as follows:&lt;br /&gt;
&lt;br /&gt;
  def wrap(target, pre, post, *args)		&lt;br /&gt;
    get_methods(target, args).each do |method_to_wrap|&lt;br /&gt;
      add_advice(target, PRE, method_to_wrap, pre)&lt;br /&gt;
      add_advice(target, POST, method_to_wrap, post)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Thus as we can see from API, the wrap method directly adds the pre and post advice to the methods (Join-points in AspectJ terminology).&lt;br /&gt;
&lt;br /&gt;
==How AspectR is different from AspectJ==&lt;br /&gt;
*Join points: Join points are not defined separately in AspectR as they are in AspectJ. Instead the method and constructor calls acts as join points. &lt;br /&gt;
*Most of the point cut designator primitives that are present in AspectR differ from AspectR.&lt;br /&gt;
*There is a huge difference in composition of pointcut designators. But you can of course specify several method calls in different classes and objects in a point cut. &lt;br /&gt;
*Around advices are more easy to add in AspectR than AspectJ but mostly before and after advices are used.&lt;br /&gt;
*AspectR has control-flow based crosscutting.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Aspect Oriented Programming is a very powerful concept which enables programmers to separate the cross-cutting concerns. If provides flexibility in decomposing source code into modules. But it lacks popularity. There is no native support for this in popular programming languages like Java. Thus it requires learning new framework. Though there is some native support for APO in Ruby, it does not offer all features of AOP. The AspectR library has these limitations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Aspect-oriented_programming Aspect Oriented Programming - wikipedia]&lt;br /&gt;
# [http://www.sable.mcgill.ca/~hendren/303/Slides/AspectJ/AspectJIntro_handout.pdf An introduction to Aspect-Oriented Programming with AspectJ by Constantinos Constantinides, McGill University]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/AspectJ AspectJ - wikipedia]&lt;br /&gt;
# [http://aspectr.sourceforge.net/ AspectR - Simple aspect-oriented programming in Ruby]&lt;br /&gt;
# [http://www.ruby-doc.org/gems/docs/a/aspectr-0.3.7/AspectR.html AspectR - Ruby documentation]&lt;br /&gt;
# [http://objectmentor.com/resources/articles/AOP_in_Ruby.pdf Aspect-Oriented Programming in Ruby by Dean Wampler]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65476</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w27 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65476"/>
		<updated>2012-09-21T19:20:34Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Aspect Oriented Programming (AOP): AspectJ and AspectR=&lt;br /&gt;
&lt;br /&gt;
Aspect Oriented Programming (AOP) refers to a new way of designing software. It aims to increase modularity by allowing the separation of cross-cutting concerns. AOP includes programming methods and tools that support the modularization of concerns at the level of the source code. It is not a replacement to popular Object Oriented Programming (OOP), but is complimentary to it.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Aspect-oriented programming entails breaking down program logic into distinct parts, called concerns. Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they &amp;quot;cut across&amp;quot; multiple abstractions in a program. An example to this is logging. In an normal object oriented language program we might need to invoke a logger function from everywhere we this functionality.&lt;br /&gt;
&lt;br /&gt;
==Why do we need AOP? The banking example==&lt;br /&gt;
Typically, an aspect is scattered or tangled as code, making it harder to understand and maintain. It is scattered by virtue of the function (such as logging) being spread over a number of unrelated functions that might use its function, possibly in entirely unrelated systems, different source languages, etc. That means to change logging can require modifying all affected modules. Aspects become tangled not only with the mainline function of the systems in which they are expressed but also with each other. That means changing one concern entails understanding all the tangled concerns or having some means by which the effect of changes can be inferred.&lt;br /&gt;
&lt;br /&gt;
Consider a simple banking application written in Ruby.&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds'&lt;br /&gt;
 class BankAccount&amp;lt;br/&amp;gt;&lt;br /&gt;
   attr_accessor :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = 0.0&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFunds&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
This is simple enough. But is not very useful. What if we need persistence too?&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds_error'&amp;lt;br/&amp;gt;&lt;br /&gt;
 class BankAccount&lt;br /&gt;
   attr_reader :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def balance=(amount)&lt;br /&gt;
     @balance = amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = retrieve_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFundsError&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def retrieve_balance&lt;br /&gt;
     # Get from database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def persist_balance&lt;br /&gt;
     # Save to database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
We can see that now other interests have become tangled with the basic functionality (sometimes called the business logic concern). Now add:&lt;br /&gt;
&lt;br /&gt;
* Transactional behavior&lt;br /&gt;
* Authentication and authorization&lt;br /&gt;
* “Observers”, like a UI&lt;br /&gt;
&lt;br /&gt;
Also this code will be repeated across other domain objects which need the same behavior. Finally, attempt to reuse this code in a new application with a different persistence store, transaction engine, security model etc.&lt;br /&gt;
&lt;br /&gt;
===The Problem===&lt;br /&gt;
&lt;br /&gt;
* Persistence itself is already modular&lt;br /&gt;
** But the code that uses it isn’t!&lt;br /&gt;
** Persistence invocation code is scattered&lt;br /&gt;
** BankAccount code is tangled with code from other concerns&lt;br /&gt;
** BankAccount’s modularity is compromised&lt;br /&gt;
* Also, similar persistence code is added to other components&lt;br /&gt;
** Redundancy:&lt;br /&gt;
*** Hard to change all occurrences consistently.&lt;br /&gt;
*** Hard to replace the Persistence solution.&lt;br /&gt;
*** Breaks Don’t Repeat Yourself! (DRY)&lt;br /&gt;
** Reuse is hard.&lt;br /&gt;
*** The persistence solution may be different in another context.&lt;br /&gt;
&lt;br /&gt;
===The Solution: Aspect Oriented Programming===&lt;br /&gt;
AOP attempts to solve this problem by allowing the programmer to express cross-cutting concerns in stand-alone modules called aspects. Then it provides mechanisms for fine-grained modification of target component behavior to incorporate the aspects’ behaviors. Aspects can contain advice (code joined to specified points in the program) and inter-type declarations (structural members added to other classes). For example, a security module can include advice that performs a security check before accessing a bank account.&lt;br /&gt;
&lt;br /&gt;
For example in the above program, the behavior is modified by adding a 'Persistence aspect' to the program. We specify separately where these aspect should be invoked in the banking account application. Now the banking account remains agnostic and the aspect code is maintained in one place. Also, now it is easy to change to a different solution or to remove the concern altogether.&lt;br /&gt;
&lt;br /&gt;
We will see how this is implemented exactly in various implementation of AOP such as AspectJ and AspectR below.&lt;br /&gt;
&lt;br /&gt;
==AOP Concepts==&lt;br /&gt;
The advice-related component of an aspect-oriented language defines a join point model (JPM). A JPM defines three things:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Joinpoint&amp;lt;/b&amp;gt; - a well defined event in the execution of a program (such as the core functionality provided by the persistence class). eg: A call to a method persist_balance from inside the withdraw method.&lt;br /&gt;
* &amp;lt;b&amp;gt;Pointcut&amp;lt;/b&amp;gt; - A collection of join points. eg: A collection of all mutator methods in BankingAccount.&lt;br /&gt;
* &amp;lt;b&amp;gt;Advice&amp;lt;/b&amp;gt; - A block of code that specifies some behavior to be executed before/after/around a certain joinpoint. &lt;br /&gt;
&lt;br /&gt;
Join-point models can be compared based on the join points exposed, how join points are specified, the operations permitted at the join points, and the structural enhancements that can be expressed.&lt;br /&gt;
&lt;br /&gt;
==AspectJ: AOP in Java==&lt;br /&gt;
AspectJ extend the Java programming language with constructs to support AOP. It is a superset of Java such that each valid Java program is also a valid AspectJ program. Also, it is designed as a general purpose language as opposed to a domain specific language. AspectJ is currently the most notable AOP technology.&lt;br /&gt;
&lt;br /&gt;
AspectJ was created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become the widely used de-facto standard for AOP by emphasizing simplicity and usability for end users. It has included IDE integrations for displaying crosscutting structure since its initial public release in 2001.&lt;br /&gt;
&lt;br /&gt;
===History and contributors===&lt;br /&gt;
Gregor Kiczales started and led the Xerox PARC team that eventually developed AspectJ; he coined the term &amp;quot;crosscutting&amp;quot;. Fourth on the team, Chris Maeda coined the term &amp;quot;aspect-oriented programming.&amp;quot; Jim Hugunin and Erik Hilsdale (Xerox PARC team members 12 and 13) were the original compiler and weaver engineers, Mik Kersten implemented the IDE integration and started the Eclipse AJDT project with Adrian Colyer (current lead of the AspectJ project) and Andrew Clement (current compiler engineer).&lt;br /&gt;
&lt;br /&gt;
The AspectBench Compiler was developed and is being maintained as a joint effort of the Programming Tools Group at the Oxford University Computing Laboratory, the Sable Research Group at McGill University and the Institute for Basic Research in Computer Science (BRICS).&lt;br /&gt;
&lt;br /&gt;
===Lanugage description===&lt;br /&gt;
All valid Java programs are also valid AspectJ programs, but AspectJ also allows programmers to define special constructs called aspects. Aspects can contain several entities unavailable to standard classes. These are:&lt;br /&gt;
&lt;br /&gt;
====Inter-type declarations==== &lt;br /&gt;
These allow a programmer to add methods, fields, or interfaces to existing classes from within the aspect. This example adds an acceptVisitor (see visitor pattern) method to the Point class:&lt;br /&gt;
&lt;br /&gt;
  aspect VisitAspect {&lt;br /&gt;
    void Point.acceptVisitor(Visitor v) {&lt;br /&gt;
      v.visit(this);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
====Pointcuts==== &lt;br /&gt;
Pointcuts allow a programmer to specify join points. All pointcuts are expressions (quantifications) that determine whether a given join point matches. A call joinpoint captures an execution event after it evaluates a method calls' arguments, but before it calls the method itself.&lt;br /&gt;
&lt;br /&gt;
For example, this point-cut specifies all calls to GetBalance method in the class BankingAccount that takes no arguments and returns long&lt;br /&gt;
&lt;br /&gt;
  call(public long BankingAccount.GetBalance())&lt;br /&gt;
&lt;br /&gt;
We may also use logical operators in the definition of pointcuts inorder to combine pointcut expressions.&lt;br /&gt;
&lt;br /&gt;
# || (OR operator) - Matches a joinpoint if either left pointcut expression or right pointcut expression matches&lt;br /&gt;
# &amp;amp;&amp;amp; (AND operator) - Matches a joinpoint if both left pointcut expression and right pointcut expression matches&lt;br /&gt;
# ! (NOT operator) - Matches all joinpoints NOT specified by the pointcut expression&lt;br /&gt;
&lt;br /&gt;
Example of a pointcut which matches both mutator methods of the BankingAccount class is&lt;br /&gt;
&lt;br /&gt;
  pointcut mutators(): call(public long BankingAccount.Withdraw(long)) || call(public long BankingAccount.Deposit(long))&lt;br /&gt;
&lt;br /&gt;
Various types of joinpoints that are possible in AspectJ are:&lt;br /&gt;
# Calls to methods and constructors&lt;br /&gt;
# Execution of methods and constructors&lt;br /&gt;
# Field access&lt;br /&gt;
# Exception handling&lt;br /&gt;
# Class initializaiton&lt;br /&gt;
# Lexical structure&lt;br /&gt;
# Control flow&lt;br /&gt;
# Self-target and argument type&lt;br /&gt;
# Conditional test&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Patterns in pointcuts&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Patterns can also be used to specify events in pointcuts. This would be useful to specify a set of events concisely.&lt;br /&gt;
&lt;br /&gt;
The use of * character is highly overloaded.&lt;br /&gt;
* Using * operator where a type is expected matches any type&lt;br /&gt;
* Using * where an identifier is expected matches any identifier&lt;br /&gt;
* * can also be used in identifier patterns&lt;br /&gt;
** the expression foo* would match any identifier that starts with foo&lt;br /&gt;
** the expression *foo* would match any identifier that has foo in it&lt;br /&gt;
* We can add + to the end of any identifier pattern to indicate that we wish to match a class and all of its subclasses&lt;br /&gt;
&lt;br /&gt;
Examples&lt;br /&gt;
* foo - class foo&lt;br /&gt;
* foo+ - class foo and all of its subclasses&lt;br /&gt;
* foo* - all classes starting with foo&lt;br /&gt;
* foo*+ - all classes starting with foo and all of their subclasses&lt;br /&gt;
* *foo* - all classes with foo in it&lt;br /&gt;
&lt;br /&gt;
====Advice==== &lt;br /&gt;
Advice allows a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. An advice must be define with respect to a pointcut. Given below is an advice defined with respect to the mutators pointcut.&lt;br /&gt;
&lt;br /&gt;
  before() : mutators() {&lt;br /&gt;
    System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This is a special type of mutator called the before mutator which is called before the joinpoint specified by the pointcut is executed.&lt;br /&gt;
&lt;br /&gt;
There are three ways to associate an advice with a pointcut:&lt;br /&gt;
# Before - run just before the pointcut&lt;br /&gt;
# After - run just after the pointcut&lt;br /&gt;
# Around - Runs instead of the pointcut with the provision for the pointcut to resume execution through a method called proceed().&lt;br /&gt;
&lt;br /&gt;
AspectJ also supports limited forms of pointcut-based static checking and aspect reuse (by inheritance). Pointcuts and advices together define composition (weaving) rules.&lt;br /&gt;
&lt;br /&gt;
====Aspect====&lt;br /&gt;
Much like a class, an Aspect is a unit of modularity. It is defined in terms of pointcuts, advices and ordinary java fields and methods. Pointcuts say which event to match and advice body says what to execute when it matches.&lt;br /&gt;
&lt;br /&gt;
  public aspect Tracer {&lt;br /&gt;
    pointcut mutators(): call(public long BankingAccount.Withdraw(long)) ||&lt;br /&gt;
                           call(public long BankingAccount.Deposit(long));&amp;lt;br/&amp;gt;&lt;br /&gt;
    before() : mutators() {&lt;br /&gt;
      System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Banking example in Java using AOP===&lt;br /&gt;
Now let us look at how persistence may be added to the BankingAccount example using AspectJ&lt;br /&gt;
&lt;br /&gt;
  class BankAccount {&amp;lt;br/&amp;gt;&lt;br /&gt;
    float balance;&amp;lt;br/&amp;gt;&lt;br /&gt;
    public BankAccount(float balance) {&lt;br /&gt;
      this.balance = balance;&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void deposit(float amount) {&lt;br /&gt;
      balance += amount&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void withdraw(float amount) {&lt;br /&gt;
      if (balance &amp;lt; amount) {&lt;br /&gt;
        throw new InsufficientFundsException();&lt;br /&gt;
      }&lt;br /&gt;
      balance -= amount;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  aspect PersistenceAspect {&amp;lt;br/&amp;gt;&lt;br /&gt;
    pointcut stateChange(BankAccount ba):&lt;br /&gt;
      (call(void BankAccount.deposit(*)) ||&lt;br /&gt;
      call(void BankAccount.withdraw(*)))&lt;br /&gt;
      &amp;amp;&amp;amp; target(ba);&amp;lt;br/&amp;gt;&lt;br /&gt;
    after(BankAccount ba) returning: stateChange(ba) {&lt;br /&gt;
      // persist ba.getBalance() value to database&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We define the pointcut stateChange as calls to the mutator functions of the BankAccount class, which are the deposit and withdraw functions in the PersistenceAspect. The persistence action should be performed after the above mentioned pointcut. So we define an advice returning, after the pointcut stateChange. So this code will execute everytime after the withdraw and deposit functions and the balance will be persisted.&lt;br /&gt;
&lt;br /&gt;
Thus the BankAccount class contains code only related to the BankAccount. It is completely free of any cross-cutting concerns such as persistence. Other concerns such as user authentication might be implemented similarly.&lt;br /&gt;
&lt;br /&gt;
==AOP in Ruby==&lt;br /&gt;
Ruby does not have native AOP support. But Ruby's metaprogramming features cover many of the aspect requirements. 'method_missing' and reflection in Ruby are classis examples of that. 'method_missing' allows the code to inspect the method call at runtime and redirect to some default method or define a new method if the called method does not exists. Similarly Ruby provides naive support to implement pointcuts and wrapper methods. They are discussed below.&lt;br /&gt;
&lt;br /&gt;
===Pointcut equivalent in Ruby===&lt;br /&gt;
There are no pointcuts in Ruby. But they can be simulated by the inspect method that Ruby provides. &lt;br /&gt;
&lt;br /&gt;
  # List all the classes&lt;br /&gt;
  ObjectSpace.each_object(Class) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the modules&lt;br /&gt;
  ObjectSpace.each_object(Module) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the instances of class String&lt;br /&gt;
  ObjectSpace.each_object(String) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Adding advice in Ruby===&lt;br /&gt;
In Ruby, alias can be used to wrap methods with advice.&lt;br /&gt;
&lt;br /&gt;
  foo.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;bar&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  foo_advice.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
  alias old_bar bar&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;{[&amp;lt; &amp;quot;+ old_bar+ &amp;quot; &amp;gt;]}”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In the above code, the alias will be executed eveytime a call to method 'bar' is made. The output of the above code will be as follows:&lt;br /&gt;
&lt;br /&gt;
  puts Foo.new.bar&lt;br /&gt;
  =&amp;gt; &amp;quot;{[&amp;lt; bar &amp;gt;]}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==AspectR==&lt;br /&gt;
We saw how AOP can be implemented in Ruby without any extra support in the above sections. AspectR is free library which provides API's to the same for Ruby. Essentially it allows you to wrap code around existing methods in your classes. This can be a good thing (and even affect how you design your code) since it separates different concerns into different parts of the code. The code for AspectR can be downloaded from [http://prdownloads.sf.net/aspectr/aspectr-0-3-5.tar.gz here]. AspectR was originally called advice.rb and written by Avi Bryant. Later Robert Feldt changed/tweaked/extended it.&lt;br /&gt;
&lt;br /&gt;
===Main features of AspectR===&lt;br /&gt;
*Joinpoints: Ruby does not have the notion of Jointpoints. But it can be considered as method or constructor calls or even exception handlers. &lt;br /&gt;
*Advices: It contains before (pre) advice, after returning and after throwing (post) advice, which indicates what is to be done before/after the jointpoint is called. &lt;br /&gt;
*Aspects: They are the classes that inherit from Aspect. It supports &amp;quot;abstract&amp;quot; Aspects and overriding between advices. &lt;br /&gt;
*Wildcards: RegExp can be used in pointcut designators, ie. to specify classes and methods to wrap advice's to. &lt;br /&gt;
*Pointcut parameters advices can access: object and method receiving call, arguments to call, return values, and exceptions raised.&lt;br /&gt;
&lt;br /&gt;
===AspectR Syntax===&lt;br /&gt;
Advice methods are passed a variable number of parameters:&lt;br /&gt;
*the first is the name of the method currently being wrapped&lt;br /&gt;
*the second is the object receiving the method call&lt;br /&gt;
*the third is the exit/return status:&lt;br /&gt;
**Array with return value(s) if the method exited normally&lt;br /&gt;
**true if the method exited with an exception&lt;br /&gt;
**nil if the method hasn't yet exited (for preAdvice)&lt;br /&gt;
*the rest are the arguments that were passed to the wrapped method.&lt;br /&gt;
&lt;br /&gt;
Here is the sample syntax:&lt;br /&gt;
  require 'aspectr'&lt;br /&gt;
  include AspectR&lt;br /&gt;
  class MyAspect &amp;lt; Aspect&lt;br /&gt;
    def someAdviceMethod(method, object, exitstatus, *args)&lt;br /&gt;
      ...&lt;br /&gt;
    end&lt;br /&gt;
      ... some other advice methods ...&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  ma = MyAspect.new&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, ... methods to wrap...)&lt;br /&gt;
  or&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, /patternToWrap/)&lt;br /&gt;
  or&lt;br /&gt;
  AspectR.wrap_classes(ma, :preAdvice, :postAdvice,&lt;br /&gt;
                      [Class1, Class2], ...methods to wrap...)&lt;br /&gt;
===Banking account example in AspectR===&lt;br /&gt;
&lt;br /&gt;
  class BankAccount&lt;br /&gt;
    attr_reader :balance&lt;br /&gt;
    def balance=(amount)&lt;br /&gt;
      @balance = amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def initialize&lt;br /&gt;
      @balance = retrieve_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def deposit(amount)&lt;br /&gt;
      @balance += amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def withdraw(amount)&lt;br /&gt;
      if @balance &amp;lt; amount&lt;br /&gt;
        raise InsufficientFundsError&lt;br /&gt;
      end&lt;br /&gt;
      @balance -= amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  class Persist &amp;lt; Aspect&lt;br /&gt;
    def retrieve_balance &lt;br /&gt;
      # Get from database&lt;br /&gt;
         ..&lt;br /&gt;
    end&lt;br /&gt;
    def persist_balance &lt;br /&gt;
      # Save to database&lt;br /&gt;
        ..&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  Persist.new.wrap(BankingAccount, :retrive_balance, :persist_balance, /[withdraw|deposit]/)&lt;br /&gt;
&lt;br /&gt;
In the above code class persists is a subclass of Aspect and has the before(retrive_balance) and after(persist_advice) advice defined in it. Then the last line of the code wraps the this persist class to the BankingAccount's withdraw and deposit methods. It also specifies what pre-advice and what post-advice should be used. The API for wrap class is as follows:&lt;br /&gt;
&lt;br /&gt;
  def wrap(target, pre, post, *args)		&lt;br /&gt;
    get_methods(target, args).each do |method_to_wrap|&lt;br /&gt;
      add_advice(target, PRE, method_to_wrap, pre)&lt;br /&gt;
      add_advice(target, POST, method_to_wrap, post)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Thus as we can see from API, the wrap method directly adds the pre and post advice to the methods (Join-points in AspectJ terminology).&lt;br /&gt;
&lt;br /&gt;
==How AspectR is different from AspectJ==&lt;br /&gt;
*Join points: Join points are not defined separately in AspectR as they are in AspectJ. Instead the method and constructor calls acts as join points. &lt;br /&gt;
*Most of the point cut designator primitives that are present in AspectR differ from AspectR.&lt;br /&gt;
*There is a huge difference in composition of pointcut designators. But you can of course specify several method calls in different classes and objects in a point cut. &lt;br /&gt;
*Around advices are more easy to add in AspectR than AspectJ but mostly before and after advices are used.&lt;br /&gt;
*AspectR has control-flow based crosscutting.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Aspect Oriented Programming is a very powerful concept which enables programmers to separate the cross-cutting concerns. If provides flexibility in decomposing source code into modules. But it lacks popularity. There is no native support for this in popular programming languages like Java. Thus it requires learning new framework. Though there is some native support for APO in Ruby, it does not offer all features of AOP. The AspectR library has these limitations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Aspect-oriented_programming Aspect Oriented Programming - wikipedia]&lt;br /&gt;
# An introduction to Aspect-Oriented Programming with AspectJ by Constantinos Constantinides, McGill University&lt;br /&gt;
# [http://en.wikipedia.org/wiki/AspectJ AspectJ - wikipedia]&lt;br /&gt;
# [http://aspectr.sourceforge.net/ AspectR - Simple aspect-oriented programming in Ruby]&lt;br /&gt;
# [http://www.ruby-doc.org/gems/docs/a/aspectr-0.3.7/AspectR.html AspectR - Ruby documentation]&lt;br /&gt;
# [http://objectmentor.com/resources/articles/AOP_in_Ruby.pdf Aspect-Oriented Programming in Ruby by Dean Wampler]&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65475</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w27 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65475"/>
		<updated>2012-09-21T19:13:56Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Aspect Oriented Programming (AOP): AspectJ and AspectR=&lt;br /&gt;
&lt;br /&gt;
Aspect Oriented Programming (AOP) refers to a new way of designing software. It aims to increase modularity by allowing the separation of cross-cutting concerns. AOP includes programming methods and tools that support the modularization of concerns at the level of the source code. It is not a replacement to popular Object Oriented Programming (OOP), but is complimentary to it.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Aspect-oriented programming entails breaking down program logic into distinct parts, called concerns. Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they &amp;quot;cut across&amp;quot; multiple abstractions in a program. An example to this is logging. In an normal object oriented language program we might need to invoke a logger function from everywhere we this functionality.&lt;br /&gt;
&lt;br /&gt;
==Why do we need AOP? The banking example==&lt;br /&gt;
Typically, an aspect is scattered or tangled as code, making it harder to understand and maintain. It is scattered by virtue of the function (such as logging) being spread over a number of unrelated functions that might use its function, possibly in entirely unrelated systems, different source languages, etc. That means to change logging can require modifying all affected modules. Aspects become tangled not only with the mainline function of the systems in which they are expressed but also with each other. That means changing one concern entails understanding all the tangled concerns or having some means by which the effect of changes can be inferred.&lt;br /&gt;
&lt;br /&gt;
Consider a simple banking application written in Ruby.&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds'&lt;br /&gt;
 class BankAccount&amp;lt;br/&amp;gt;&lt;br /&gt;
   attr_accessor :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = 0.0&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFunds&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
This is simple enough. But is not very useful. What if we need persistence too?&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds_error'&amp;lt;br/&amp;gt;&lt;br /&gt;
 class BankAccount&lt;br /&gt;
   attr_reader :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def balance=(amount)&lt;br /&gt;
     @balance = amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = retrieve_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFundsError&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def retrieve_balance&lt;br /&gt;
     # Get from database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def persist_balance&lt;br /&gt;
     # Save to database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
We can see that now other interests have become tangled with the basic functionality (sometimes called the business logic concern). Now add:&lt;br /&gt;
&lt;br /&gt;
* Transactional behavior&lt;br /&gt;
* Authentication and authorization&lt;br /&gt;
* “Observers”, like a UI&lt;br /&gt;
&lt;br /&gt;
Also this code will be repeated across other domain objects which need the same behavior. Finally, attempt to reuse this code in a new application with a different persistence store, transaction engine, security model etc.&lt;br /&gt;
&lt;br /&gt;
===The Problem===&lt;br /&gt;
&lt;br /&gt;
* Persistence itself is already modular&lt;br /&gt;
** But the code that uses it isn’t!&lt;br /&gt;
** Persistence invocation code is scattered&lt;br /&gt;
** BankAccount code is tangled with code from other concerns&lt;br /&gt;
** BankAccount’s modularity is compromised&lt;br /&gt;
* Also, similar persistence code is added to other components&lt;br /&gt;
** Redundancy:&lt;br /&gt;
*** Hard to change all occurrences consistently.&lt;br /&gt;
*** Hard to replace the Persistence solution.&lt;br /&gt;
*** Breaks Don’t Repeat Yourself! (DRY)&lt;br /&gt;
** Reuse is hard.&lt;br /&gt;
*** The persistence solution may be different in another context.&lt;br /&gt;
&lt;br /&gt;
===The Solution: Aspect Oriented Programming===&lt;br /&gt;
AOP attempts to solve this problem by allowing the programmer to express cross-cutting concerns in stand-alone modules called aspects. Then it provides mechanisms for fine-grained modification of target component behavior to incorporate the aspects’ behaviors. Aspects can contain advice (code joined to specified points in the program) and inter-type declarations (structural members added to other classes). For example, a security module can include advice that performs a security check before accessing a bank account.&lt;br /&gt;
&lt;br /&gt;
For example in the above program, the behavior is modified by adding a 'Persistence aspect' to the program. We specify separately where these aspect should be invoked in the banking account application. Now the banking account remains agnostic and the aspect code is maintained in one place. Also, now it is easy to change to a different solution or to remove the concern altogether.&lt;br /&gt;
&lt;br /&gt;
We will see how this is implemented exactly in various implementation of AOP such as AspectJ and AspectR below.&lt;br /&gt;
&lt;br /&gt;
==AOP Concepts==&lt;br /&gt;
The advice-related component of an aspect-oriented language defines a join point model (JPM). A JPM defines three things:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Joinpoint&amp;lt;/b&amp;gt; - a well defined event in the execution of a program (such as the core functionality provided by the persistence class). eg: A call to a method persist_balance from inside the withdraw method.&lt;br /&gt;
* &amp;lt;b&amp;gt;Pointcut&amp;lt;/b&amp;gt; - A collection of join points. eg: A collection of all mutator methods in BankingAccount.&lt;br /&gt;
* &amp;lt;b&amp;gt;Advice&amp;lt;/b&amp;gt; - A block of code that specifies some behavior to be executed before/after/around a certain joinpoint. &lt;br /&gt;
&lt;br /&gt;
Join-point models can be compared based on the join points exposed, how join points are specified, the operations permitted at the join points, and the structural enhancements that can be expressed.&lt;br /&gt;
&lt;br /&gt;
==AspectJ: AOP in Java==&lt;br /&gt;
AspectJ extend the Java programming language with constructs to support AOP. It is a superset of Java such that each valid Java program is also a valid AspectJ program. Also, it is designed as a general purpose language as opposed to a domain specific language. AspectJ is currently the most notable AOP technology.&lt;br /&gt;
&lt;br /&gt;
AspectJ was created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become the widely used de-facto standard for AOP by emphasizing simplicity and usability for end users. It has included IDE integrations for displaying crosscutting structure since its initial public release in 2001.&lt;br /&gt;
&lt;br /&gt;
===History and contributors===&lt;br /&gt;
Gregor Kiczales started and led the Xerox PARC team that eventually developed AspectJ; he coined the term &amp;quot;crosscutting&amp;quot;. Fourth on the team, Chris Maeda coined the term &amp;quot;aspect-oriented programming.&amp;quot; Jim Hugunin and Erik Hilsdale (Xerox PARC team members 12 and 13) were the original compiler and weaver engineers, Mik Kersten implemented the IDE integration and started the Eclipse AJDT project with Adrian Colyer (current lead of the AspectJ project) and Andrew Clement (current compiler engineer).&lt;br /&gt;
&lt;br /&gt;
The AspectBench Compiler was developed and is being maintained as a joint effort of the Programming Tools Group at the Oxford University Computing Laboratory, the Sable Research Group at McGill University and the Institute for Basic Research in Computer Science (BRICS).&lt;br /&gt;
&lt;br /&gt;
===Lanugage description===&lt;br /&gt;
All valid Java programs are also valid AspectJ programs, but AspectJ also allows programmers to define special constructs called aspects. Aspects can contain several entities unavailable to standard classes. These are:&lt;br /&gt;
&lt;br /&gt;
====Inter-type declarations==== &lt;br /&gt;
These allow a programmer to add methods, fields, or interfaces to existing classes from within the aspect. This example adds an acceptVisitor (see visitor pattern) method to the Point class:&lt;br /&gt;
&lt;br /&gt;
  aspect VisitAspect {&lt;br /&gt;
    void Point.acceptVisitor(Visitor v) {&lt;br /&gt;
      v.visit(this);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
====Pointcuts==== &lt;br /&gt;
Pointcuts allow a programmer to specify join points. All pointcuts are expressions (quantifications) that determine whether a given join point matches. A call joinpoint captures an execution event after it evaluates a method calls' arguments, but before it calls the method itself.&lt;br /&gt;
&lt;br /&gt;
For example, this point-cut specifies all calls to GetBalance method in the class BankingAccount that takes no arguments and returns long&lt;br /&gt;
&lt;br /&gt;
  call(public long BankingAccount.GetBalance())&lt;br /&gt;
&lt;br /&gt;
We may also use logical operators in the definition of pointcuts inorder to combine pointcut expressions.&lt;br /&gt;
&lt;br /&gt;
# || (OR operator) - Matches a joinpoint if either left pointcut expression or right pointcut expression matches&lt;br /&gt;
# &amp;amp;&amp;amp; (AND operator) - Matches a joinpoint if both left pointcut expression and right pointcut expression matches&lt;br /&gt;
# ! (NOT operator) - Matches all joinpoints NOT specified by the pointcut expression&lt;br /&gt;
&lt;br /&gt;
Example of a pointcut which matches both mutator methods of the BankingAccount class is&lt;br /&gt;
&lt;br /&gt;
  pointcut mutators(): call(public long BankingAccount.Withdraw(long)) || call(public long BankingAccount.Deposit(long))&lt;br /&gt;
&lt;br /&gt;
Various types of joinpoints that are possible in AspectJ are:&lt;br /&gt;
# Calls to methods and constructors&lt;br /&gt;
# Execution of methods and constructors&lt;br /&gt;
# Field access&lt;br /&gt;
# Exception handling&lt;br /&gt;
# Class initializaiton&lt;br /&gt;
# Lexical structure&lt;br /&gt;
# Control flow&lt;br /&gt;
# Self-target and argument type&lt;br /&gt;
# Conditional test&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Patterns in pointcuts&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Patterns can also be used to specify events in pointcuts. This would be useful to specify a set of events concisely.&lt;br /&gt;
&lt;br /&gt;
The use of * character is highly overloaded.&lt;br /&gt;
* Using * operator where a type is expected matches any type&lt;br /&gt;
* Using * where an identifier is expected matches any identifier&lt;br /&gt;
* * can also be used in identifier patterns&lt;br /&gt;
** the expression foo* would match any identifier that starts with foo&lt;br /&gt;
** the expression *foo* would match any identifier that has foo in it&lt;br /&gt;
* We can add + to the end of any identifier pattern to indicate that we wish to match a class and all of its subclasses&lt;br /&gt;
&lt;br /&gt;
Examples&lt;br /&gt;
* foo - class foo&lt;br /&gt;
* foo+ - class foo and all of its subclasses&lt;br /&gt;
* foo* - all classes starting with foo&lt;br /&gt;
* foo*+ - all classes starting with foo and all of their subclasses&lt;br /&gt;
* *foo* - all classes with foo in it&lt;br /&gt;
&lt;br /&gt;
====Advice==== &lt;br /&gt;
Advice allows a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. An advice must be define with respect to a pointcut. Given below is an advice defined with respect to the mutators pointcut.&lt;br /&gt;
&lt;br /&gt;
  before() : mutators() {&lt;br /&gt;
    System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This is a special type of mutator called the before mutator which is called before the joinpoint specified by the pointcut is executed.&lt;br /&gt;
&lt;br /&gt;
There are three ways to associate an advice with a pointcut:&lt;br /&gt;
# Before - run just before the pointcut&lt;br /&gt;
# After - run just after the pointcut&lt;br /&gt;
# Around - Runs instead of the pointcut with the provision for the pointcut to resume execution through a method called proceed().&lt;br /&gt;
&lt;br /&gt;
AspectJ also supports limited forms of pointcut-based static checking and aspect reuse (by inheritance). Pointcuts and advices together define composition (weaving) rules.&lt;br /&gt;
&lt;br /&gt;
====Aspect====&lt;br /&gt;
Much like a class, an Aspect is a unit of modularity. It is defined in terms of pointcuts, advices and ordinary java fields and methods. Pointcuts say which event to match and advice body says what to execute when it matches.&lt;br /&gt;
&lt;br /&gt;
  public aspect Tracer {&lt;br /&gt;
    pointcut mutators(): call(public long BankingAccount.Withdraw(long)) ||&lt;br /&gt;
                           call(public long BankingAccount.Deposit(long));&amp;lt;br/&amp;gt;&lt;br /&gt;
    before() : mutators() {&lt;br /&gt;
      System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Banking example in Java using AOP===&lt;br /&gt;
Now let us look at how persistence may be added to the BankingAccount example using AspectJ&lt;br /&gt;
&lt;br /&gt;
  class BankAccount {&amp;lt;br/&amp;gt;&lt;br /&gt;
    float balance;&amp;lt;br/&amp;gt;&lt;br /&gt;
    public BankAccount(float balance) {&lt;br /&gt;
      this.balance = balance;&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void deposit(float amount) {&lt;br /&gt;
      balance += amount&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void withdraw(float amount) {&lt;br /&gt;
      if (balance &amp;lt; amount) {&lt;br /&gt;
        throw new InsufficientFundsException();&lt;br /&gt;
      }&lt;br /&gt;
      balance -= amount;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  aspect PersistenceAspect {&amp;lt;br/&amp;gt;&lt;br /&gt;
    pointcut stateChange(BankAccount ba):&lt;br /&gt;
      (call(void BankAccount.deposit(*)) ||&lt;br /&gt;
      call(void BankAccount.withdraw(*)))&lt;br /&gt;
      &amp;amp;&amp;amp; target(ba);&amp;lt;br/&amp;gt;&lt;br /&gt;
    after(BankAccount ba) returning: stateChange(ba) {&lt;br /&gt;
      // persist ba.getBalance() value to database&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We define the pointcut stateChange as calls to the mutator functions of the BankAccount class, which are the deposit and withdraw functions in the PersistenceAspect. The persistence action should be performed after the above mentioned pointcut. So we define an advice returning, after the pointcut stateChange. So this code will execute everytime after the withdraw and deposit functions and the balance will be persisted.&lt;br /&gt;
&lt;br /&gt;
Thus the BankAccount class contains code only related to the BankAccount. It is completely free of any cross-cutting concerns such as persistence. Other concerns such as user authentication might be implemented similarly.&lt;br /&gt;
&lt;br /&gt;
==AOP in Ruby==&lt;br /&gt;
Ruby does not have native AOP support. But Ruby's metaprogramming features cover many of the aspect requirements. 'method_missing' and reflection in Ruby are classis examples of that. 'method_missing' allows the code to inspect the method call at runtime and redirect to some default method or define a new method if the called method does not exists. Similarly Ruby provides naive support to implement pointcuts and wrapper methods. They are discussed below.&lt;br /&gt;
&lt;br /&gt;
===Pointcut equivalent in Ruby===&lt;br /&gt;
There are no pointcuts in Ruby. But they can be simulated by the inspect method that Ruby provides. &lt;br /&gt;
&lt;br /&gt;
  # List all the classes&lt;br /&gt;
  ObjectSpace.each_object(Class) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the modules&lt;br /&gt;
  ObjectSpace.each_object(Module) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the instances of class String&lt;br /&gt;
  ObjectSpace.each_object(String) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Adding advice in Ruby===&lt;br /&gt;
In Ruby, alias can be used to wrap methods with advice.&lt;br /&gt;
&lt;br /&gt;
  foo.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;bar&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  foo_advice.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
  alias old_bar bar&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;{[&amp;lt; &amp;quot;+ old_bar+ &amp;quot; &amp;gt;]}”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In the above code, the alias will be executed eveytime a call to method 'bar' is made. The output of the above code will be as follows:&lt;br /&gt;
&lt;br /&gt;
  puts Foo.new.bar&lt;br /&gt;
  =&amp;gt; &amp;quot;{[&amp;lt; bar &amp;gt;]}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==AspectR==&lt;br /&gt;
We saw how AOP can be implemented in Ruby without any extra support in the above sections. AspectR is free library which provides API's to the same for Ruby. Essentially it allows you to wrap code around existing methods in your classes. This can be a good thing (and even affect how you design your code) since it separates different concerns into different parts of the code. The code for AspectR can be downloaded from [http://prdownloads.sf.net/aspectr/aspectr-0-3-5.tar.gz here]. AspectR was originally called advice.rb and written by Avi Bryant. Later Robert Feldt changed/tweaked/extended it.&lt;br /&gt;
&lt;br /&gt;
===Main features of AspectR===&lt;br /&gt;
*Joinpoints: Ruby does not have the notion of Jointpoints. But it can be considered as method or constructor calls or even exception handlers. &lt;br /&gt;
*Advices: It contains before (pre) advice, after returning and after throwing (post) advice, which indicates what is to be done before/after the jointpoint is called. &lt;br /&gt;
*Aspects: They are the classes that inherit from Aspect. It supports &amp;quot;abstract&amp;quot; Aspects and overriding between advices. &lt;br /&gt;
*Wildcards: RegExp can be used in pointcut designators, ie. to specify classes and methods to wrap advice's to. &lt;br /&gt;
*Pointcut parameters advices can access: object and method receiving call, arguments to call, return values, and exceptions raised.&lt;br /&gt;
&lt;br /&gt;
===AspectR Syntax===&lt;br /&gt;
Advice methods are passed a variable number of parameters:&lt;br /&gt;
*the first is the name of the method currently being wrapped&lt;br /&gt;
*the second is the object receiving the method call&lt;br /&gt;
*the third is the exit/return status:&lt;br /&gt;
**Array with return value(s) if the method exited normally&lt;br /&gt;
**true if the method exited with an exception&lt;br /&gt;
**nil if the method hasn't yet exited (for preAdvice)&lt;br /&gt;
*the rest are the arguments that were passed to the wrapped method.&lt;br /&gt;
&lt;br /&gt;
Here is the sample syntax:&lt;br /&gt;
  require 'aspectr'&lt;br /&gt;
  include AspectR&lt;br /&gt;
  class MyAspect &amp;lt; Aspect&lt;br /&gt;
    def someAdviceMethod(method, object, exitstatus, *args)&lt;br /&gt;
      ...&lt;br /&gt;
    end&lt;br /&gt;
      ... some other advice methods ...&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  ma = MyAspect.new&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, ... methods to wrap...)&lt;br /&gt;
  or&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, /patternToWrap/)&lt;br /&gt;
  or&lt;br /&gt;
  AspectR.wrap_classes(ma, :preAdvice, :postAdvice,&lt;br /&gt;
                      [Class1, Class2], ...methods to wrap...)&lt;br /&gt;
===Banking account example in AspectR===&lt;br /&gt;
&lt;br /&gt;
  class BankAccount&lt;br /&gt;
    attr_reader :balance&lt;br /&gt;
    def balance=(amount)&lt;br /&gt;
      @balance = amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def initialize&lt;br /&gt;
      @balance = retrieve_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def deposit(amount)&lt;br /&gt;
      @balance += amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def withdraw(amount)&lt;br /&gt;
      if @balance &amp;lt; amount&lt;br /&gt;
        raise InsufficientFundsError&lt;br /&gt;
      end&lt;br /&gt;
      @balance -= amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  class Persist &amp;lt; Aspect&lt;br /&gt;
    def retrieve_balance &lt;br /&gt;
      # Get from database&lt;br /&gt;
         ..&lt;br /&gt;
    end&lt;br /&gt;
    def persist_balance &lt;br /&gt;
      # Save to database&lt;br /&gt;
        ..&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  Persist.new.wrap(BankingAccount, :retrive_balance, :persist_balance, /[withdraw|deposit]/)&lt;br /&gt;
&lt;br /&gt;
In the above code class persists is a subclass of Aspect and has the before(retrive_balance) and after(persist_advice) advice defined in it. Then the last line of the code wraps the this persist class to the BankingAccount's withdraw and deposit methods. It also specifies what pre-advice and what post-advice should be used. The API for wrap class is as follows:&lt;br /&gt;
&lt;br /&gt;
  def wrap(target, pre, post, *args)		&lt;br /&gt;
    get_methods(target, args).each do |method_to_wrap|&lt;br /&gt;
      add_advice(target, PRE, method_to_wrap, pre)&lt;br /&gt;
      add_advice(target, POST, method_to_wrap, post)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Thus as we can see from API, the wrap method directly adds the pre and post advice to the methods (Join-points in AspectJ terminology).&lt;br /&gt;
&lt;br /&gt;
==How AspectR is different from AspectJ==&lt;br /&gt;
*Join points: Join points are not defined separately in AspectR as they are in AspectJ. Instead the method and constructor calls acts as join points. &lt;br /&gt;
*Most of the point cut designator primitives that are present in AspectR differ from AspectR.&lt;br /&gt;
*There is a huge difference in composition of pointcut designators. But you can of course specify several method calls in different classes and objects in a point cut. &lt;br /&gt;
*Around advices are more easy to add in AspectR than AspectJ but mostly before and after advices are used.&lt;br /&gt;
*AspectR has control-flow based crosscutting.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Aspect Oriented Programming is a very powerful concept which enables programmers to separate the cross-cutting concerns. If provides flexibility in decomposing source code into modules. But it lacks popularity. There is no native support for this in popular programming languages like Java. Thus it requires learning new framework. Though there is some native support for APO in Ruby, it does not offer all features of AOP. The AspectR library has these limitations.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Aspect-oriented_programming Aspect Oriented Programming - wikipedia]&lt;br /&gt;
# An introduction to Aspect-Oriented Programming with AspectJ by Constantinos Constantinides, McGill University&lt;br /&gt;
# [http://en.wikipedia.org/wiki/AspectJ AspectJ - wikipedia]&lt;br /&gt;
# [http://aspectr.sourceforge.net/ AspectR - Simple aspect-oriented programming in Ruby]&lt;br /&gt;
# [http://www.ruby-doc.org/gems/docs/a/aspectr-0.3.7/AspectR.html AspectR - Ruby documentation]&lt;br /&gt;
# Aspect-Oriented Programming in Ruby by Dean Wampler&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65474</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w27 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65474"/>
		<updated>2012-09-21T18:54:15Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Aspect Oriented Programming (AOP): AspectJ and AspectR=&lt;br /&gt;
&lt;br /&gt;
Aspect Oriented Programming (AOP) refers to a new way of designing software. It aims to increase modularity by allowing the separation of cross-cutting concerns. AOP includes programming methods and tools that support the modularization of concerns at the level of the source code. It is not a replacement to popular Object Oriented Programming (OOP), but is complimentary to it.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Aspect-oriented programming entails breaking down program logic into distinct parts, called concerns. Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they &amp;quot;cut across&amp;quot; multiple abstractions in a program. An example to this is logging. In an normal object oriented language program we might need to invoke a logger function from everywhere we this functionality.&lt;br /&gt;
&lt;br /&gt;
==Why do we need AOP? The banking example==&lt;br /&gt;
Typically, an aspect is scattered or tangled as code, making it harder to understand and maintain. It is scattered by virtue of the function (such as logging) being spread over a number of unrelated functions that might use its function, possibly in entirely unrelated systems, different source languages, etc. That means to change logging can require modifying all affected modules. Aspects become tangled not only with the mainline function of the systems in which they are expressed but also with each other. That means changing one concern entails understanding all the tangled concerns or having some means by which the effect of changes can be inferred.&lt;br /&gt;
&lt;br /&gt;
Consider a simple banking application written in Ruby.&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds'&lt;br /&gt;
 class BankAccount&amp;lt;br/&amp;gt;&lt;br /&gt;
   attr_accessor :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = 0.0&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFunds&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
This is simple enough. But is not very useful. What if we need persistence too?&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds_error'&amp;lt;br/&amp;gt;&lt;br /&gt;
 class BankAccount&lt;br /&gt;
   attr_reader :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def balance=(amount)&lt;br /&gt;
     @balance = amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = retrieve_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFundsError&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def retrieve_balance&lt;br /&gt;
     # Get from database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def persist_balance&lt;br /&gt;
     # Save to database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
We can see that now other interests have become tangled with the basic functionality (sometimes called the business logic concern). Now add:&lt;br /&gt;
&lt;br /&gt;
* Transactional behavior&lt;br /&gt;
* Authentication and authorization&lt;br /&gt;
* “Observers”, like a UI&lt;br /&gt;
&lt;br /&gt;
Also this code will be repeated across other domain objects which need the same behavior. Finally, attempt to reuse this code in a new application with a different persistence store, transaction engine, security model etc.&lt;br /&gt;
&lt;br /&gt;
===The Problem===&lt;br /&gt;
&lt;br /&gt;
* Persistence itself is already modular&lt;br /&gt;
** But the code that uses it isn’t!&lt;br /&gt;
** Persistence invocation code is scattered&lt;br /&gt;
** BankAccount code is tangled with code from other concerns&lt;br /&gt;
** BankAccount’s modularity is compromised&lt;br /&gt;
* Also, similar persistence code is added to other components&lt;br /&gt;
** Redundancy:&lt;br /&gt;
*** Hard to change all occurrences consistently.&lt;br /&gt;
*** Hard to replace the Persistence solution.&lt;br /&gt;
*** Breaks Don’t Repeat Yourself! (DRY)&lt;br /&gt;
** Reuse is hard.&lt;br /&gt;
*** The persistence solution may be different in another context.&lt;br /&gt;
&lt;br /&gt;
===The Solution: Aspect Oriented Programming===&lt;br /&gt;
AOP attempts to solve this problem by allowing the programmer to express cross-cutting concerns in stand-alone modules called aspects. Then it provides mechanisms for fine-grained modification of target component behavior to incorporate the aspects’ behaviors. Aspects can contain advice (code joined to specified points in the program) and inter-type declarations (structural members added to other classes). For example, a security module can include advice that performs a security check before accessing a bank account.&lt;br /&gt;
&lt;br /&gt;
For example in the above program, the behavior is modified by adding a 'Persistence aspect' to the program. We specify separately where these aspect should be invoked in the banking account application. Now the banking account remains agnostic and the aspect code is maintained in one place. Also, now it is easy to change to a different solution or to remove the concern altogether.&lt;br /&gt;
&lt;br /&gt;
We will see how this is implemented exactly in various implementation of AOP such as AspectJ and AspectR below.&lt;br /&gt;
&lt;br /&gt;
==AOP Concepts==&lt;br /&gt;
The advice-related component of an aspect-oriented language defines a join point model (JPM). A JPM defines three things:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Joinpoint&amp;lt;/b&amp;gt; - a well defined event in the execution of a program (such as the core functionality provided by the persistence class). eg: A call to a method persist_balance from inside the withdraw method.&lt;br /&gt;
* &amp;lt;b&amp;gt;Pointcut&amp;lt;/b&amp;gt; - A collection of join points. eg: A collection of all mutator methods in BankingAccount.&lt;br /&gt;
* &amp;lt;b&amp;gt;Advice&amp;lt;/b&amp;gt; - A block of code that specifies some behavior to be executed before/after/around a certain joinpoint. &lt;br /&gt;
&lt;br /&gt;
Join-point models can be compared based on the join points exposed, how join points are specified, the operations permitted at the join points, and the structural enhancements that can be expressed.&lt;br /&gt;
&lt;br /&gt;
==AspectJ: AOP in Java==&lt;br /&gt;
AspectJ extend the Java programming language with constructs to support AOP. It is a superset of Java such that each valid Java program is also a valid AspectJ program. Also, it is designed as a general purpose language as opposed to a domain specific language. AspectJ is currently the most notable AOP technology.&lt;br /&gt;
&lt;br /&gt;
AspectJ was created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become the widely used de-facto standard for AOP by emphasizing simplicity and usability for end users. It has included IDE integrations for displaying crosscutting structure since its initial public release in 2001.&lt;br /&gt;
&lt;br /&gt;
===History and contributors===&lt;br /&gt;
Gregor Kiczales started and led the Xerox PARC team that eventually developed AspectJ; he coined the term &amp;quot;crosscutting&amp;quot;. Fourth on the team, Chris Maeda coined the term &amp;quot;aspect-oriented programming.&amp;quot; Jim Hugunin and Erik Hilsdale (Xerox PARC team members 12 and 13) were the original compiler and weaver engineers, Mik Kersten implemented the IDE integration and started the Eclipse AJDT project with Adrian Colyer (current lead of the AspectJ project) and Andrew Clement (current compiler engineer).&lt;br /&gt;
&lt;br /&gt;
The AspectBench Compiler was developed and is being maintained as a joint effort of the Programming Tools Group at the Oxford University Computing Laboratory, the Sable Research Group at McGill University and the Institute for Basic Research in Computer Science (BRICS).&lt;br /&gt;
&lt;br /&gt;
===Lanugage description===&lt;br /&gt;
All valid Java programs are also valid AspectJ programs, but AspectJ also allows programmers to define special constructs called aspects. Aspects can contain several entities unavailable to standard classes. These are:&lt;br /&gt;
&lt;br /&gt;
====Inter-type declarations==== &lt;br /&gt;
These allow a programmer to add methods, fields, or interfaces to existing classes from within the aspect. This example adds an acceptVisitor (see visitor pattern) method to the Point class:&lt;br /&gt;
&lt;br /&gt;
  aspect VisitAspect {&lt;br /&gt;
    void Point.acceptVisitor(Visitor v) {&lt;br /&gt;
      v.visit(this);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
====Pointcuts==== &lt;br /&gt;
Pointcuts allow a programmer to specify join points. All pointcuts are expressions (quantifications) that determine whether a given join point matches. A call joinpoint captures an execution event after it evaluates a method calls' arguments, but before it calls the method itself.&lt;br /&gt;
&lt;br /&gt;
For example, this point-cut specifies all calls to GetBalance method in the class BankingAccount that takes no arguments and returns long&lt;br /&gt;
&lt;br /&gt;
  call(public long BankingAccount.GetBalance())&lt;br /&gt;
&lt;br /&gt;
We may also use logical operators in the definition of pointcuts inorder to combine pointcut expressions.&lt;br /&gt;
&lt;br /&gt;
# || (OR operator) - Matches a joinpoint if either left pointcut expression or right pointcut expression matches&lt;br /&gt;
# &amp;amp;&amp;amp; (AND operator) - Matches a joinpoint if both left pointcut expression and right pointcut expression matches&lt;br /&gt;
# ! (NOT operator) - Matches all joinpoints NOT specified by the pointcut expression&lt;br /&gt;
&lt;br /&gt;
Example of a pointcut which matches both mutator methods of the BankingAccount class is&lt;br /&gt;
&lt;br /&gt;
  pointcut mutators(): call(public long BankingAccount.Withdraw(long)) || call(public long BankingAccount.Deposit(long))&lt;br /&gt;
&lt;br /&gt;
Various types of joinpoints that are possible in AspectJ are:&lt;br /&gt;
# Calls to methods and constructors&lt;br /&gt;
# Execution of methods and constructors&lt;br /&gt;
# Field access&lt;br /&gt;
# Exception handling&lt;br /&gt;
# Class initializaiton&lt;br /&gt;
# Lexical structure&lt;br /&gt;
# Control flow&lt;br /&gt;
# Self-target and argument type&lt;br /&gt;
# Conditional test&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Patterns in pointcuts&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Patterns can also be used to specify events in pointcuts. This would be useful to specify a set of events concisely.&lt;br /&gt;
&lt;br /&gt;
The use of * character is highly overloaded.&lt;br /&gt;
* Using * operator where a type is expected matches any type&lt;br /&gt;
* Using * where an identifier is expected matches any identifier&lt;br /&gt;
* * can also be used in identifier patterns&lt;br /&gt;
** the expression foo* would match any identifier that starts with foo&lt;br /&gt;
** the expression *foo* would match any identifier that has foo in it&lt;br /&gt;
* We can add + to the end of any identifier pattern to indicate that we wish to match a class and all of its subclasses&lt;br /&gt;
&lt;br /&gt;
Examples&lt;br /&gt;
* foo - class foo&lt;br /&gt;
* foo+ - class foo and all of its subclasses&lt;br /&gt;
* foo* - all classes starting with foo&lt;br /&gt;
* foo*+ - all classes starting with foo and all of their subclasses&lt;br /&gt;
* *foo* - all classes with foo in it&lt;br /&gt;
&lt;br /&gt;
====Advice==== &lt;br /&gt;
Advice allows a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. An advice must be define with respect to a pointcut. Given below is an advice defined with respect to the mutators pointcut.&lt;br /&gt;
&lt;br /&gt;
  before() : mutators() {&lt;br /&gt;
    System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This is a special type of mutator called the before mutator which is called before the joinpoint specified by the pointcut is executed.&lt;br /&gt;
&lt;br /&gt;
There are three ways to associate an advice with a pointcut:&lt;br /&gt;
# Before - run just before the pointcut&lt;br /&gt;
# After - run just after the pointcut&lt;br /&gt;
# Around - Runs instead of the pointcut with the provision for the pointcut to resume execution through a method called proceed().&lt;br /&gt;
&lt;br /&gt;
AspectJ also supports limited forms of pointcut-based static checking and aspect reuse (by inheritance). Pointcuts and advices together define composition (weaving) rules.&lt;br /&gt;
&lt;br /&gt;
====Aspect====&lt;br /&gt;
Much like a class, an Aspect is a unit of modularity. It is defined in terms of pointcuts, advices and ordinary java fields and methods. Pointcuts say which event to match and advice body says what to execute when it matches.&lt;br /&gt;
&lt;br /&gt;
  public aspect Tracer {&lt;br /&gt;
    pointcut mutators(): call(public long BankingAccount.Withdraw(long)) ||&lt;br /&gt;
                           call(public long BankingAccount.Deposit(long));&amp;lt;br/&amp;gt;&lt;br /&gt;
    before() : mutators() {&lt;br /&gt;
      System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Banking example in Java using AOP===&lt;br /&gt;
Now let us look at how persistence may be added to the BankingAccount example using AspectJ&lt;br /&gt;
&lt;br /&gt;
  class BankAccount {&amp;lt;br/&amp;gt;&lt;br /&gt;
    float balance;&amp;lt;br/&amp;gt;&lt;br /&gt;
    public BankAccount(float balance) {&lt;br /&gt;
      this.balance = balance;&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void deposit(float amount) {&lt;br /&gt;
      balance += amount&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void withdraw(float amount) {&lt;br /&gt;
      if (balance &amp;lt; amount) {&lt;br /&gt;
        throw new InsufficientFundsException();&lt;br /&gt;
      }&lt;br /&gt;
      balance -= amount;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  aspect PersistenceAspect {&amp;lt;br/&amp;gt;&lt;br /&gt;
    pointcut stateChange(BankAccount ba):&lt;br /&gt;
      (call(void BankAccount.deposit(*)) ||&lt;br /&gt;
      call(void BankAccount.withdraw(*)))&lt;br /&gt;
      &amp;amp;&amp;amp; target(ba);&amp;lt;br/&amp;gt;&lt;br /&gt;
    after(BankAccount ba) returning: stateChange(ba) {&lt;br /&gt;
      // persist ba.getBalance() value to database&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We define the pointcut stateChange as calls to the mutator functions of the BankAccount class, which are the deposit and withdraw functions in the PersistenceAspect. The persistence action should be performed after the above mentioned pointcut. So we define an advice returning, after the pointcut stateChange. So this code will execute everytime after the withdraw and deposit functions and the balance will be persisted.&lt;br /&gt;
&lt;br /&gt;
Thus the BankAccount class contains code only related to the BankAccount. It is completely free of any cross-cutting concerns such as persistence. Other concerns such as user authentication might be implemented similarly.&lt;br /&gt;
&lt;br /&gt;
==AOP in Ruby==&lt;br /&gt;
Ruby does not have native AOP support. But Ruby's metaprogramming features cover many of the aspect requirements. 'method_missing' and reflection in Ruby are classis examples of that. 'method_missing' allows the code to inspect the method call at runtime and redirect to some default method or define a new method if the called method does not exists. Similarly Ruby provides naive support to implement pointcuts and wrapper methods. They are discussed below.&lt;br /&gt;
&lt;br /&gt;
===Pointcut equivalent in Ruby===&lt;br /&gt;
There are no pointcuts in Ruby. But they can be simulated by the inspect method that Ruby provides. &lt;br /&gt;
&lt;br /&gt;
  # List all the classes&lt;br /&gt;
  ObjectSpace.each_object(Class) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the modules&lt;br /&gt;
  ObjectSpace.each_object(Module) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the instances of class String&lt;br /&gt;
  ObjectSpace.each_object(String) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Adding advice in Ruby===&lt;br /&gt;
In Ruby, alias can be used to wrap methods with advice.&lt;br /&gt;
&lt;br /&gt;
  foo.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;bar&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  foo_advice.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
  alias old_bar bar&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;{[&amp;lt; &amp;quot;+ old_bar+ &amp;quot; &amp;gt;]}”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In the above code, the alias will be executed eveytime a call to method 'bar' is made. The output of the above code will be as follows:&lt;br /&gt;
&lt;br /&gt;
  puts Foo.new.bar&lt;br /&gt;
  =&amp;gt; &amp;quot;{[&amp;lt; bar &amp;gt;]}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==AspectR==&lt;br /&gt;
We saw how AOP can be implemented in Ruby without any extra support in the above sections. AspectR is free library which provides API's to the same for Ruby. Essentially it allows you to wrap code around existing methods in your classes. This can be a good thing (and even affect how you design your code) since it separates different concerns into different parts of the code. The code for AspectR can be downloaded from [http://prdownloads.sf.net/aspectr/aspectr-0-3-5.tar.gz here]. AspectR was originally called advice.rb and written by Avi Bryant. Later Robert Feldt changed/tweaked/extended it.&lt;br /&gt;
&lt;br /&gt;
===Main features of AspectR===&lt;br /&gt;
*Joinpoints: Ruby does not have the notion of Jointpoints. But it can be considered as method or constructor calls or even exception handlers. &lt;br /&gt;
*Advices: It contains before (pre) advice, after returning and after throwing (post) advice, which indicates what is to be done before/after the jointpoint is called. &lt;br /&gt;
*Aspects: They are the classes that inherit from Aspect. It supports &amp;quot;abstract&amp;quot; Aspects and overriding between advices. &lt;br /&gt;
*Wildcards: RegExp can be used in pointcut designators, ie. to specify classes and methods to wrap advice's to. &lt;br /&gt;
*Pointcut parameters advices can access: object and method receiving call, arguments to call, return values, and exceptions raised.&lt;br /&gt;
&lt;br /&gt;
===AspectR Syntax===&lt;br /&gt;
Advice methods are passed a variable number of parameters:&lt;br /&gt;
*the first is the name of the method currently being wrapped&lt;br /&gt;
*the second is the object receiving the method call&lt;br /&gt;
*the third is the exit/return status:&lt;br /&gt;
**Array with return value(s) if the method exited normally&lt;br /&gt;
**true if the method exited with an exception&lt;br /&gt;
**nil if the method hasn't yet exited (for preAdvice)&lt;br /&gt;
*the rest are the arguments that were passed to the wrapped method.&lt;br /&gt;
&lt;br /&gt;
Here is the sample syntax:&lt;br /&gt;
  require 'aspectr'&lt;br /&gt;
  include AspectR&lt;br /&gt;
  class MyAspect &amp;lt; Aspect&lt;br /&gt;
    def someAdviceMethod(method, object, exitstatus, *args)&lt;br /&gt;
      ...&lt;br /&gt;
    end&lt;br /&gt;
      ... some other advice methods ...&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  ma = MyAspect.new&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, ... methods to wrap...)&lt;br /&gt;
  or&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, /patternToWrap/)&lt;br /&gt;
  or&lt;br /&gt;
  AspectR.wrap_classes(ma, :preAdvice, :postAdvice,&lt;br /&gt;
                      [Class1, Class2], ...methods to wrap...)&lt;br /&gt;
===Banking account example in AspectR===&lt;br /&gt;
&lt;br /&gt;
  class BankAccount&lt;br /&gt;
    attr_reader :balance&lt;br /&gt;
    def balance=(amount)&lt;br /&gt;
      @balance = amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def initialize&lt;br /&gt;
      @balance = retrieve_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def deposit(amount)&lt;br /&gt;
      @balance += amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def withdraw(amount)&lt;br /&gt;
      if @balance &amp;lt; amount&lt;br /&gt;
        raise InsufficientFundsError&lt;br /&gt;
      end&lt;br /&gt;
      @balance -= amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  class Persist &amp;lt; Aspect&lt;br /&gt;
    def retrieve_balance &lt;br /&gt;
      # Get from database&lt;br /&gt;
         ..&lt;br /&gt;
    end&lt;br /&gt;
    def persist_balance &lt;br /&gt;
      # Save to database&lt;br /&gt;
        ..&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  Persist.new.wrap(BankingAccount, :retrive_balance, :persist_balance, /[withdraw|deposit]/)&lt;br /&gt;
&lt;br /&gt;
In the above code class persists is a subclass of Aspect and has the before(retrive_balance) and after(persist_advice) advice defined in it. Then the last line of the code wraps the this persist class to the BankingAccount's withdraw and deposit methods. It also specifies what pre-advice and what post-advice should be used. The API for wrap class is as follows:&lt;br /&gt;
&lt;br /&gt;
  def wrap(target, pre, post, *args)		&lt;br /&gt;
    get_methods(target, args).each do |method_to_wrap|&lt;br /&gt;
      add_advice(target, PRE, method_to_wrap, pre)&lt;br /&gt;
      add_advice(target, POST, method_to_wrap, post)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Thus as we can see from API, the wrap method directly adds the pre and post advice to the methods (Join-points in AspectJ terminology).&lt;br /&gt;
&lt;br /&gt;
==How AspectR is different from AspectJ==&lt;br /&gt;
*Join points: Join points are not defined separately in AspectR as they are in AspectJ. Instead the method and constructor calls acts as join points. &lt;br /&gt;
*Most of the point cut designator primitives that are present in AspectR differ from AspectR.&lt;br /&gt;
*There is a huge difference in composition of pointcut designators. But you can of course specify several method calls in different classes and objects in a point cut. &lt;br /&gt;
*Around advices are more easy to add in AspectR than AspectJ but mostly before and after advices are used.&lt;br /&gt;
*AspectR has control-flow based crosscutting.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Aspect-oriented_programming Aspect Oriented Programming - wikipedia]&lt;br /&gt;
# An introduction to Aspect-Oriented Programming with AspectJ by Constantinos Constantinides, McGill University&lt;br /&gt;
# [http://en.wikipedia.org/wiki/AspectJ AspectJ - wikipedia]&lt;br /&gt;
# [http://aspectr.sourceforge.net/ AspectR - Simple aspect-oriented programming in Ruby]&lt;br /&gt;
# [http://www.ruby-doc.org/gems/docs/a/aspectr-0.3.7/AspectR.html AspectR - Ruby documentation]&lt;br /&gt;
# Aspect-Oriented Programming in Ruby by Dean Wampler&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65168</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w27 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65168"/>
		<updated>2012-09-17T19:04:27Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Aspect Oriented Programming (AOP): AspectJ and AspectR=&lt;br /&gt;
&lt;br /&gt;
Aspect Oriented Programming (AOP) refers to a new way of designing software. It aims to increase modularity by allowing the separation of cross-cutting concerns. AOP includes programming methods and tools that support the modularization of concerns at the level of the source code. It is not a replacement to popular Object Oriented Programming (OOP), but is complimentary to it.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Aspect-oriented programming entails breaking down program logic into distinct parts, called concerns. Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they &amp;quot;cut across&amp;quot; multiple abstractions in a program. An example to this is logging. In an normal object oriented language program we might need to invoke a logger function from everywhere we this functionality.&lt;br /&gt;
&lt;br /&gt;
==Why do we need AOP? The banking example==&lt;br /&gt;
Typically, an aspect is scattered or tangled as code, making it harder to understand and maintain. It is scattered by virtue of the function (such as logging) being spread over a number of unrelated functions that might use its function, possibly in entirely unrelated systems, different source languages, etc. That means to change logging can require modifying all affected modules. Aspects become tangled not only with the mainline function of the systems in which they are expressed but also with each other. That means changing one concern entails understanding all the tangled concerns or having some means by which the effect of changes can be inferred.&lt;br /&gt;
&lt;br /&gt;
Consider a simple banking application written in Ruby.&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds'&lt;br /&gt;
 class BankAccount&amp;lt;br/&amp;gt;&lt;br /&gt;
   attr_accessor :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = 0.0&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFunds&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
This is simple enough. But is not very useful. What if we need persistence too?&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds_error'&amp;lt;br/&amp;gt;&lt;br /&gt;
 class BankAccount&lt;br /&gt;
   attr_reader :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def balance=(amount)&lt;br /&gt;
     @balance = amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = retrieve_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFundsError&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def retrieve_balance&lt;br /&gt;
     # Get from database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def persist_balance&lt;br /&gt;
     # Save to database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
We can see that now other interests have become tangled with the basic functionality (sometimes called the business logic concern). Now add:&lt;br /&gt;
&lt;br /&gt;
* Transactional behavior&lt;br /&gt;
* Authentication and authorization&lt;br /&gt;
* “Observers”, like a UI&lt;br /&gt;
&lt;br /&gt;
Also this code will be repeated across other domain objects which need the same behavior. Finally, attempt to reuse this code in a new application with a different persistence store, transaction engine, security model etc.&lt;br /&gt;
&lt;br /&gt;
===The Problem===&lt;br /&gt;
&lt;br /&gt;
* Persistence itself is already modular&lt;br /&gt;
** But the code that uses it isn’t!&lt;br /&gt;
** Persistence invocation code is scattered&lt;br /&gt;
** BankAccount code is tangled with code from other concerns&lt;br /&gt;
** BankAccount’s modularity is compromised&lt;br /&gt;
* Also, similar persistence code is added to other components&lt;br /&gt;
** Redundancy:&lt;br /&gt;
*** Hard to change all occurrences consistently.&lt;br /&gt;
*** Hard to replace the Persistence solution.&lt;br /&gt;
*** Breaks Don’t Repeat Yourself! (DRY)&lt;br /&gt;
** Reuse is hard.&lt;br /&gt;
*** The persistence solution may be different in another context.&lt;br /&gt;
&lt;br /&gt;
===The Solution: Aspect Oriented Programming===&lt;br /&gt;
AOP attempts to solve this problem by allowing the programmer to express cross-cutting concerns in stand-alone modules called aspects. Then it provides mechanisms for fine-grained modification of target component behavior to incorporate the aspects’ behaviors. Aspects can contain advice (code joined to specified points in the program) and inter-type declarations (structural members added to other classes). For example, a security module can include advice that performs a security check before accessing a bank account.&lt;br /&gt;
&lt;br /&gt;
For example in the above program, the behavior is modified by adding a 'Persistence aspect' to the program. We specify separately where these aspect should be invoked in the banking account application. Now the banking account remains agnostic and the aspect code is maintained in one place. Also, now it is easy to change to a different solution or to remove the concern altogether.&lt;br /&gt;
&lt;br /&gt;
We will see how this is implemented exactly in various implementation of AOP such as AspectJ and AspectR below.&lt;br /&gt;
&lt;br /&gt;
==AOP Concepts==&lt;br /&gt;
The advice-related component of an aspect-oriented language defines a join point model (JPM). A JPM defines three things:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Joinpoint&amp;lt;/b&amp;gt; - a well defined event in the execution of a program (such as the core functionality provided by the persistence class). eg: A call to a method persist_balance from inside the withdraw method.&lt;br /&gt;
* &amp;lt;b&amp;gt;Pointcut&amp;lt;/b&amp;gt; - A collection of join points. eg: A collection of all mutator methods in BankingAccount.&lt;br /&gt;
* &amp;lt;b&amp;gt;Advice&amp;lt;/b&amp;gt; - A block of code that specifies some behavior to be executed before/after/around a certain joinpoint. &lt;br /&gt;
&lt;br /&gt;
Join-point models can be compared based on the join points exposed, how join points are specified, the operations permitted at the join points, and the structural enhancements that can be expressed.&lt;br /&gt;
&lt;br /&gt;
==AspectJ: AOP in Java==&lt;br /&gt;
AspectJ extend the Java programming language with constructs to support AOP. It is a superset of Java such that each valid Java program is also a valid AspectJ program. Also, it is designed as a general purpose language as opposed to a domain specific language. AspectJ is currently the most notable AOP technology.&lt;br /&gt;
&lt;br /&gt;
AspectJ was created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become the widely used de-facto standard for AOP by emphasizing simplicity and usability for end users. It has included IDE integrations for displaying crosscutting structure since its initial public release in 2001.&lt;br /&gt;
&lt;br /&gt;
===History and contributors===&lt;br /&gt;
Gregor Kiczales started and led the Xerox PARC team that eventually developed AspectJ; he coined the term &amp;quot;crosscutting&amp;quot;. Fourth on the team, Chris Maeda coined the term &amp;quot;aspect-oriented programming.&amp;quot; Jim Hugunin and Erik Hilsdale (Xerox PARC team members 12 and 13) were the original compiler and weaver engineers, Mik Kersten implemented the IDE integration and started the Eclipse AJDT project with Adrian Colyer (current lead of the AspectJ project) and Andrew Clement (current compiler engineer).&lt;br /&gt;
&lt;br /&gt;
The AspectBench Compiler was developed and is being maintained as a joint effort of the Programming Tools Group at the Oxford University Computing Laboratory, the Sable Research Group at McGill University and the Institute for Basic Research in Computer Science (BRICS).&lt;br /&gt;
&lt;br /&gt;
===Lanugage description===&lt;br /&gt;
All valid Java programs are also valid AspectJ programs, but AspectJ also allows programmers to define special constructs called aspects. Aspects can contain several entities unavailable to standard classes. These are:&lt;br /&gt;
&lt;br /&gt;
====Inter-type declarations==== &lt;br /&gt;
These allow a programmer to add methods, fields, or interfaces to existing classes from within the aspect. This example adds an acceptVisitor (see visitor pattern) method to the Point class:&lt;br /&gt;
&lt;br /&gt;
  aspect VisitAspect {&lt;br /&gt;
    void Point.acceptVisitor(Visitor v) {&lt;br /&gt;
      v.visit(this);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
====Pointcuts==== &lt;br /&gt;
Pointcuts allow a programmer to specify join points. All pointcuts are expressions (quantifications) that determine whether a given join point matches. A call joinpoint captures an execution event after it evaluates a method calls' arguments, but before it calls the method itself.&lt;br /&gt;
&lt;br /&gt;
For example, this point-cut specifies all calls to GetBalance method in the class BankingAccount that takes no arguments and returns long&lt;br /&gt;
&lt;br /&gt;
  call(public long BankingAccount.GetBalance())&lt;br /&gt;
&lt;br /&gt;
We may also use logical operators in the definition of pointcuts inorder to combine pointcut expressions.&lt;br /&gt;
&lt;br /&gt;
# || (OR operator) - Matches a joinpoint if either left pointcut expression or right pointcut expression matches&lt;br /&gt;
# &amp;amp;&amp;amp; (AND operator) - Matches a joinpoint if both left pointcut expression and right pointcut expression matches&lt;br /&gt;
# ! (NOT operator) - Matches all joinpoints NOT specified by the pointcut expression&lt;br /&gt;
&lt;br /&gt;
Example of a pointcut which matches both mutator methods of the BankingAccount class is&lt;br /&gt;
&lt;br /&gt;
  pointcut mutators(): call(public long BankingAccount.Withdraw(long)) || call(public long BankingAccount.Deposit(long))&lt;br /&gt;
&lt;br /&gt;
Various types of joinpoints that are possible in AspectJ are:&lt;br /&gt;
# Calls to methods and constructors&lt;br /&gt;
# Execution of methods and constructors&lt;br /&gt;
# Field access&lt;br /&gt;
# Exception handling&lt;br /&gt;
# Class initializaiton&lt;br /&gt;
# Lexical structure&lt;br /&gt;
# Control flow&lt;br /&gt;
# Self-target and argument type&lt;br /&gt;
# Conditional test&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Patterns in pointcuts&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Patterns can also be used to specify events in pointcuts. This would be useful to specify a set of events concisely.&lt;br /&gt;
&lt;br /&gt;
The use of * character is highly overloaded.&lt;br /&gt;
* Using * operator where a type is expected matches any type&lt;br /&gt;
* Using * where an identifier is expected matches any identifier&lt;br /&gt;
* * can also be used in identifier patterns&lt;br /&gt;
** the expression foo* would match any identifier that starts with foo&lt;br /&gt;
** the expression *foo* would match any identifier that has foo in it&lt;br /&gt;
* We can add + to the end of any identifier pattern to indicate that we wish to match a class and all of its subclasses&lt;br /&gt;
&lt;br /&gt;
Examples&lt;br /&gt;
* foo - class foo&lt;br /&gt;
* foo+ - class foo and all of its subclasses&lt;br /&gt;
* foo* - all classes starting with foo&lt;br /&gt;
* foo*+ - all classes starting with foo and all of their subclasses&lt;br /&gt;
* *foo* - all classes with foo in it&lt;br /&gt;
&lt;br /&gt;
====Advice==== &lt;br /&gt;
Advice allows a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. An advice must be define with respect to a pointcut. Given below is an advice defined with respect to the mutators pointcut.&lt;br /&gt;
&lt;br /&gt;
  before() : mutators() {&lt;br /&gt;
    System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This is a special type of mutator called the before mutator which is called before the joinpoint specified by the pointcut is executed.&lt;br /&gt;
&lt;br /&gt;
There are three ways to associate an advice with a pointcut:&lt;br /&gt;
# Before - run just before the pointcut&lt;br /&gt;
# After - run just after the pointcut&lt;br /&gt;
# Around - Runs instead of the pointcut with the provision for the pointcut to resume execution through a method called proceed().&lt;br /&gt;
&lt;br /&gt;
AspectJ also supports limited forms of pointcut-based static checking and aspect reuse (by inheritance). Pointcuts and advices together define composition (weaving) rules.&lt;br /&gt;
&lt;br /&gt;
====Aspect====&lt;br /&gt;
Much like a class, an Aspect is a unit of modularity. It is defined in terms of pointcuts, advices and ordinary java fields and methods. Pointcuts say which event to match and advice body says what to execute when it matches.&lt;br /&gt;
&lt;br /&gt;
  public aspect Tracer {&lt;br /&gt;
    pointcut mutators(): call(public long BankingAccount.Withdraw(long)) ||&lt;br /&gt;
                           call(public long BankingAccount.Deposit(long));&amp;lt;br/&amp;gt;&lt;br /&gt;
    before() : mutators() {&lt;br /&gt;
      System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Banking example in Java using AOP===&lt;br /&gt;
Now let us look at how persistence may be added to the BankingAccount example using AspectJ&lt;br /&gt;
&lt;br /&gt;
  class BankAccount {&amp;lt;br/&amp;gt;&lt;br /&gt;
    float balance;&amp;lt;br/&amp;gt;&lt;br /&gt;
    public BankAccount(float balance) {&lt;br /&gt;
      this.balance = balance;&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void deposit(float amount) {&lt;br /&gt;
      balance += amount&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void withdraw(float amount) {&lt;br /&gt;
      if (balance &amp;lt; amount) {&lt;br /&gt;
        throw new InsufficientFundsException();&lt;br /&gt;
      }&lt;br /&gt;
      balance -= amount;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  aspect PersistenceAspect {&amp;lt;br/&amp;gt;&lt;br /&gt;
    pointcut stateChange(BankAccount ba):&lt;br /&gt;
      (call(void BankAccount.deposit(*)) ||&lt;br /&gt;
      call(void BankAccount.withdraw(*)))&lt;br /&gt;
      &amp;amp;&amp;amp; target(ba);&amp;lt;br/&amp;gt;&lt;br /&gt;
    after(BankAccount ba) returning: stateChange(ba) {&lt;br /&gt;
      // persist ba.getBalance() value to database&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We define the pointcut stateChange as calls to the mutator functions of the BankAccount class, which are the deposit and withdraw functions in the PersistenceAspect. The persistence action should be performed after the above mentioned pointcut. So we define an advice returning, after the pointcut stateChange. So this code will execute everytime after the withdraw and deposit functions and the balance will be persisted.&lt;br /&gt;
&lt;br /&gt;
Thus the BankAccount class contains code only related to the BankAccount. It is completely free of any cross-cutting concerns such as persistence. Other concerns such as user authentication might be implemented similarly.&lt;br /&gt;
&lt;br /&gt;
==AOP in Ruby==&lt;br /&gt;
Ruby does not have native AOP support. But Ruby's metaprogramming features cover many of the aspect requirements. 'method_missing' and reflection in Ruby are classis examples of that. 'method_missing' allows the code to inspect the method call at runtime and redirect to some default method or define a new method if the called method does not exists. Similarly Ruby provides naive support to implement pointcuts and wrapper methods. They are discussed below.&lt;br /&gt;
&lt;br /&gt;
===Pointcut equivalent in Ruby===&lt;br /&gt;
There are no pointcuts in Ruby. But they can be simulated by the inspect method that Ruby provides. &lt;br /&gt;
&lt;br /&gt;
  # List all the classes&lt;br /&gt;
  ObjectSpace.each_object(Class) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the modules&lt;br /&gt;
  ObjectSpace.each_object(Module) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the instances of class String&lt;br /&gt;
  ObjectSpace.each_object(String) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Adding advice in Ruby===&lt;br /&gt;
In Ruby, alias can be used to wrap methods with advice.&lt;br /&gt;
&lt;br /&gt;
  foo.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;bar&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  foo_advice.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
  alias old_bar bar&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;{[&amp;lt; &amp;quot;+ old_bar+ &amp;quot; &amp;gt;]}”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In the above code, the alias will be executed eveytime a call to method 'bar' is made. The output of the above code will be as follows:&lt;br /&gt;
&lt;br /&gt;
  puts Foo.new.bar&lt;br /&gt;
  =&amp;gt; &amp;quot;{[&amp;lt; bar &amp;gt;]}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==AspectR==&lt;br /&gt;
We saw how AOP can be implemented in Ruby without any extra support in the above sections. AspectR is free library which provides API's to the same for Ruby. Essentially it allows you to wrap code around existing methods in your classes. This can be a good thing (and even affect how you design your code) since it separates different concerns into different parts of the code. The code for AspectR can be downloaded from [http://prdownloads.sf.net/aspectr/aspectr-0-3-5.tar.gz here]. AspectR was originally called advice.rb and written by Avi Bryant. Later Robert Feldt changed/tweaked/extended it.&lt;br /&gt;
&lt;br /&gt;
===Main features of AspectR===&lt;br /&gt;
*Joinpoints: Ruby does not have the notion of Jointpoints. But it can be considered as method or constructor calls or even exception handlers. &lt;br /&gt;
*Advices: It contains before (pre) advice, after returning and after throwing (post) advice, which indicates what is to be done before/after the jointpoint is called. &lt;br /&gt;
*Aspects: They are the classes that inherit from Aspect. It supports &amp;quot;abstract&amp;quot; Aspects and overriding between advices. &lt;br /&gt;
*Wildcards: RegExp can be used in pointcut designators, ie. to specify classes and methods to wrap advice's to. &lt;br /&gt;
*Pointcut parameters advices can access: object and method receiving call, arguments to call, return values, and exceptions raised.&lt;br /&gt;
&lt;br /&gt;
===AspectR Syntax===&lt;br /&gt;
Advice methods are passed a variable number of parameters:&lt;br /&gt;
*the first is the name of the method currently being wrapped&lt;br /&gt;
*the second is the object receiving the method call&lt;br /&gt;
*the third is the exit/return status:&lt;br /&gt;
**Array with return value(s) if the method exited normally&lt;br /&gt;
**true if the method exited with an exception&lt;br /&gt;
**nil if the method hasn't yet exited (for preAdvice)&lt;br /&gt;
*the rest are the arguments that were passed to the wrapped method.&lt;br /&gt;
&lt;br /&gt;
Here is the sample syntax:&lt;br /&gt;
  require 'aspectr'&lt;br /&gt;
  include AspectR&lt;br /&gt;
  class MyAspect &amp;lt; Aspect&lt;br /&gt;
    def someAdviceMethod(method, object, exitstatus, *args)&lt;br /&gt;
      ...&lt;br /&gt;
    end&lt;br /&gt;
      ... some other advice methods ...&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  ma = MyAspect.new&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, ... methods to wrap...)&lt;br /&gt;
  or&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, /patternToWrap/)&lt;br /&gt;
  or&lt;br /&gt;
  AspectR.wrap_classes(ma, :preAdvice, :postAdvice,&lt;br /&gt;
                      [Class1, Class2], ...methods to wrap...)&lt;br /&gt;
===Banking account example in AspectR===&lt;br /&gt;
&lt;br /&gt;
  class BankAccount&lt;br /&gt;
    attr_reader :balance&lt;br /&gt;
    def balance=(amount)&lt;br /&gt;
      @balance = amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def initialize&lt;br /&gt;
      @balance = retrieve_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def deposit(amount)&lt;br /&gt;
      @balance += amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def withdraw(amount)&lt;br /&gt;
      if @balance &amp;lt; amount&lt;br /&gt;
        raise InsufficientFundsError&lt;br /&gt;
      end&lt;br /&gt;
      @balance -= amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  class Persist &amp;lt; Aspect&lt;br /&gt;
    def retrieve_balance &lt;br /&gt;
      # Get from database&lt;br /&gt;
         ..&lt;br /&gt;
    end&lt;br /&gt;
    def persist_balance &lt;br /&gt;
      # Save to database&lt;br /&gt;
        ..&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  Persist.new.wrap(BankingAccount, :retrive_balance, :persist_balance, /[withdraw|deposit]/)&lt;br /&gt;
&lt;br /&gt;
In the above code class persists is a subclass of Aspect and has the before(retrive_balance) and after(persist_advice) advice defined in it. Then the last line of the code wraps the this persist class to the BankingAccount's withdraw and deposit methods. It also specifies what pre-advice and what post-advice should be used. The API for wrap class is as follows:&lt;br /&gt;
&lt;br /&gt;
  def wrap(target, pre, post, *args)		&lt;br /&gt;
    get_methods(target, args).each do |method_to_wrap|&lt;br /&gt;
      add_advice(target, PRE, method_to_wrap, pre)&lt;br /&gt;
      add_advice(target, POST, method_to_wrap, post)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Thus as we can see from API, the wrap method directly adds the pre and post advice to the methods (Join-points in AspectJ terminology).&lt;br /&gt;
&lt;br /&gt;
==How AspectR is different from AspectJ==&lt;br /&gt;
*Join points: Join points are not defined separately in AspectR as they are in AspectJ. Instead the method and constructor calls acts as join points. &lt;br /&gt;
*Most of the point cut designator primitives that are present in AspectR differ from AspectR.&lt;br /&gt;
*There is a huge difference in composition of pointcut designators. But you can of course specify several method calls in different classes and objects in a point cut. &lt;br /&gt;
*Around advices are more easy to add in AspectR than AspectJ but mostly before and after advices are used.&lt;br /&gt;
*AspectR has control-flow based crosscutting.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Aspect-oriented_programming Aspect Oriented Programming - wikipedia]&lt;br /&gt;
# An introduction to Aspect-Oriented Programming with AspectJ by Constantinos Constantinides, McGill University&lt;br /&gt;
# [http://en.wikipedia.org/wiki/AspectJ AspectJ - wikipedia]&lt;br /&gt;
# [http://aspectr.sourceforge.net/ AspectR - Simple aspect-oriented programming in Ruby]&lt;br /&gt;
# [http://www.ruby-doc.org/gems/docs/a/aspectr-0.3.7/AspectR.html AspectR - Ruby documentation]&lt;br /&gt;
# Aspect-Oriented Programming in Ruby by Dean Wampler&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65167</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w27 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=65167"/>
		<updated>2012-09-17T19:02:52Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Aspect Oriented Programming (AOP): AspectJ and AspectR=&lt;br /&gt;
&lt;br /&gt;
Aspect Oriented Programming (AOP) refers to a new way of designing software. It aims to increase modularity by allowing the separation of cross-cutting concerns. AOP includes programming methods and tools that support the modularization of concerns at the level of the source code. It is not a replacement to popular Object Oriented Programming (OOP), but is complimentary to it.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Aspect-oriented programming entails breaking down program logic into distinct parts, called concerns. Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they &amp;quot;cut across&amp;quot; multiple abstractions in a program. An example to this is logging. In an normal object oriented language program we might need to invoke a logger function from everywhere we this functionality.&lt;br /&gt;
&lt;br /&gt;
==Why do we need AOP? The banking example==&lt;br /&gt;
Typically, an aspect is scattered or tangled as code, making it harder to understand and maintain. It is scattered by virtue of the function (such as logging) being spread over a number of unrelated functions that might use its function, possibly in entirely unrelated systems, different source languages, etc. That means to change logging can require modifying all affected modules. Aspects become tangled not only with the mainline function of the systems in which they are expressed but also with each other. That means changing one concern entails understanding all the tangled concerns or having some means by which the effect of changes can be inferred.&lt;br /&gt;
&lt;br /&gt;
Consider a simple banking application written in Ruby.&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds'&lt;br /&gt;
 class BankAccount&amp;lt;br/&amp;gt;&lt;br /&gt;
   attr_accessor :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = 0.0&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFunds&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
This is simple enough. But is not very useful. What if we need persistence too?&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds_error'&amp;lt;br/&amp;gt;&lt;br /&gt;
 class BankAccount&lt;br /&gt;
   attr_reader :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def balance=(amount)&lt;br /&gt;
     @balance = amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = retrieve_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFundsError&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def retrieve_balance&lt;br /&gt;
     # Get from database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def persist_balance&lt;br /&gt;
     # Save to database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
We can see that now other interests have become tangled with the basic functionality (sometimes called the business logic concern). Now add:&lt;br /&gt;
&lt;br /&gt;
* Transactional behavior&lt;br /&gt;
* Authentication and authorization&lt;br /&gt;
* “Observers”, like a UI&lt;br /&gt;
&lt;br /&gt;
Also this code will be repeated across other domain objects which need the same behavior. Finally, attempt to reuse this code in a new application with a different persistence store, transaction engine, security model etc.&lt;br /&gt;
&lt;br /&gt;
===The Problem===&lt;br /&gt;
&lt;br /&gt;
* Persistence itself is already modular&lt;br /&gt;
** But the code that uses it isn’t!&lt;br /&gt;
** Persistence invocation code is scattered&lt;br /&gt;
** BankAccount code is tangled with code from other concerns&lt;br /&gt;
** BankAccount’s modularity is compromised&lt;br /&gt;
* Also, similar persistence code is added to other components&lt;br /&gt;
** Redundancy:&lt;br /&gt;
*** Hard to change all occurrences consistently.&lt;br /&gt;
*** Hard to replace the Persistence solution.&lt;br /&gt;
*** Breaks Don’t Repeat Yourself! (DRY)&lt;br /&gt;
** Reuse is hard.&lt;br /&gt;
*** The persistence solution may be different in another context.&lt;br /&gt;
&lt;br /&gt;
===The Solution: Aspect Oriented Programming===&lt;br /&gt;
AOP attempts to solve this problem by allowing the programmer to express cross-cutting concerns in stand-alone modules called aspects. Then it provides mechanisms for fine-grained modification of target component behavior to incorporate the aspects’ behaviors. Aspects can contain advice (code joined to specified points in the program) and inter-type declarations (structural members added to other classes). For example, a security module can include advice that performs a security check before accessing a bank account.&lt;br /&gt;
&lt;br /&gt;
For example in the above program, the behavior is modified by adding a 'Persistence aspect' to the program. We specify separately where these aspect should be invoked in the banking account application. Now the banking account remains agnostic and the aspect code is maintained in one place. Also, now it is easy to change to a different solution or to remove the concern altogether.&lt;br /&gt;
&lt;br /&gt;
We will see how this is implemented exactly in various implementation of AOP such as AspectJ and AspectR below.&lt;br /&gt;
&lt;br /&gt;
==AOP Concepts==&lt;br /&gt;
The advice-related component of an aspect-oriented language defines a join point model (JPM). A JPM defines three things:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Joinpoint&amp;lt;/b&amp;gt; - a well defined event in the execution of a program (such as the core functionality provided by the persistence class). eg: A call to a method persist_balance from inside the withdraw method.&lt;br /&gt;
* &amp;lt;b&amp;gt;Pointcut&amp;lt;/b&amp;gt; - A collection of join points. eg: A collection of all mutator methods in BankingAccount.&lt;br /&gt;
* &amp;lt;b&amp;gt;Advice&amp;lt;/b&amp;gt; - A block of code that specifies some behavior to be executed before/after/around a certain joinpoint. &lt;br /&gt;
&lt;br /&gt;
Join-point models can be compared based on the join points exposed, how join points are specified, the operations permitted at the join points, and the structural enhancements that can be expressed.&lt;br /&gt;
&lt;br /&gt;
==AspectJ: AOP in Java==&lt;br /&gt;
AspectJ extend the Java programming language with constructs to support AOP. It is a superset of Java such that each valid Java program is also a valid AspectJ program. Also, it is designed as a general purpose language as opposed to a domain specific language. AspectJ is currently the most notable AOP technology.&lt;br /&gt;
&lt;br /&gt;
AspectJ was created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become the widely used de-facto standard for AOP by emphasizing simplicity and usability for end users. It has included IDE integrations for displaying crosscutting structure since its initial public release in 2001.&lt;br /&gt;
&lt;br /&gt;
===History and contributors===&lt;br /&gt;
Gregor Kiczales started and led the Xerox PARC team that eventually developed AspectJ; he coined the term &amp;quot;crosscutting&amp;quot;. Fourth on the team, Chris Maeda coined the term &amp;quot;aspect-oriented programming.&amp;quot; Jim Hugunin and Erik Hilsdale (Xerox PARC team members 12 and 13) were the original compiler and weaver engineers, Mik Kersten implemented the IDE integration and started the Eclipse AJDT project with Adrian Colyer (current lead of the AspectJ project) and Andrew Clement (current compiler engineer).&lt;br /&gt;
&lt;br /&gt;
The AspectBench Compiler was developed and is being maintained as a joint effort of the Programming Tools Group at the Oxford University Computing Laboratory, the Sable Research Group at McGill University and the Institute for Basic Research in Computer Science (BRICS).&lt;br /&gt;
&lt;br /&gt;
===Lanugage description===&lt;br /&gt;
All valid Java programs are also valid AspectJ programs, but AspectJ also allows programmers to define special constructs called aspects. Aspects can contain several entities unavailable to standard classes. These are:&lt;br /&gt;
&lt;br /&gt;
====Inter-type declarations==== &lt;br /&gt;
These allow a programmer to add methods, fields, or interfaces to existing classes from within the aspect. This example adds an acceptVisitor (see visitor pattern) method to the Point class:&lt;br /&gt;
&lt;br /&gt;
  aspect VisitAspect {&lt;br /&gt;
    void Point.acceptVisitor(Visitor v) {&lt;br /&gt;
      v.visit(this);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
====Pointcuts==== &lt;br /&gt;
Pointcuts allow a programmer to specify join points. All pointcuts are expressions (quantifications) that determine whether a given join point matches. A call joinpoint captures an execution event after it evaluates a method calls' arguments, but before it calls the method itself.&lt;br /&gt;
&lt;br /&gt;
For example, this point-cut specifies all calls to GetBalance method in the class BankingAccount that takes no arguments and returns long&lt;br /&gt;
&lt;br /&gt;
  call(public long BankingAccount.GetBalance())&lt;br /&gt;
&lt;br /&gt;
We may also use logical operators in the definition of pointcuts inorder to combine pointcut expressions.&lt;br /&gt;
&lt;br /&gt;
# || (OR operator) - Matches a joinpoint if either left pointcut expression or right pointcut expression matches&lt;br /&gt;
# &amp;amp;&amp;amp; (AND operator) - Matches a joinpoint if both left pointcut expression and right pointcut expression matches&lt;br /&gt;
# ! (NOT operator) - Matches all joinpoints NOT specified by the pointcut expression&lt;br /&gt;
&lt;br /&gt;
Example of a pointcut which matches both mutator methods of the BankingAccount class is&lt;br /&gt;
&lt;br /&gt;
  pointcut mutators(): call(public long BankingAccount.Withdraw(long)) || call(public long BankingAccount.Deposit(long))&lt;br /&gt;
&lt;br /&gt;
Various types of joinpoints that are possible in AspectJ are:&lt;br /&gt;
# Calls to methods and constructors&lt;br /&gt;
# Execution of methods and constructors&lt;br /&gt;
# Field access&lt;br /&gt;
# Exception handling&lt;br /&gt;
# Class initializaiton&lt;br /&gt;
# Lexical structure&lt;br /&gt;
# Control flow&lt;br /&gt;
# Self-target and argument type&lt;br /&gt;
# Conditional test&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Patterns in pointcuts&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Patterns can also be used to specify events in pointcuts. This would be useful to specify a set of events concisely.&lt;br /&gt;
&lt;br /&gt;
The use of * character is highly overloaded.&lt;br /&gt;
* Using * operator where a type is expected matches any type&lt;br /&gt;
* Using * where an identifier is expected matches any identifier&lt;br /&gt;
* * can also be used in identifier patterns&lt;br /&gt;
** the expression foo* would match any identifier that starts with foo&lt;br /&gt;
** the expression *foo* would match any identifier that has foo in it&lt;br /&gt;
* We can add + to the end of any identifier pattern to indicate that we wish to match a class and all of its subclasses&lt;br /&gt;
&lt;br /&gt;
Examples&lt;br /&gt;
* foo - class foo&lt;br /&gt;
* foo+ - class foo and all of its subclasses&lt;br /&gt;
* foo* - all classes starting with foo&lt;br /&gt;
* foo*+ - all classes starting with foo and all of their subclasses&lt;br /&gt;
* *foo* - all classes with foo in it&lt;br /&gt;
&lt;br /&gt;
====Advice==== &lt;br /&gt;
Advice allows a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. An advice must be define with respect to a pointcut. Given below is an advice defined with respect to the mutators pointcut.&lt;br /&gt;
&lt;br /&gt;
  before() : mutators() {&lt;br /&gt;
    System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This is a special type of mutator called the before mutator which is called before the joinpoint specified by the pointcut is executed.&lt;br /&gt;
&lt;br /&gt;
There are three ways to associate an advice with a pointcut:&lt;br /&gt;
# Before - run just before the pointcut&lt;br /&gt;
# After - run just after the pointcut&lt;br /&gt;
# Around - Runs instead of the pointcut with the provision for the pointcut to resume execution through a method called proceed().&lt;br /&gt;
&lt;br /&gt;
AspectJ also supports limited forms of pointcut-based static checking and aspect reuse (by inheritance). Pointcuts and advices together define composition (weaving) rules.&lt;br /&gt;
&lt;br /&gt;
====Aspect====&lt;br /&gt;
Much like a class, an Aspect is a unit of modularity. It is defined in terms of pointcuts, advices and ordinary java fields and methods. Pointcuts say which event to match and advice body says what to execute when it matches.&lt;br /&gt;
&lt;br /&gt;
  public aspect Tracer {&lt;br /&gt;
    pointcut mutators(): call(public long BankingAccount.Withdraw(long)) ||&lt;br /&gt;
                           call(public long BankingAccount.Deposit(long));&amp;lt;br/&amp;gt;&lt;br /&gt;
    before() : mutators() {&lt;br /&gt;
      System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Banking example in Java using AOP===&lt;br /&gt;
Now let us look at how persistence may be added to the BankingAccount example using AspectJ&lt;br /&gt;
&lt;br /&gt;
  class BankAccount {&amp;lt;br/&amp;gt;&lt;br /&gt;
    float balance;&amp;lt;br/&amp;gt;&lt;br /&gt;
    public BankAccount(float balance) {&lt;br /&gt;
      this.balance = balance;&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void deposit(float amount) {&lt;br /&gt;
      balance += amount&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void withdraw(float amount) {&lt;br /&gt;
      if (balance &amp;lt; amount) {&lt;br /&gt;
        throw new InsufficientFundsException();&lt;br /&gt;
      }&lt;br /&gt;
      balance -= amount;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  aspect PersistenceAspect {&amp;lt;br/&amp;gt;&lt;br /&gt;
    pointcut stateChange(BankAccount ba):&lt;br /&gt;
      (call(void BankAccount.deposit(*)) ||&lt;br /&gt;
      call(void BankAccount.withdraw(*)))&lt;br /&gt;
      &amp;amp;&amp;amp; target(ba);&amp;lt;br/&amp;gt;&lt;br /&gt;
    after(BankAccount ba) returning: stateChange(ba) {&lt;br /&gt;
      // persist ba.getBalance() value to database&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We define the pointcut stateChange as calls to the mutator functions of the BankAccount class, which are the deposit and withdraw functions in the PersistenceAspect. The persistence action should be performed after the above mentioned pointcut. So we define an advice returning, after the pointcut stateChange. So this code will execute everytime after the withdraw and deposit functions and the balance will be persisted.&lt;br /&gt;
&lt;br /&gt;
Thus the BankAccount class contains code only related to the BankAccount. It is completely free of any cross-cutting concerns such as persistence. Other concerns such as user authentication might be implemented similarly.&lt;br /&gt;
&lt;br /&gt;
==AOP in Ruby==&lt;br /&gt;
Ruby does not have native AOP support. But Ruby's metaprogramming features cover many of the aspect requirements. 'method_missing' and reflection in Ruby are classis examples of that. 'method_missing' allows the code to inspect the method call at runtime and redirect to some default method or define a new method if the called method does not exists. Similarly Ruby provides naive support to implement pointcuts and wrapper methods. They are discussed below.&lt;br /&gt;
&lt;br /&gt;
===Pointcut equivalent in Ruby===&lt;br /&gt;
There are no pointcuts in Ruby. But they can be simulated by the inspect method that Ruby provides. &lt;br /&gt;
&lt;br /&gt;
  # List all the classes&lt;br /&gt;
  ObjectSpace.each_object(Class) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the modules&lt;br /&gt;
  ObjectSpace.each_object(Module) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the instances of class String&lt;br /&gt;
  ObjectSpace.each_object(String) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Adding advice in Ruby===&lt;br /&gt;
In Ruby, alias can be used to wrap methods with advice.&lt;br /&gt;
&lt;br /&gt;
  foo.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;bar&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  foo_advice.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
  alias old_bar bar&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;{[&amp;lt; &amp;quot;+ old_bar+ &amp;quot; &amp;gt;]}”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In the above code, the alias will be executed eveytime a call to method 'bar' is made. The output of the above code will be as follows:&lt;br /&gt;
&lt;br /&gt;
  puts Foo.new.bar&lt;br /&gt;
  =&amp;gt; &amp;quot;{[&amp;lt; bar &amp;gt;]}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==AspectR==&lt;br /&gt;
We saw how AOP can be implemented in Ruby without any extra support in the above sections. AspectR is free library which provides API's to the same for Ruby. Essentially it allows you to wrap code around existing methods in your classes. This can be a good thing (and even affect how you design your code) since it separates different concerns into different parts of the code. The code for AspectR can be downloaded from [http://prdownloads.sf.net/aspectr/aspectr-0-3-5.tar.gz here]. AspectR was originally called advice.rb and written by Avi Bryant. Later Robert Feldt changed/tweaked/extended it.&lt;br /&gt;
&lt;br /&gt;
===Main features of AspectR===&lt;br /&gt;
*Joinpoints: Ruby does not have the notion of Jointpoints. But it can be considered as method or constructor calls or even exception handlers. &lt;br /&gt;
*Advices: It contains before (pre) advice, after returning and after throwing (post) advice, which indicates what is to be done before/after the jointpoint is called. &lt;br /&gt;
*Aspects: They are the classes that inherit from Aspect. It supports &amp;quot;abstract&amp;quot; Aspects and overriding between advices. &lt;br /&gt;
*Wildcards: RegExp can be used in pointcut designators, ie. to specify classes and methods to wrap advice's to. &lt;br /&gt;
*Pointcut parameters advices can access: object and method receiving call, arguments to call, return values, and exceptions raised.&lt;br /&gt;
&lt;br /&gt;
===AspectR Syntax===&lt;br /&gt;
Advice methods are passed a variable number of parameters:&lt;br /&gt;
*the first is the name of the method currently being wrapped&lt;br /&gt;
*the second is the object receiving the method call&lt;br /&gt;
*the third is the exit/return status:&lt;br /&gt;
**Array with return value(s) if the method exited normally&lt;br /&gt;
**true if the method exited with an exception&lt;br /&gt;
**nil if the method hasn't yet exited (for preAdvice)&lt;br /&gt;
*the rest are the arguments that were passed to the wrapped method.&lt;br /&gt;
&lt;br /&gt;
Here is the sample syntax:&lt;br /&gt;
  require 'aspectr'&lt;br /&gt;
  include AspectR&lt;br /&gt;
  class MyAspect &amp;lt; Aspect&lt;br /&gt;
    def someAdviceMethod(method, object, exitstatus, *args)&lt;br /&gt;
      ...&lt;br /&gt;
    end&lt;br /&gt;
      ... some other advice methods ...&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  ma = MyAspect.new&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, ... methods to wrap...)&lt;br /&gt;
  or&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, /patternToWrap/)&lt;br /&gt;
  or&lt;br /&gt;
  AspectR.wrap_classes(ma, :preAdvice, :postAdvice,&lt;br /&gt;
                      [Class1, Class2], ...methods to wrap...)&lt;br /&gt;
===Banking account example in AspectR===&lt;br /&gt;
&lt;br /&gt;
  class BankAccount&lt;br /&gt;
    attr_reader :balance&lt;br /&gt;
    def balance=(amount)&lt;br /&gt;
      @balance = amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def initialize&lt;br /&gt;
      @balance = retrieve_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def deposit(amount)&lt;br /&gt;
      @balance += amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def withdraw(amount)&lt;br /&gt;
      if @balance &amp;lt; amount&lt;br /&gt;
        raise InsufficientFundsError&lt;br /&gt;
      end&lt;br /&gt;
      @balance -= amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  class Persist &amp;lt; Aspect&lt;br /&gt;
    def retrieve_balance &lt;br /&gt;
      # Get from database&lt;br /&gt;
         ..&lt;br /&gt;
    end&lt;br /&gt;
    def persist_balance &lt;br /&gt;
      # Save to database&lt;br /&gt;
        ..&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  Persist.new.wrap(BankingAccount, :retrive_balance, :persist_balance, /[withdraw|deposit]/)&lt;br /&gt;
&lt;br /&gt;
In the above code class persists is a subclass of Aspect and has the before(retrive_balance) and after(persist_advice) advice defined in it. Then the last line of the code wraps the this persist class to the BankingAccount's withdraw and deposit methods. It also specifies what pre-advice and what post-advice should be used. The API for wrap class is as follows:&lt;br /&gt;
&lt;br /&gt;
  def wrap(target, pre, post, *args)		&lt;br /&gt;
    get_methods(target, args).each do |method_to_wrap|&lt;br /&gt;
      add_advice(target, PRE, method_to_wrap, pre)&lt;br /&gt;
      add_advice(target, POST, method_to_wrap, post)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Thus as we can see from API, the wrap method directly adds the pre and post advice to the methods (Join-points in AspectJ terminology).&lt;br /&gt;
&lt;br /&gt;
==How AspectR is different from AspectJ==&lt;br /&gt;
*Join points: Join points are not defined separately in AspectR as they are in AspectJ. Instead the method and constructor calls acts as join points. &lt;br /&gt;
*Most of the point cut designator primitives that are present in AspectR differ from AspectR.&lt;br /&gt;
*There is a huge difference in composition of pointcut designators. But you can of course specify several method calls in different classes and objects in a point cut. &lt;br /&gt;
*Around advices are more easy to add in AspectR than AspectJ but mostly before and after advices are used.&lt;br /&gt;
*AspectR has control-flow based crosscutting.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Aspect-oriented_programming Aspect Oriented Programming - wikipedia]&lt;br /&gt;
# An introduction to Aspect-Oriented Programming with AspectJ by Constantinos Constantinides, McGill University&lt;br /&gt;
# [[AspectJ|AspectJ - wikipedia]]&lt;br /&gt;
# [http://aspectr.sourceforge.net/ AspectR - Simple aspect-oriented programming in Ruby]&lt;br /&gt;
# [http://www.ruby-doc.org/gems/docs/a/aspectr-0.3.7/AspectR.html AspectR - Ruby documentation]&lt;br /&gt;
# Aspect-Oriented Programming in Ruby by Dean Wampler&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=64914</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w27 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w27_ms&amp;diff=64914"/>
		<updated>2012-09-15T00:21:09Z</updated>

		<summary type="html">&lt;p&gt;Sdkshirs: /* Banking account example in AspectR */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Aspect Oriented Programming (AOP): AspectJ and AspectR=&lt;br /&gt;
&lt;br /&gt;
Aspect Oriented Programming (AOP) refers to a new way of designing software. It aims to increase modularity by allowing the separation of cross-cutting concerns. AOP includes programming methods and tools that support the modularization of concerns at the level of the source code. It is not a replacement to popular Object Oriented Programming (OOP), but is complimentary to it.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Aspect-oriented programming entails breaking down program logic into distinct parts, called concerns. Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they &amp;quot;cut across&amp;quot; multiple abstractions in a program. An example to this is logging. In an normal object oriented language program we might need to invoke a logger function from everywhere we this functionality.&lt;br /&gt;
&lt;br /&gt;
==Why do we need AOP? The banking example==&lt;br /&gt;
Typically, an aspect is scattered or tangled as code, making it harder to understand and maintain. It is scattered by virtue of the function (such as logging) being spread over a number of unrelated functions that might use its function, possibly in entirely unrelated systems, different source languages, etc. That means to change logging can require modifying all affected modules. Aspects become tangled not only with the mainline function of the systems in which they are expressed but also with each other. That means changing one concern entails understanding all the tangled concerns or having some means by which the effect of changes can be inferred.&lt;br /&gt;
&lt;br /&gt;
Consider a simple banking application written in Ruby.&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds'&lt;br /&gt;
 class BankAccount&amp;lt;br/&amp;gt;&lt;br /&gt;
   attr_accessor :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = 0.0&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFunds&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
This is simple enough. But is not very useful. What if we need persistence too?&lt;br /&gt;
&lt;br /&gt;
 require 'insufficient_funds_error'&amp;lt;br/&amp;gt;&lt;br /&gt;
 class BankAccount&lt;br /&gt;
   attr_reader :balance&amp;lt;br/&amp;gt;&lt;br /&gt;
   def balance=(amount)&lt;br /&gt;
     @balance = amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def initialize&lt;br /&gt;
     @balance = retrieve_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def deposit(amount)&lt;br /&gt;
     @balance += amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def withdraw(amount)&lt;br /&gt;
     if @balance &amp;lt; amount&lt;br /&gt;
       raise InsufficientFundsError&lt;br /&gt;
     end&lt;br /&gt;
     @balance -= amount&lt;br /&gt;
     persist_balance&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def retrieve_balance&lt;br /&gt;
     # Get from database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
   def persist_balance&lt;br /&gt;
     # Save to database&lt;br /&gt;
   end&amp;lt;br/&amp;gt;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
We can see that now other interests have become tangled with the basic functionality (sometimes called the business logic concern). Now add:&lt;br /&gt;
&lt;br /&gt;
* Transactional behavior&lt;br /&gt;
* Authentication and authorization&lt;br /&gt;
* “Observers”, like a UI&lt;br /&gt;
&lt;br /&gt;
Also this code will be repeated across other domain objects which need the same behavior. Finally, attempt to reuse this code in a new application with a different persistence store, transaction engine, security model etc.&lt;br /&gt;
&lt;br /&gt;
===The Problem===&lt;br /&gt;
&lt;br /&gt;
* Persistence itself is already modular&lt;br /&gt;
** But the code that uses it isn’t!&lt;br /&gt;
** Persistence invocation code is scattered&lt;br /&gt;
** BankAccount code is tangled with code from other concerns&lt;br /&gt;
** BankAccount’s modularity is compromised&lt;br /&gt;
* Also, similar persistence code is added to other components&lt;br /&gt;
** Redundancy:&lt;br /&gt;
*** Hard to change all occurrences consistently.&lt;br /&gt;
*** Hard to replace the Persistence solution.&lt;br /&gt;
*** Breaks Don’t Repeat Yourself! (DRY)&lt;br /&gt;
** Reuse is hard.&lt;br /&gt;
*** The persistence solution may be different in another context.&lt;br /&gt;
&lt;br /&gt;
===The Solution: Aspect Oriented Programming===&lt;br /&gt;
AOP attempts to solve this problem by allowing the programmer to express cross-cutting concerns in stand-alone modules called aspects. Then it provides mechanisms for fine-grained modification of target component behavior to incorporate the aspects’ behaviors. Aspects can contain advice (code joined to specified points in the program) and inter-type declarations (structural members added to other classes). For example, a security module can include advice that performs a security check before accessing a bank account.&lt;br /&gt;
&lt;br /&gt;
For example in the above program, the behavior is modified by adding a 'Persistence aspect' to the program. We specify separately where these aspect should be invoked in the banking account application. Now the banking account remains agnostic and the aspect code is maintained in one place. Also, now it is easy to change to a different solution or to remove the concern altogether.&lt;br /&gt;
&lt;br /&gt;
We will see how this is implemented exactly in various implementation of AOP such as AspectJ and AspectR below.&lt;br /&gt;
&lt;br /&gt;
==AOP Concepts==&lt;br /&gt;
The advice-related component of an aspect-oriented language defines a join point model (JPM). A JPM defines three things:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;Joinpoint&amp;lt;/b&amp;gt; - a well defined event in the execution of a program (such as the core functionality provided by the persistence class). eg: A call to a method persist_balance from inside the withdraw method.&lt;br /&gt;
* &amp;lt;b&amp;gt;Pointcut&amp;lt;/b&amp;gt; - A collection of join points. eg: A collection of all mutator methods in BankingAccount.&lt;br /&gt;
* &amp;lt;b&amp;gt;Advice&amp;lt;/b&amp;gt; - A block of code that specifies some behavior to be executed before/after/around a certain joinpoint. &lt;br /&gt;
&lt;br /&gt;
Join-point models can be compared based on the join points exposed, how join points are specified, the operations permitted at the join points, and the structural enhancements that can be expressed.&lt;br /&gt;
&lt;br /&gt;
==AspectJ: AOP in Java==&lt;br /&gt;
AspectJ extend the Java programming language with constructs to support AOP. It is a superset of Java such that each valid Java program is also a valid AspectJ program. Also, it is designed as a general purpose language as opposed to a domain specific language. AspectJ is currently the most notable AOP technology.&lt;br /&gt;
&lt;br /&gt;
AspectJ was created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become the widely used de-facto standard for AOP by emphasizing simplicity and usability for end users. It has included IDE integrations for displaying crosscutting structure since its initial public release in 2001.&lt;br /&gt;
&lt;br /&gt;
===History and contributors===&lt;br /&gt;
Gregor Kiczales started and led the Xerox PARC team that eventually developed AspectJ; he coined the term &amp;quot;crosscutting&amp;quot;. Fourth on the team, Chris Maeda coined the term &amp;quot;aspect-oriented programming.&amp;quot; Jim Hugunin and Erik Hilsdale (Xerox PARC team members 12 and 13) were the original compiler and weaver engineers, Mik Kersten implemented the IDE integration and started the Eclipse AJDT project with Adrian Colyer (current lead of the AspectJ project) and Andrew Clement (current compiler engineer).&lt;br /&gt;
&lt;br /&gt;
The AspectBench Compiler was developed and is being maintained as a joint effort of the Programming Tools Group at the Oxford University Computing Laboratory, the Sable Research Group at McGill University and the Institute for Basic Research in Computer Science (BRICS).&lt;br /&gt;
&lt;br /&gt;
===Lanugage description===&lt;br /&gt;
All valid Java programs are also valid AspectJ programs, but AspectJ also allows programmers to define special constructs called aspects. Aspects can contain several entities unavailable to standard classes. These are:&lt;br /&gt;
&lt;br /&gt;
====Inter-type declarations==== &lt;br /&gt;
These allow a programmer to add methods, fields, or interfaces to existing classes from within the aspect. This example adds an acceptVisitor (see visitor pattern) method to the Point class:&lt;br /&gt;
&lt;br /&gt;
  aspect VisitAspect {&lt;br /&gt;
    void Point.acceptVisitor(Visitor v) {&lt;br /&gt;
      v.visit(this);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
====Pointcuts==== &lt;br /&gt;
Pointcuts allow a programmer to specify join points. All pointcuts are expressions (quantifications) that determine whether a given join point matches. A call joinpoint captures an execution event after it evaluates a method calls' arguments, but before it calls the method itself.&lt;br /&gt;
&lt;br /&gt;
For example, this point-cut specifies all calls to GetBalance method in the class BankingAccount that takes no arguments and returns long&lt;br /&gt;
&lt;br /&gt;
  call(public long BankingAccount.GetBalance())&lt;br /&gt;
&lt;br /&gt;
We may also use logical operators in the definition of pointcuts inorder to combine pointcut expressions.&lt;br /&gt;
&lt;br /&gt;
# || (OR operator) - Matches a joinpoint if either left pointcut expression or right pointcut expression matches&lt;br /&gt;
# &amp;amp;&amp;amp; (AND operator) - Matches a joinpoint if both left pointcut expression and right pointcut expression matches&lt;br /&gt;
# ! (NOT operator) - Matches all joinpoints NOT specified by the pointcut expression&lt;br /&gt;
&lt;br /&gt;
Example of a pointcut which matches both mutator methods of the BankingAccount class is&lt;br /&gt;
&lt;br /&gt;
  pointcut mutators(): call(public long BankingAccount.Withdraw(long)) || call(public long BankingAccount.Deposit(long))&lt;br /&gt;
&lt;br /&gt;
Various types of joinpoints that are possible in AspectJ are:&lt;br /&gt;
# Calls to methods and constructors&lt;br /&gt;
# Execution of methods and constructors&lt;br /&gt;
# Field access&lt;br /&gt;
# Exception handling&lt;br /&gt;
# Class initializaiton&lt;br /&gt;
# Lexical structure&lt;br /&gt;
# Control flow&lt;br /&gt;
# Self-target and argument type&lt;br /&gt;
# Conditional test&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Patterns in pointcuts&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
Patterns can also be used to specify events in pointcuts. This would be useful to specify a set of events concisely.&lt;br /&gt;
&lt;br /&gt;
The use of * character is highly overloaded.&lt;br /&gt;
* Using * operator where a type is expected matches any type&lt;br /&gt;
* Using * where an identifier is expected matches any identifier&lt;br /&gt;
* * can also be used in identifier patterns&lt;br /&gt;
** the expression foo* would match any identifier that starts with foo&lt;br /&gt;
** the expression *foo* would match any identifier that has foo in it&lt;br /&gt;
* We can add + to the end of any identifier pattern to indicate that we wish to match a class and all of its subclasses&lt;br /&gt;
&lt;br /&gt;
Examples&lt;br /&gt;
* foo - class foo&lt;br /&gt;
* foo+ - class foo and all of its subclasses&lt;br /&gt;
* foo* - all classes starting with foo&lt;br /&gt;
* foo*+ - all classes starting with foo and all of their subclasses&lt;br /&gt;
* *foo* - all classes with foo in it&lt;br /&gt;
&lt;br /&gt;
====Advice==== &lt;br /&gt;
Advice allows a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. An advice must be define with respect to a pointcut. Given below is an advice defined with respect to the mutators pointcut.&lt;br /&gt;
&lt;br /&gt;
  before() : mutators() {&lt;br /&gt;
    System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This is a special type of mutator called the before mutator which is called before the joinpoint specified by the pointcut is executed.&lt;br /&gt;
&lt;br /&gt;
There are three ways to associate an advice with a pointcut:&lt;br /&gt;
# Before - run just before the pointcut&lt;br /&gt;
# After - run just after the pointcut&lt;br /&gt;
# Around - Runs instead of the pointcut with the provision for the pointcut to resume execution through a method called proceed().&lt;br /&gt;
&lt;br /&gt;
AspectJ also supports limited forms of pointcut-based static checking and aspect reuse (by inheritance). Pointcuts and advices together define composition (weaving) rules.&lt;br /&gt;
&lt;br /&gt;
====Aspect====&lt;br /&gt;
Much like a class, an Aspect is a unit of modularity. It is defined in terms of pointcuts, advices and ordinary java fields and methods. Pointcuts say which event to match and advice body says what to execute when it matches.&lt;br /&gt;
&lt;br /&gt;
  public aspect Tracer {&lt;br /&gt;
    pointcut mutators(): call(public long BankingAccount.Withdraw(long)) ||&lt;br /&gt;
                           call(public long BankingAccount.Deposit(long));&amp;lt;br/&amp;gt;&lt;br /&gt;
    before() : mutators() {&lt;br /&gt;
      System.out.println(&amp;quot;Mutator called&amp;quot;);&lt;br /&gt;
    }    &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Banking example in Java using AOP===&lt;br /&gt;
Now let us look at how persistence may be added to the BankingAccount example using AspectJ&lt;br /&gt;
&lt;br /&gt;
  class BankAccount {&amp;lt;br/&amp;gt;&lt;br /&gt;
    float balance;&amp;lt;br/&amp;gt;&lt;br /&gt;
    public BankAccount(float balance) {&lt;br /&gt;
      this.balance = balance;&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void deposit(float amount) {&lt;br /&gt;
      balance += amount&lt;br /&gt;
    }&amp;lt;br/&amp;gt;&lt;br /&gt;
    public void withdraw(float amount) {&lt;br /&gt;
      if (balance &amp;lt; amount) {&lt;br /&gt;
        throw new InsufficientFundsException();&lt;br /&gt;
      }&lt;br /&gt;
      balance -= amount;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  aspect PersistenceAspect {&amp;lt;br/&amp;gt;&lt;br /&gt;
    pointcut stateChange(BankAccount ba):&lt;br /&gt;
      (call(void BankAccount.deposit(*)) ||&lt;br /&gt;
      call(void BankAccount.withdraw(*)))&lt;br /&gt;
      &amp;amp;&amp;amp; target(ba);&amp;lt;br/&amp;gt;&lt;br /&gt;
    after(BankAccount ba) returning: stateChange(ba) {&lt;br /&gt;
      // persist ba.getBalance() value to database&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We define the pointcut stateChange as calls to the mutator functions of the BankAccount class, which are the deposit and withdraw functions in the PersistenceAspect. The persistence action should be performed after the above mentioned pointcut. So we define an advice returning, after the pointcut stateChange. So this code will execute everytime after the withdraw and deposit functions and the balance will be persisted.&lt;br /&gt;
&lt;br /&gt;
Thus the BankAccount class contains code only related to the BankAccount. It is completely free of any cross-cutting concerns such as persistence. Other concerns such as user authentication might be implemented similarly.&lt;br /&gt;
&lt;br /&gt;
==AOP in Ruby==&lt;br /&gt;
Ruby does not have native AOP support. But Ruby's metaprogramming features cover many of the aspect requirements. 'method_missing' and reflection in Ruby are classis examples of that. 'method_missing' allows the code to inspect the method call at runtime and redirect to some default method or define a new method if the called method does not exists. Similarly Ruby provides naive support to implement pointcuts and wrapper methods. They are discussed below.&lt;br /&gt;
&lt;br /&gt;
===Pointcut equivalent in Ruby===&lt;br /&gt;
There are no pointcuts in Ruby. But they can be simulated by the inspect method that Ruby provides. &lt;br /&gt;
&lt;br /&gt;
  # List all the classes&lt;br /&gt;
  ObjectSpace.each_object(Class) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the modules&lt;br /&gt;
  ObjectSpace.each_object(Module) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  # List all the instances of class String&lt;br /&gt;
  ObjectSpace.each_object(String) do |o|&lt;br /&gt;
    puts o.inspect&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
===Adding advice in Ruby===&lt;br /&gt;
In Ruby, alias can be used to wrap methods with advice.&lt;br /&gt;
&lt;br /&gt;
  foo.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;bar&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  foo_advice.rb&lt;br /&gt;
  class Foo&lt;br /&gt;
  alias old_bar bar&lt;br /&gt;
    def bar&lt;br /&gt;
      &amp;quot;{[&amp;lt; &amp;quot;+ old_bar+ &amp;quot; &amp;gt;]}”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In the above code, the alias will be executed eveytime a call to method 'bar' is made. The output of the above code will be as follows:&lt;br /&gt;
&lt;br /&gt;
  puts Foo.new.bar&lt;br /&gt;
  =&amp;gt; &amp;quot;{[&amp;lt; bar &amp;gt;]}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==AspectR==&lt;br /&gt;
We saw how AOP can be implemented in Ruby without any extra support in the above sections. AspectR is free library which provides API's to the same for Ruby. Essentially it allows you to wrap code around existing methods in your classes. This can be a good thing (and even affect how you design your code) since it separates different concerns into different parts of the code. The code for AspectR can be downloaded from [http://prdownloads.sf.net/aspectr/aspectr-0-3-5.tar.gz here]. AspectR was originally called advice.rb and written by Avi Bryant. Later Robert Feldt changed/tweaked/extended it.&lt;br /&gt;
&lt;br /&gt;
===Main features of AspectR===&lt;br /&gt;
*Joinpoints: Ruby does not have the notion of Jointpoints. But it can be considered as method or constructor calls or even exception handlers. &lt;br /&gt;
*Advices: It contains before (pre) advice, after returning and after throwing (post) advice, which indicates what is to be done before/after the jointpoint is called. &lt;br /&gt;
*Aspects: They are the classes that inherit from Aspect. It supports &amp;quot;abstract&amp;quot; Aspects and overriding between advices. &lt;br /&gt;
*Wildcards: RegExp can be used in pointcut designators, ie. to specify classes and methods to wrap advice's to. &lt;br /&gt;
*Pointcut parameters advices can access: object and method receiving call, arguments to call, return values, and exceptions raised.&lt;br /&gt;
&lt;br /&gt;
===AspectR Syntax===&lt;br /&gt;
Advice methods are passed a variable number of parameters:&lt;br /&gt;
*the first is the name of the method currently being wrapped&lt;br /&gt;
*the second is the object receiving the method call&lt;br /&gt;
*the third is the exit/return status:&lt;br /&gt;
**Array with return value(s) if the method exited normally&lt;br /&gt;
**true if the method exited with an exception&lt;br /&gt;
**nil if the method hasn't yet exited (for preAdvice)&lt;br /&gt;
*the rest are the arguments that were passed to the wrapped method.&lt;br /&gt;
&lt;br /&gt;
Here is the sample syntax:&lt;br /&gt;
  require 'aspectr'&lt;br /&gt;
  include AspectR&lt;br /&gt;
  class MyAspect &amp;lt; Aspect&lt;br /&gt;
    def someAdviceMethod(method, object, exitstatus, *args)&lt;br /&gt;
      ...&lt;br /&gt;
    end&lt;br /&gt;
      ... some other advice methods ...&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  ma = MyAspect.new&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, ... methods to wrap...)&lt;br /&gt;
  or&lt;br /&gt;
  ma.wrap(someClassorObject, :preAdvice, :postAdvice, /patternToWrap/)&lt;br /&gt;
  or&lt;br /&gt;
  AspectR.wrap_classes(ma, :preAdvice, :postAdvice,&lt;br /&gt;
                      [Class1, Class2], ...methods to wrap...)&lt;br /&gt;
===Banking account example in AspectR===&lt;br /&gt;
&lt;br /&gt;
  class BankAccount&lt;br /&gt;
    attr_reader :balance&lt;br /&gt;
    def balance=(amount)&lt;br /&gt;
      @balance = amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def initialize&lt;br /&gt;
      @balance = retrieve_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def deposit(amount)&lt;br /&gt;
      @balance += amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&amp;lt;br/&amp;gt;&lt;br /&gt;
    def withdraw(amount)&lt;br /&gt;
      if @balance &amp;lt; amount&lt;br /&gt;
        raise InsufficientFundsError&lt;br /&gt;
      end&lt;br /&gt;
      @balance -= amount&lt;br /&gt;
      persist_balance &lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  class Persist &amp;lt; Aspect&lt;br /&gt;
    def retrieve_balance &lt;br /&gt;
      # Get from database&lt;br /&gt;
         ..&lt;br /&gt;
    end&lt;br /&gt;
    def persist_balance &lt;br /&gt;
      # Save to database&lt;br /&gt;
        ..&lt;br /&gt;
    end&lt;br /&gt;
  end&amp;lt;br/&amp;gt;&lt;br /&gt;
  Persist.new.wrap(BankingAccount, :retrive_balance, :persist_balance, /[withdraw|deposit]/)&lt;br /&gt;
&lt;br /&gt;
In the above code class persists is a subclass of Aspect and has the before(retrive_balance) and after(persist_advice) advice defined in it. Then the last line of the code wraps the this persist class to the BankingAccount's withdraw and deposit methods. It also specifies what pre-advice and what post-advice should be used. The API for wrap class is as follows:&lt;br /&gt;
&lt;br /&gt;
  def wrap(target, pre, post, *args)		&lt;br /&gt;
    get_methods(target, args).each do |method_to_wrap|&lt;br /&gt;
      add_advice(target, PRE, method_to_wrap, pre)&lt;br /&gt;
      add_advice(target, POST, method_to_wrap, post)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Thus as we can see from API, the wrap method directly adds the pre and post advice to the methods (Join-points in AspectJ terminology).&lt;br /&gt;
&lt;br /&gt;
==How AspectR is different from AspectJ==&lt;br /&gt;
*Join points: Join points are not defined separately in AspectR as they are in AspectJ. Instead the method and constructor calls acts as join points. &lt;br /&gt;
*Most of the point cut designator primitives that are present in AspectR differ from AspectR.&lt;br /&gt;
*There is a huge difference in composition of pointcut designators. But you can of course specify several method calls in different classes and objects in a point cut. &lt;br /&gt;
*Around advices are more easy to add in AspectR than AspectJ but mostly before and after advices are used.&lt;br /&gt;
*AspectR has control-flow based crosscutting.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
# [[Aspect-oriented_programming|Aspect Oriented Programming - wikipedia]]&lt;br /&gt;
# An introduction to Aspect-Oriented Programming with AspectJ by Constantinos Constantinides, McGill University&lt;br /&gt;
# [[AspectJ|AspectJ - wikipedia]]&lt;br /&gt;
# [http://aspectr.sourceforge.net/ AspectR - Simple aspect-oriented programming in Ruby]&lt;br /&gt;
# [http://www.ruby-doc.org/gems/docs/a/aspectr-0.3.7/AspectR.html AspectR - Ruby documentation]&lt;br /&gt;
# Aspect-Oriented Programming in Ruby by Dean Wampler&lt;/div&gt;</summary>
		<author><name>Sdkshirs</name></author>
	</entry>
</feed>