Difference between revisions of "OOP Analysis Design Mandatory Principles"

From Catglobe Wiki
Jump to: navigation, search
Line 1: Line 1:
== Open &amp; Closed Principle<br>  ==
+
== Open &amp; Closed Principle (OCP)<br>  ==
  
 
=== Introduction:<br>  ===
 
=== Introduction:<br>  ===
Line 7: Line 7:
 
=== Description:<br>  ===
 
=== Description:<br>  ===
  
#Class can be subclassed for extension<br>
+
#Class can be subclassed for extension<br>  
#Class' behaviors can be overriden<br>
+
#Class' behaviors can be overriden<br>  
#No modification is on the locked behaviors of base class (<u>related open behaviors aren't</u>)<br>
+
#No modification is on the locked behaviors of base class (<u>related open behaviors aren't</u>)<br>  
 
#Modifications are on subclass<br>
 
#Modifications are on subclass<br>
  
=== Specification: ===
+
=== Specification: ===
  
*Description #1 and #2 means that base class must assure the accessibilities and inheritance
+
*Description #1 and #2 means that base class must assure the accessibilities and inheritance  
 
*Description #3 and #4 means that we have 2 methods for the base class extension:
 
*Description #3 and #4 means that we have 2 methods for the base class extension:
  
#Allow the closed action might be overriden and reused on subclass
+
#Allow the closed action might be overriden and reused on subclass  
 
#Allow some actions open on base class so that those actions might happen inside the closed action
 
#Allow some actions open on base class so that those actions might happen inside the closed action
  
=== Example:<br> ===
+
=== Application:<br> ===
 +
 
 +
Having been applied spreadly in new Report engine, especially when we have been brought highest priorities onto <u>'''close inheriting'''</u> and '''<u>reusage</u>'''<br>
 +
 
 +
=== Example:<br> ===
 +
 
 +
DiagramInfo as the base:<br>
 +
 
 +
<source lang="csharp">/// <summary>
 +
      /// Set predefined criteria due to chart's specific characteristic
 +
      /// <param name="diagram"></param>
 +
      /// </summary>
 +
      protected virtual void SetPredefinedCriteria(Diagram diagram) { /* No operation performed */ }
 +
 
 +
      /// <summary>
 +
      /// Set predefined layout due to chart's specific characteristic
 +
      /// <param name="diagram"></param>
 +
      /// </summary>
 +
      protected virtual void SetPredefinedLayout(Diagram diagram) { /* No operation performed */ }
 +
 
 +
      /// <summary>
 +
      /// Set style sheet with corresponding properties
 +
      /// </summary>
 +
      /// <param name="diagram"></param>
 +
      protected virtual void SetStyleSheet(Diagram diagram)
 +
      {
 +
        ChartLayoutInfo.SetStyleSheet(diagram.Style ?? StyleSheet.GetDefault());
 +
      }
 +
 
 +
      /// <summary>
 +
      /// Set hidden filters
 +
      /// </summary>
 +
      /// <param name="diagram"></param>
 +
      protected virtual void SetHiddenFilters(Diagram diagram) { /* No operation performed */ }
 +
 
 +
      /// <summary>
 +
      /// Copy some specific static data from a <see cref="Diagram"/> to <see cref="DiagramInfo"/>,
 +
      /// appropriete to type of diagram. Basic action is to set <see cref="StyleSheet" /> of <see cref="ChartLayoutInfo"/>
 +
      /// </summary>
 +
      /// <param name="diagram">Diagram owning data to be copied</param>
 +
      public virtual void CopyStaticDataFrom(Diagram diagram)
 +
      {
 +
        if(diagram.Type != DiagramType)
 +
            throw new ArgumentException("Diagram must be {0} type", DiagramType.ToString());
 +
 
 +
        SetStyleSheet(diagram);
 +
        SetPredefinedCriteria(diagram);
 +
        SetPredefinedLayout(diagram);
 +
        SetHiddenFilters(diagram);
 +
      }</source>
 +
 
 +
CrossDiagramInfo as a subclass:
 +
 
 +
<source lang="csharp">/// <summary>
 +
      /// Set hidden filters
 +
      /// </summary>
 +
      /// <param name="diagram"></param>
 +
      protected override void SetHiddenFilters(Diagram diagram)
 +
      {
 +
        // Appy hidden filters
 +
        var filter = HiddenFilter.GetHiddenFilter(DataAccess.AccessFactory.CurrentUser, diagram);
 +
        if (filter != null)
 +
            Filters = (Filters.Union(new[] {new FilterInfo(diagram.DataCacheSpecification, filter.Filter)})).ToArray();
 +
      }</source>
 +
 
 +
TrackingDiagramInfo as a subclass:
 +
 
 +
