1 / 48

IEG3080 Tutorial 8

IEG3080 Tutorial 8. Prepared by KK. Outline. Design Patterns Creational Patterns Structural Patterns Behavioral Patterns Assignment 4. Design Patterns. Design Patterns – Adapter. Adapter Convert the interface of a class into another interface clients expect. Task

aurek
Download Presentation

IEG3080 Tutorial 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IEG3080 Tutorial 8 Prepared by KK

  2. Outline • Design Patterns • Creational Patterns • Structural Patterns • Behavioral Patterns • Assignment 4

  3. Design Patterns

  4. Design Patterns – Adapter • Adapter • Convert the interface of a class into another interface clients expect. Task -Keep Target unchanged -Adapt Source interface to Target interface Client Target Adapter Source

  5. Design Patterns – Adapter class Course { protected String code; protected String title; protected int num; public Course(String courseCode) { this.code = courseCode; } public virtual void Display() { Console.WriteLine("Course name: {0}", this.code); } } Target

  6. Design Patterns – Adapter class CourseDataBank { public String CourseTitle(String courseCode){ String title = ""; switch(courseCode.ToLower()){ case "ieg3080a" : title = "Information And Software Engineering Practice"; break; case "ieg3080b" : title = "Information And Software Engineering Practice"; break; } return title; } public int CourseStudentNum(String courseCode){ int num = 0; switch(courseCode.ToLower()){ case "ieg3080a" : num = 37;break; case "ieg3080b" : num = 97;break; } return num; } } Source

  7. Design Patterns – Adapter class Program { static void Main(string[] args) { Course course = new Course("IEG3080a"); course.Display(); Course adapter = new Adapter("IEG3080a"); adapter.Display(); Console.Read(); } } Client

  8. Design Patterns – Adapter class Adapter : Course { private CourseDataBank bank; public Adapter(String courseCode):base(courseCode) { } public override void Display() { bank = new CourseDataBank(); this.title = bank.CourseTitle(this.code); this.num = bank.CourseStudentNum(this.code); Console.WriteLine("Course name: {0}", this.code); Console.WriteLine("Course Title: {0}", this.title); Console.WriteLine("Num of Student: {0}", this.num); } } Adapter

  9. Design Patterns – Adapter

  10. Design Patterns – Adapter

  11. Design Patterns – Bridge • Bridge • Decouple an abstraction from its implementation so that the two can vary independently. Task -No implementation in Abstraction Abstraction Implementer

  12. Design Patterns – Bridge class Abstraction { private Implementor m_Data; public Abstraction() { } public Implementor Implementor { set { m_Data = value; } get { return m_Data; } } public virtual void PrintDeadline() { m_Data.PrintDeadline(); } } class Assignment : Abstraction { } Abstraction

  13. Design Patterns – Bridge abstract class Implementor { protected String m_name; public Implementor() { } public abstract void PrintDeadline(); } Implementer

  14. Design Patterns – Bridge class Assignment3 : Implementor { public Assignment3(){ m_name = "Assignment3"; } public override void PrintDeadline() { Console.WriteLine("{0} Deadline : 19 - 3 -2007", m_name); } } class Assignment4 : Implementor { public Assignment4(){ m_name = "Assignment4"; } public override void PrintDeadline() { Console.WriteLine("{0} Deadline : 20 - 4 -2007", m_name); } } Implementer

  15. Design Patterns – Bridge

  16. Design Patterns – Bridge

  17. Design Patterns – Composite • Composite • Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Task -batch operation Leaf Component Composite

  18. Design Patterns – Composite class Program{ static void Main(string[] args){ Composite patterns = new Composite("Design Patterns"); Composite creational = new Composite("Creational Patterns"); creational.Add(new Leaf("Abstract Factory")); creational.Add(new Leaf("Builder")); creational.Add(new Leaf("Factory Method")); creational.Add(new Leaf("Prototype")); creational.Add(new Leaf("Singleton")); patterns.Add(creational); Composite structural = new Composite("Structural Patterns"); structural.Add(new Leaf("Adapter")); structural.Add(new Leaf("Bridge")); structural.Add(new Leaf("Composite")); structural.Add(new Leaf("Decorator")); structural.Add(new Leaf("Facade")); structural.Add(new Leaf("Flyweight")); structural.Add(new Leaf("Proxy")); patterns.Add(structural); Composite behavioral = new Composite("Behavioral Patterns"); behavioral.Add(new Leaf("Chain of Resp.")); behavioral.Add(new Leaf("Command")); behavioral.Add(new Leaf("Interpreter")); behavioral.Add(new Leaf("Iterator")); behavioral.Add(new Leaf("Mediator")); behavioral.Add(new Leaf("Memento")); behavioral.Add(new Leaf("Observer")); behavioral.Add(new Leaf("State")); behavioral.Add(new Leaf("Strategy")); behavioral.Add(new Leaf("Template Method")); behavioral.Add(new Leaf("Visitor")); patterns.Add(behavioral); patterns.Display(0); Console.Read(); } } Client

  19. Design Patterns – Composite abstract class Component { protected string name; // Constructor public Component(string name) { this.name = name; } public abstract void Add(Component c); public abstract void Remove(Component c); public abstract void Display(int depth); } Component

  20. Design Patterns – Composite class Composite : Component { private ArrayList children = new ArrayList(); public Composite(string name) : base(name) { } public override void Add(Component component){ children.Add(component); } public override void Remove(Component component){ children.Remove(component); } public override void Display(int depth) { Console.WriteLine(new String('-', depth) + name); foreach (Component component in children) { component.Display(depth + 1); } } } Composite

  21. Design Patterns – Composite class Leaf : Component { public Leaf(string name) : base(name) { } public override void Add(Component c) { Console.WriteLine("Invalid"); } public override void Remove(Component c) { Console.WriteLine("Invalid"); } public override void Display(int depth) { Console.WriteLine(new String('-', depth) + name); } } Leaf

  22. Design Patterns – Composite

  23. Design Patterns – Composite

  24. Design Patterns – Decorator • Decorator • Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Task -Template Component Decorator

  25. Design Patterns – Decorator abstract class Component { public abstract void Print(); } class ConcreteComponent : Component { public override void Print() { Console.WriteLine("Letter body (Component)!"); } } Component

  26. Design Patterns – Decorator class Program { static void Main(string[] args) { // Create ConcreteComponent and two Decorators ConcreteComponent letter = new ConcreteComponent(); ConcreteDecoratorA letter1 = new ConcreteDecoratorA(); ConcreteDecoratorB letter2 = new ConcreteDecoratorB(); // Link decorators letter1.SetComponent(letter); letter2.SetComponent(letter1); letter2.Print(); // Wait for user Console.Read(); } } Client

  27. Design Patterns – Decorator abstract class Decorator : Component { protected Component component; public void SetComponent(Component component) { this.component = component; } public override void Print() { if (component != null) { component.Print(); } } } Decorator

  28. Design Patterns – Decorator class ConcreteDecoratorA : Decorator { private string addedState; public override void Print() { Console.WriteLine("Dear Students,"); base.Print(); addedState = "New State"; Console.WriteLine("Thanks!"); } } class ConcreteDecoratorB : Decorator { public override void Print() { Console.WriteLine("------------------------------------"); base.Print(); AddedBehavior(); Console.WriteLine("------------------------------------"); } void AddedBehavior() { Console.WriteLine("Regards"); Console.WriteLine("KK"); } } Decorator

  29. Design Patterns – Decorator

  30. Design Patterns – Decorator

  31. Design Patterns – Façade • Façade • Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. Task -Linking independent classes together Facade Subsystem

  32. Design Patterns – Façade class Program { static void Main(string[] args) { Facade facade = new Facade(); facade.MethodA(); facade.MethodB(); // Wait for user Console.Read(); } } Client

  33. Design Patterns – Façade class SubSystem1 { public void Method1() { Console.WriteLine(" SubSystem1 Method"); } } class SubSystem2 { public void Method2() { Console.WriteLine(" SubSystem2 Method"); } } class SubSystem3 { public void Method3() { Console.WriteLine(" SubSystem3 Method"); } } Subsystem

  34. Design Patterns – Façade class Facade { SubSystem1 sys1; SubSystem2 sys2; SubSystem3 sys3; public Facade() { sys1 = new SubSystem1(); sys2 = new SubSystem2(); sys3 = new SubSystem3(); } public void MethodA() { Console.WriteLine("\nMethodA() ---- "); sys1.Method1(); sys2.Method2(); } public void MethodB() { Console.WriteLine("\nMethodB() ---- "); sys2.Method2(); sys3.Method3(); } } Facade

  35. Design Patterns – Façade

  36. Design Patterns – Façade

  37. Design Patterns • References • Gamma, Erich et al, “Design Patterns: Elements of Reusable Object-Oriented Software,” Addison-Wesley. • Design Patterns with C# sample code: http://www.dofactory.com/Patterns/Patterns.aspx

  38. Assignment 4

  39. Assignment 4

  40. Assignment 4

  41. Assignment 4 • Create a new project, such as a Windows Application • Run wsdl.exe GoogleSearch.wsdl to generate GoogleSearchService.cs, the C# client class. • Add a reference to the System.Web.Services DLL to your project. • Write your code to call GoogleSearchService.

  42. Assignment 4

  43. Assignment 4

  44. Assignment 4 • 1 mark will be deducted if the api key is not filled in

  45. Assignment 4 • 1 mark will be deducted if not fullfill this requirement

  46. Assignment 4

  47. Assignment 4

  48. Assignment 4 • http://code.google.com/apis/soapsearch/ • http://course.ie.cuhk.edu.hk/~ieg3080a/assignment/GoogleWSold.zip • http://course.ie.cuhk.edu.hk/~ieg3080a/assignment/GoogleWS.zip • 2 marks will be deducted for late submission

More Related