Шаблоны проектирования 2

Download Шаблоны проектирования 2

Post on 28-May-2015

1.707 views

Category:

Technology

3 download

Embed Size (px)

DESCRIPTION

Лекция Михаила Гуренкова по шаблонам проектирования в рамках курса по ООП. Каф. Вычислительная математика и программирование, МАИ.

TRANSCRIPT

  • 1.

2.

  • Factory ( , )
  • Abstract Factory( )
  • Singleton ( )
  • Lazy Load ( )

3. Faade() 4. Faade()

  • class TreeNode
  • {
  • }
  • class SearchResult
  • {
  • }
  • static class ACAlgoritm
  • {
  • public static TreeNode BuildTree(string[] a_Keywords)
  • {
  • }
  • public static SearchResult[] FindAll(TreeNode a_Root, string a_Text)
  • {
  • }
  • }
  • class SearchAlgoritm
  • {
  • TreeNode m_Root;
  • public SearchAlgoritm(string[] a_Keywords)
  • {
  • m_Root = ACAlgoritm.BuildTree(a_Keywords);
  • }
  • public SearchResult[] FindAll(string a_Text)
  • {
  • return ACAlgoritm.FindAll(m_Root, a_Text);
  • }
  • public static SearchResult[] FindAll(
  • string[] a_Keywords, string a_Text)
  • {
  • return ACAlgoritm.FindAll(
  • ACAlgoritm.BuildTree(a_Keywords),a_Text);
  • }
  • }

5. Faade()

  • ,
  • ( )
  • API , API

6. Composite ( ) 7. Composite ( )

  • select*fromCompanies
  • where(IsClient = 1orIsFormerClient = 1)andIsDeleted = 0andid_Object = 103

8. Composite ( )

  • IsClient = 1orIsFormerClient = 1)andIsDeleted = 0andid_Object = 103

9. Composite ( )

  • interface IFilter
  • {
  • string GetSql();
  • }
  • interface ICompositeFilter : IFilter
  • {
  • void AddFilter(IFilter a_Filter);
  • }

10. Composite ( )

  • class ScalarFilter : IFilter
  • {
  • string m_Sql;
  • public ScalarFilter(string a_Field, string a_Relation, object a_Value)
  • {
  • m_Sql = string.Format("{0} {1} {2}", a_Field, a_Relation, a_Value);
  • }
  • public ScalarFilter(string a_Field, object a_Value)
  • : this (a_Field, "=", a_Value) {}
  • public string GetSql()
  • {
  • return m_Sql;
  • }
  • }

11. Composite ( )

  • class AndFilter : ICompositeFilter
  • {
  • List m_Filters = new List();
  • public void AddFilter(IFilter a_Filter)
  • {
  • m_Filters.Add(a_Filter);
  • }
  • public string GetSql()
  • {
  • string result = string.Empty;
  • foreach (IFilter filter in m_Filters)
  • {
  • if (result != "")
  • result += " and ";
  • result += filter.GetSql();
  • }
  • return result;
  • }
  • }

12. Composite ( )

  • protected IFilter GetFilterInternal()
  • {
  • XAndFilter result = new XAndFilter();
  • if (ObjectsItemSelection.HasSelectedItems)
  • {
  • XOrFilter objectFilter = new XOrFilter();
  • foreach (XObject obj in ObjectsItemSelection.SelectedItems)
  • objectFilter.Add(new XScalarFilter("id_Object", obj.Id));
  • result.Add(objectFilter);
  • }
  • if (OperatorCheckBox.Checked || ClientCheckBox.Checked || FormerClientCheckBox.Checked)
  • {
  • XAndFilter categoryFilter = new XAndFilter();
  • if (OperatorCheckBox.Checked)
  • categoryFilter.Add(new XScalarFilter("IsOperator", 1));
  • if (ClientCheckBox.Checked)
  • categoryFilter.Add(new XScalarFilter("IsClient", 1));
  • result.Add(categoryFilter);
  • }
  • if (HoldingComboBox.Text.Trim() != "")
  • {
  • result.Add(DB.Companies.HoldingFilter(HoldingComboBox.Text.Trim()));
  • }
  • return result;
  • }