<source lang="csharp">/// <summary>
 +
      /// Set predefined criterias, this includes some accessory conditions
 +
      /// </summary>
 +
      protected override void SetPredefinedCriteria(Diagram diagram)
 +
      {
 +
        // TODO : Implement further features like:
 +
        // - Generated automatically as default on loading viewer page
 +
        // - Hide criteria panel right as default on loading viewer page
 +
        if (!SelfDefinedCriterias.ContainsKey(SelfDefinedCriteriaType.Tracking_AutoGenerated))
 +
            SelfDefinedCriterias.Add(SelfDefinedCriteriaType.Tracking_AutoGenerated, false);
 +
        else
 +
            SelfDefinedCriterias[SelfDefinedCriteriaType.Tracking_AutoGenerated] = false;
 +
 
 +
        if (!SelfDefinedCriterias.ContainsKey(SelfDefinedCriteriaType.Tracking_CriteriaHidden))
 +
            SelfDefinedCriterias.Add(SelfDefinedCriteriaType.Tracking_CriteriaHidden, false);
 +
        else
 +
            SelfDefinedCriterias[SelfDefinedCriteriaType.Tracking_CriteriaHidden] = false;
 +
      }
 +
 
 +
      /// <summary>
 +
      /// Set features of predefined layouts
 +
      /// </summary>
 +
      protected override void SetPredefinedLayout(Diagram diagram)
 +
      {
 +
        if (ChartLayoutInfo != null)
 +
        {
 +
            // Tracking always excluded total column calculation. This added here as the only one place. DO NOT CHANGE, Dude !
 +
            if (!ChartLayoutInfo.SelfDefinedLayouts.ContainsKey(SelfDefinedLayoutType.Tracking_TotalCalculationExcluded))
 +
              ChartLayoutInfo.SelfDefinedLayouts.Add(SelfDefinedLayoutType.Tracking_TotalCalculationExcluded, true);
 +
            else
 +
              ChartLayoutInfo.SelfDefinedLayouts[SelfDefinedLayoutType.Tracking_TotalCalculationExcluded] = true;
 +
        }
 +
      }
 +
 
 +
      /// <summary>
 +
      /// Set style sheet with FreeLabelCollection of BarLine chart
 +
      /// </summary>
 +
      protected override void SetStyleSheet(Diagram diagram)
 +
      {
 +
        base.SetStyleSheet(diagram);
 +
 
 +
        if (FreeLabelHandler != null)
 +
        {
 +
            FreeLabelHandler.Transform(null);
 +
            ChartLayoutInfo.StyleSheet.BarLineChartStyle.FreeLabelCollections = FreeLabelHandler.FreeLabels;
 +
        }
 +
        // Specific : Stylesheet - Free labels
 +
        FreeLabelHandler =
 +
            new TrackingDiagramFreeLabelHandler(this, diagram, ChartLayoutInfo.StyleSheet.BarLineChartStyle.FreeLabelCollections);
 +
      }
 +
 
 +
      /// <summary>
 +
      /// Copy some specific static data from a <see cref="Diagram"/> to <see cref="TrackingDiagramInfo"/>, appropriete to type of diagram
 +
      /// </summary>
 +
      /// <param name="diagram">Diagram owning data to be copied</param>
 +
      public override void CopyStaticDataFrom(Diagram diagram)
 +
      {
 +
        base.CopyStaticDataFrom(diagram);
 +
       
 +
        // Static chart title
 +
        ChartTitle = diagram.ChartTitle;
 +
      }</source>
 +
 
 +
== Don't Repeat Yourself Principle (DRY)<br> ==
  
 
<br>
 
<br>
 +
 +
== Single Responsibility Principle (SRP)<br> ==
 +
 +
<br>
 +
 +
== Liskov Substitution Principle (LSP)<br> ==

Revision as of 08:36, 22 April 2010

Open & Closed Principle (OCP)

Introduction:

As the name itself, it's a principle instruct us how to close the main base behaviors of the original classes by preventing almost direct modifications, also to open existing classes to extend or make some changes by subclass and override on them.

