IEG3080 Tutorial 8 Prepared by KK
Outline Design Patterns –Creational Patterns –Structural Patterns –Behavioral Patterns Assignment 4
Design Patterns CreationalStructuralBehavioral Abstract Factory Builder Factory Method Prototype Singleton Adapter Bridge Composite Decorator Façade Flyweight Proxy Chain of Resp. Command Interpreter Iterator Mediator Memento Observer State Strategy Template Visitor
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 Adapter Client Target Source
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
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
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
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
Design Patterns – Adapter
Design Patterns – Bridge –Bridge Decouple an abstraction from its implementation so that the two can vary independently. Task -No implementation in Abstraction Abstraction Implementer
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
Design Patterns – Bridge abstract class Implementor { protected String m_name; public Implementor() { } public abstract void PrintDeadline(); } Implementer
Design Patterns – Bridge class Assignment3 : Implementor { public Assignment3(){ m_name = "Assignment3"; } public override void PrintDeadline() { Console.WriteLine("{0} Deadline : ", m_name); } class Assignment4 : Implementor { public Assignment4(){ m_name = "Assignment4"; } public override void PrintDeadline() { Console.WriteLine("{0} Deadline : ", m_name); } Implementer
Design Patterns – Bridge
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 Component Leaf 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 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 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 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 Design Patterns – Composite
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
Design Patterns – Decorator abstract class Component { public abstract void Print(); } class ConcreteComponent : Component { public override void Print() { Console.WriteLine("Letter body (Component)!"); } Component
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
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
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
Design Patterns – Decorator
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
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
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
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
Design Patterns – Façade
Design Patterns References –Gamma, Erich et al, “Design Patterns: Elements of Reusable Object-Oriented Software,” Addison-Wesley. –Design Patterns with C# sample code:
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.
Assignment 4
1 mark will be deducted if the api key is not filled in
Assignment 4 1 mark will be deducted if not fullfill this requirement
Assignment 4
eWSold.ziphttp://course.ie.cuhk.edu.hk/~ieg3080a/assignment/Googl eWSold.zip eWS.ziphttp://course.ie.cuhk.edu.hk/~ieg3080a/assignment/Googl eWS.zip 2 marks will be deducted for late submission