13. Composite ( )

  • !

14. !

  • public class XScalarFilter : IFilter
  • {
  • string m_Command;
  • IDataParameter m_Parameter;
  • public XScalarFilter(string a_Field, string a_Relation, object a_Value)
  • {
  • int counter = XFilter.Counter;
  • m_Command = string.Format("{0} {1} @p{2}", a_Field, a_Relation, counter);
  • m_Parameter = new SqlParameter();
  • m_Parameter.Value = a_Value;
  • m_Parameter.ParameterName = "@p" + counter;
  • }
  • public XScalarFilter(string a_Field, object a_Value)
  • : this(a_Field, "=", a_Value) {}
  • public string SqlText
  • {
  • get { return m_Command; }
  • }
  • public IList Parameters
  • {
  • get { return new IDataParameter[] { m_Parameter }; }
  • }
  • }

15. 16. Command ( )

  • interface ICommand
  • {
  • void Run();
  • }
  • interface ICommand
  • {
  • void Do();
  • void UnDo();
  • }
  • foreach(ICommand command in GetCommands())
  • {
  • command.Do();
  • }

17. Command ( )

  • select ContractNumber from CompanyClients where id_Company = @id_Company
  • select FullName from Companies where id = @id_Company
  • select FullName from Companies where id = @id_Company
  • select @DateFrom
  • select ...

18. Command ( ) 19.

  • [TemplateItem(TemplateKind.ExcelSpreadsheet, "Cell")]
  • public class XCellTemplateItem : XTemplateItem
  • {
  • public override void Apply()
  • {
  • XExcelWorkDesk wd = (XExcelWorkDesk)WorkDesk;
  • }
  • }
  • [TemplateItem(TemplateKind.ExcelSpreadsheet, "Table")]
  • class XTableTemplateItem : XTemplateItem
  • {
  • }

20.

  • Type FindTypeByName(string a_Name)
  • {
  • foreach (Type type in GetType().Assembly.GetTypes())
  • {
  • if (!type.IsSubclassOf(typeof(XTemplateItem)))
  • {
  • continue;
  • }
  • if (!type.IsDefined(typeof(TemplateItemAttribute), false))
  • {
  • continue;
  • }
  • TemplateItemAttribute attr = (TemplateItemAttribute)type.GetCustomAttributes(typeof(TemplateItemAttribute), false)[0];
  • if (attr.TemplateKind == m_Template.TemplateKind && attr.Name == a_Name)
  • {
  • return type;
  • }
  • }
  • return null;
  • }

21. Template Method( )

  • abstract class Game
  • {
  • protected int playersCount;
  • protectedabstract void initializeGame();
  • protectedabstract void makePlay(int player);
  • protectedabstract bool endOfGame();
  • protectedabstract void printWinner();
  • /* A template method : */
  • publicvoid playOneGame(int playersCount) {
  • this.playersCount = playersCount;
  • initializeGame();
  • int j = 0;
  • while (!endOfGame()) {
  • makePlay(j);
  • j = (j + 1) % playersCount;
  • }
  • printWinner();
  • }
  • }

22. Template Method( )

  • abstract public class XParser
  • {
  • public XCallInformation ParseAndProcess(XBillingString a_BillingString)
  • {
    • a_BillingString.SetParser(this);
    • XCallInformation result = DoParse(a_BillingString);
    • if (result == null)
    • {
    • XBilling.RegisterError(a_BillingString);
    • }
    • return result;
  • }
  • public XCallInformation Parse(XBillingString a_BillingString)
  • {
    • a_BillingString.SetParser(this);
    • return DoParse(a_BillingString);
    • }
  • }

23.