Description:

  1. Class can be subclassed for extension
  2. Class' behaviors can be overriden
  3. No modification is on the locked behaviors of base class (related open behaviors aren't)
  4. Modifications are on subclass

Specification:

  • Description #1 and #2 means that base class must assure the accessibilities and inheritance
  • Description #3 and #4 means that we have 2 methods for the base class extension:
  1. Allow the closed action might be overriden and reused on subclass
  2. Allow some actions open on base class so that those actions might happen inside the closed action

Application:

Having been applied spreadly in new Report engine, especially when we have been brought highest priorities onto close inheriting and reusage

Example:

DiagramInfo as the base:

/// <summary>
      /// Set predefined criteria due to chart's specific characteristic
      /// <param name="diagram"></param>
      /// </summary>
      protected virtual void SetPredefinedCriteria(Diagram diagram) { /* No operation performed */ }

      /// <summary>
      /// Set predefined layout due to chart's specific characteristic
      /// <param name="diagram"></param>
      /// </summary>
      protected virtual void SetPredefinedLayout(Diagram diagram) { /* No operation performed */ }

      /// <summary>
      /// Set style sheet with corresponding properties
      /// </summary>
      /// <param name="diagram"></param>
      protected virtual void SetStyleSheet(Diagram diagram)
      {
         ChartLayoutInfo.SetStyleSheet(diagram.Style ?? StyleSheet.GetDefault());
      }

      /// <summary>
      /// Set hidden filters
      /// </summary>
      /// <param name="diagram"></param>
      protected virtual void SetHiddenFilters(Diagram diagram) { /* No operation performed */ }

      /// <summary>
      /// Copy some specific static data from a <see cref="Diagram"/> to <see cref="DiagramInfo"/>, 
      /// appropriete to type of diagram. Basic action is to set <see cref="StyleSheet" /> of <see cref="ChartLayoutInfo"/>
      /// </summary>
      /// <param name="diagram">Diagram owning data to be copied</param>
      public virtual void CopyStaticDataFrom(Diagram diagram)
      {
         if(diagram.Type != DiagramType)
            throw new ArgumentException("Diagram must be {0} type", DiagramType.ToString());

         SetStyleSheet(diagram);
         SetPredefinedCriteria(diagram);
         SetPredefinedLayout(diagram);
         SetHiddenFilters(diagram);
      }

CrossDiagramInfo as a subclass:

/// <summary>
      /// Set hidden filters
      /// </summary>
      /// <param name="diagram"></param>
      protected override void SetHiddenFilters(Diagram diagram)
      {
         // Appy hidden filters
         var filter = HiddenFilter.GetHiddenFilter(DataAccess.AccessFactory.CurrentUser, diagram);
         if (filter != null)
            Filters = (Filters.Union(new[] {new FilterInfo(diagram.DataCacheSpecification, filter.Filter)})).ToArray();
      }

TrackingDiagramInfo as a subclass:

/// <summary>
      /// Set predefined criterias, this includes some accessory conditions
      /// </summary>
      protected override void SetPredefinedCriteria(Diagram diagram)
      {
         // TODO : Implement further features like:
         // - Generated automatically as default on loading viewer page
         // - Hide criteria panel right as default on loading viewer page
         if (!SelfDefinedCriterias.ContainsKey(SelfDefinedCriteriaType.Tracking_AutoGenerated))
            SelfDefinedCriterias.Add(SelfDefinedCriteriaType.Tracking_AutoGenerated, false);
         else
            SelfDefinedCriterias[SelfDefinedCriteriaType.Tracking_AutoGenerated] = false;

         if (!SelfDefinedCriterias.ContainsKey(SelfDefinedCriteriaType.Tracking_CriteriaHidden))
            SelfDefinedCriterias.Add(SelfDefinedCriteriaType.Tracking_CriteriaHidden, false);
         else
            SelfDefinedCriterias[SelfDefinedCriteriaType.Tracking_CriteriaHidden] = false;
      }

      /// <summary>
      /// Set features of predefined layouts
      /// </summary>
      protected override void SetPredefinedLayout(Diagram diagram)
      {
         if (ChartLayoutInfo != null)
         {
            // Tracking always excluded total column calculation. This added here as the only one place. DO NOT CHANGE, Dude !
            if (!ChartLayoutInfo.SelfDefinedLayouts.ContainsKey(SelfDefinedLayoutType.Tracking_TotalCalculationExcluded))
               ChartLayoutInfo.SelfDefinedLayouts.Add(SelfDefinedLayoutType.Tracking_TotalCalculationExcluded, true);
            else
               ChartLayoutInfo.SelfDefinedLayouts[SelfDefinedLayoutType.Tracking_TotalCalculationExcluded] = true;
         }
      }

      /// <summary>
      /// Set style sheet with FreeLabelCollection of BarLine chart
      /// </summary>
      protected override void SetStyleSheet(Diagram diagram)
      {
         base.SetStyleSheet(diagram);

         if (FreeLabelHandler != null)
         {
            FreeLabelHandler.Transform(null);
            ChartLayoutInfo.StyleSheet.BarLineChartStyle.FreeLabelCollections = FreeLabelHandler.FreeLabels;
         }
         // Specific : Stylesheet - Free labels
         FreeLabelHandler =
            new TrackingDiagramFreeLabelHandler(this, diagram, ChartLayoutInfo.StyleSheet.BarLineChartStyle.FreeLabelCollections);
      }

      /// <summary>
      /// Copy some specific static data from a <see cref="Diagram"/> to <see cref="TrackingDiagramInfo"/>, appropriete to type of diagram
      /// </summary>
      /// <param name="diagram">Diagram owning data to be copied</param>
      public override void CopyStaticDataFrom(Diagram diagram)
      {
         base.CopyStaticDataFrom(diagram);
         
         // Static chart title
         ChartTitle = diagram.ChartTitle;
      }

Don't Repeat Yourself Principle (DRY)


Single Responsibility Principle (SRP)


Liskov Substitution Principle (LSP)