Difference between revisions of "OOP Analysis Design Mandatory Principles"
Line 162: | Line 162: | ||
== Don't Repeat Yourself Principle (DRY)<br> == | == Don't Repeat Yourself Principle (DRY)<br> == | ||
+ | |||
+ | === Example<br> === | ||
Lets come back to the example of OCP, in TrackingDiagramInfo methods SetPredefinedCriteria, SetPredefinedLayout we saw the code that duplicated the same feature in 3 times, though they have different logic. Now I want to make sure that I will implement the feature one single time in the only single place. By this way, I can <u>reserve for further case in which this setting attempt may happen</u>. Where I oughta put this ?<br> | Lets come back to the example of OCP, in TrackingDiagramInfo methods SetPredefinedCriteria, SetPredefinedLayout we saw the code that duplicated the same feature in 3 times, though they have different logic. Now I want to make sure that I will implement the feature one single time in the only single place. By this way, I can <u>reserve for further case in which this setting attempt may happen</u>. Where I oughta put this ?<br> | ||
− | In TrackingDiagramInfo ? No, as it should have been taken inside the root methods of DiagramInfo. So it must be in a helper file of DiagramInfo. I named it as CommonHelper. | + | In TrackingDiagramInfo ? No, as it should have been taken inside the root methods of DiagramInfo. So it must be in a helper file of DiagramInfo. I named it as CommonHelper. <br> |
+ | |||
+ | ==== SetPredefinedCriteria<br> ==== | ||
<source lang="csharp">/// <summary> | <source lang="csharp">/// <summary> | ||
Line 177: | Line 181: | ||
SelfDefinedCriterias.TrySet(SelfDefinedCriteriaType.Tracking_AutoGenerated, false); | SelfDefinedCriterias.TrySet(SelfDefinedCriteriaType.Tracking_AutoGenerated, false); | ||
SelfDefinedCriterias.TrySet(SelfDefinedCriteriaType.Tracking_CriteriaHidden, false); | SelfDefinedCriterias.TrySet(SelfDefinedCriteriaType.Tracking_CriteriaHidden, false); | ||
− | } | + | }</source><br> |
+ | |||
+ | ==== SetPredefinedLayout<br> ==== | ||
− | + | <source lang="csharp">/// <summary> | |
/// Set features of predefined layouts | /// Set features of predefined layouts | ||
/// </summary> | /// </summary> | ||
Line 189: | Line 195: | ||
ChartLayoutInfo.SelfDefinedLayouts.TrySet(SelfDefinedLayoutType.Tracking_TotalCalculationExcluded, true); | ChartLayoutInfo.SelfDefinedLayouts.TrySet(SelfDefinedLayoutType.Tracking_TotalCalculationExcluded, true); | ||
} | } | ||
− | }</source> | + | }</source><br> |
− | + | ==== SetPredefinedCriteriaExtension (in CommonHelper) ==== | |
<source lang="csharp">/// <summary> | <source lang="csharp">/// <summary> | ||
− | /// An extension class for <see cref=" | + | /// An extension class for <see cref="SelfDefinedCriteriaType"/> |
/// </summary> | /// </summary> | ||
− | public static class | + | public static class SelfDefinedCriteriaTypeExtension |
{ | { | ||
/// <summary> | /// <summary> | ||
− | /// Try to set a object-typed value with corresponding <see cref=" | + | /// Try to set a object-typed value with corresponding <see cref="SelfDefinedCriteriaType"/>-typed key no matter what any item owning this key has existed or not |
/// </summary> | /// </summary> | ||
/// <param name="dictionary">Dictionary has item whose key to be set value</param> | /// <param name="dictionary">Dictionary has item whose key to be set value</param> | ||
Line 205: | Line 211: | ||
/// <param name="value">Value of item</param> | /// <param name="value">Value of item</param> | ||
/// <returns>Dictionary has item whose value already set</returns> | /// <returns>Dictionary has item whose value already set</returns> | ||
− | public static Dictionary< | + | public static Dictionary<SelfDefinedCriteriaType, object> TrySet(this Dictionary<SelfDefinedCriteriaType, object> dictionary, SelfDefinedCriteriaType key, object value) |
{ | { | ||
if (!dictionary.ContainsKey(key)) | if (!dictionary.ContainsKey(key)) | ||
Line 213: | Line 219: | ||
return dictionary; | return dictionary; | ||
} | } | ||
− | } | + | }</source><br> |
− | + | ==== SetPredefinedLayout (in CommonHelper)<br> ==== | |
− | /// An extension class for <see cref=" | + | |
+ | <source lang="csharp">/// <summary> | ||
+ | /// An extension class for <see cref="SelfDefinedLayoutType"/> | ||
/// </summary> | /// </summary> | ||
− | public static class | + | public static class SelfDefinedLayoutTypeExtension |
{ | { | ||
/// <summary> | /// <summary> | ||
− | /// Try to set a object-typed value with corresponding <see cref=" | + | /// Try to set a object-typed value with corresponding <see cref="SelfDefinedLayoutType"/>-typed key no matter what any item owning this key has existed or not |
/// </summary> | /// </summary> | ||
/// <param name="dictionary">Dictionary has item whose key to be set value</param> | /// <param name="dictionary">Dictionary has item whose key to be set value</param> | ||
Line 227: | Line 235: | ||
/// <param name="value">Value of item</param> | /// <param name="value">Value of item</param> | ||
/// <returns>Dictionary has item whose value already set</returns> | /// <returns>Dictionary has item whose value already set</returns> | ||
− | public static Dictionary< | + | public static Dictionary<SelfDefinedLayoutType, object> TrySet(this Dictionary<SelfDefinedLayoutType, object> dictionary, SelfDefinedLayoutType key, object value) |
{ | { | ||
if (!dictionary.ContainsKey(key)) | if (!dictionary.ContainsKey(key)) | ||
Line 235: | Line 243: | ||
return dictionary; | return dictionary; | ||
} | } | ||
− | }</source | + | }</source> |
− | |||
− | |||
== Single Responsibility Principle (SRP)<br> == | == Single Responsibility Principle (SRP)<br> == |
Revision as of 09:24, 22 April 2010
Contents
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
- Class can be subclassed for extension
- Class' behaviors can be overriden
- No modification is on the locked behaviors of base class (related open behaviors aren't)
- 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:
- 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
Application
Having been applied spreadly in new Report engine, especially when we have been brought highest priorities onto close inheriting and reusage
Example
Structure
DiagramInfo as the base:
- CopyStaticDataFrom is wanted to be standard and unchangable in diagram generating logic: Validating DiagramType of input Diagram
- CopyStaticDataFrom is wanted to be extended in diagram generating logic in setting: Stylesheet, PredefinedCriterias, PredefinedLayouts, hidden filters
/// <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
Has Hidden filters needing specific action
/// <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
Has Stylesheet, PredefinedCriterias, PredefinedLayout needing specific actions
/// <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)
Example
Lets come back to the example of OCP, in TrackingDiagramInfo methods SetPredefinedCriteria, SetPredefinedLayout we saw the code that duplicated the same feature in 3 times, though they have different logic. Now I want to make sure that I will implement the feature one single time in the only single place. By this way, I can reserve for further case in which this setting attempt may happen. Where I oughta put this ?
In TrackingDiagramInfo ? No, as it should have been taken inside the root methods of DiagramInfo. So it must be in a helper file of DiagramInfo. I named it as CommonHelper.
SetPredefinedCriteria
/// <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
SelfDefinedCriterias.TrySet(SelfDefinedCriteriaType.Tracking_AutoGenerated, false);
SelfDefinedCriterias.TrySet(SelfDefinedCriteriaType.Tracking_CriteriaHidden, false);
}
SetPredefinedLayout
/// <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 !
ChartLayoutInfo.SelfDefinedLayouts.TrySet(SelfDefinedLayoutType.Tracking_TotalCalculationExcluded, true);
}
}
SetPredefinedCriteriaExtension (in CommonHelper)
/// <summary>
/// An extension class for <see cref="SelfDefinedCriteriaType"/>
/// </summary>
public static class SelfDefinedCriteriaTypeExtension
{
/// <summary>
/// Try to set a object-typed value with corresponding <see cref="SelfDefinedCriteriaType"/>-typed key no matter what any item owning this key has existed or not
/// </summary>
/// <param name="dictionary">Dictionary has item whose key to be set value</param>
/// <param name="key">Item key to be set</param>
/// <param name="value">Value of item</param>
/// <returns>Dictionary has item whose value already set</returns>
public static Dictionary<SelfDefinedCriteriaType, object> TrySet(this Dictionary<SelfDefinedCriteriaType, object> dictionary, SelfDefinedCriteriaType key, object value)
{
if (!dictionary.ContainsKey(key))
dictionary.Add(key, value);
else
dictionary[key] = value;
return dictionary;
}
}
SetPredefinedLayout (in CommonHelper)
/// <summary>
/// An extension class for <see cref="SelfDefinedLayoutType"/>
/// </summary>
public static class SelfDefinedLayoutTypeExtension
{
/// <summary>
/// Try to set a object-typed value with corresponding <see cref="SelfDefinedLayoutType"/>-typed key no matter what any item owning this key has existed or not
/// </summary>
/// <param name="dictionary">Dictionary has item whose key to be set value</param>
/// <param name="key">Item key to be set</param>
/// <param name="value">Value of item</param>
/// <returns>Dictionary has item whose value already set</returns>
public static Dictionary<SelfDefinedLayoutType, object> TrySet(this Dictionary<SelfDefinedLayoutType, object> dictionary, SelfDefinedLayoutType key, object value)
{
if (!dictionary.ContainsKey(key))
dictionary.Add(key, value);
else
dictionary[key] = value;
return dictionary;
}
}
Single Responsibility Principle (SRP)