Presentation is loading. Please wait.

Presentation is loading. Please wait.

IEG3080 Tutorial 7 Prepared by Ryan.

Similar presentations


Presentation on theme: "IEG3080 Tutorial 7 Prepared by Ryan."— Presentation transcript:

1 IEG3080 Tutorial 7 Prepared by Ryan

2 Outline Design Patterns Assignment 3 Creational Patterns
Structural Patterns Behavioral Patterns Assignment 3

3 Design Patterns What’s Design Patterns?
General recurring solutions to some common software design problems and to make the designs more flexible and reusable Descriptions of relationship and interaction between objects or classes that are customized to solve a general design problem in a particular context

4 Design Patterns Reuse Mechanisms in Design Patterns Class Inheritance
White-box reuse The internals of parent classes are often visible to the subclass Reuse by subclassing Object Composition Black-box reuse The internals of objects are encapsulated Reuse by assembling/composing objects

5 Design Patterns The principles of good object-oriented design
Program to an (abstract) interface, not an (concrete) implementation Reduce implementation dependencies By declaring variables to be instances of an abstract class (interface) Favor object composition over class inheritance (object composition) defined dynamically at run-time, (class inheritance) defined at compile-time Keep each class encapsulated and focused on one task

6 Design Patterns “Gang of Four” Design Patterns – total 23 patterns
Creational Patterns (5) The process of object creation Structural Patterns (7) The composition of objects or classes Behavioral Patterns (11) The way in which objects or classes interact and distribute responsibility

7 Design Patterns Creational Structural Behavioral 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

8 Design Patterns – Creational
Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes. class Shop { Product product; public Shop(AbstractFactory a) { p = a.CreateProduct1(); p = a.CreateProduct2(); } abstract class AbstractFactory { public abstract Product CreateProduct1(); public abstract Product CreateProduct2(); The “product” being created depends on the “factory”

9 Design Patterns – Creational
Builder Separate the construction of a complex object from its representation so that the same construction process can create different representations. class Shop { Product p; public Shop(Builder b) { b.CreateSubProduct1(); b.CreateSubProduct2(); p = b.GetProduct(); } abstract class Builder { public abstract void CreateSubProduct1(); public abstract void CreateSubProduct2(); public abstract Product GetProduct(); “Parts” of the complex object is encapsulated inside the builder The complex object is returned as a final step

10 Design Patterns – Creational
Factory Method Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. abstract class Shop { protected Product p; public Shop() { CreateProduct(); } public abstract void CreateProduct(); class Shop1 : Shop { public override void CreateProduct() { p = new Product1(); } class Shop2 : Shop { p = new Product2(); Subclasses define the object to be created

11 Design Patterns – Creational
Prototype Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype. class Shop { Product p, p2; public Shop() { p = new Product(); p2 = p.Clone(); } abstract class Prototype { public abstract Prototype Clone(); class Product : Prototype { public override Prototype Clone() { return (Prototype)this.MemberwiseClone(); A object is created by copying (clone)

12 Design Patterns – Creational
Singleton Ensure a class has only one instance and provide a global point of access to it. class Singleton {      private static Singleton instance;      protected Singleton() { }      public static Singleton Instance() {       if (instance == null)        {          instance = new Singleton();        }        return instance;      } } class Test { public static void Main() {            Singleton s1 = Singleton.Instance(); Private constructor

13 Design Patterns – Creational
Summary Abstract Factory Create a family of objects by a “factory object” Builder Create complex objects with different representations by a “builder object” Factory Method Provide an interface for objects creation, specify the objects to be created in the subclass Prototype Create new objects by copying Singleton Allow only one instance of a class

14 Design Patterns References
Gamma, Erich et al, “Design Patterns: Elements of Reusable Object-Oriented Software,” Addison-Wesley. Design Patterns with C# sample code:

15 Assignment 3 Use a type-safe container (Array/ArrayList) to store objects Implement IComparable interface to support built-in Sort method .NET Framework Class Library Interface IComparable { int CompareTo(object o); } Method “CompareTo()” return value Less than 0: this instance is less than object 0: this instance is equal to object Greater than 0: this instance is greater than object

16 Assignment 3 Sample Code // Type-safe ArrayList
// Implement IEnumerable interface class CustomerList : IEnumerable { ArrayList list; public CustomerList() { list = new ArrayList(); } public void Add(Customer c) { list.Add(c); public IEnumerator GetEnumerator() { return list.GetEnumerator(); public void Sort() { list.Sort(); // ArrayList built-in Sort() method // Implement IComparable interface class Customer : IComparable { string customer_name; public Customer(string name) { customer_name = name; } // Implement IComparable.CompareTo() public int CompareTo(object o) { Customer temp = (Customer)o; //Type casting to “Customer” return customer_name.CompareTo(temp.customer_name); // Other Methods ……

17 Assignment 3 Implement a GUI for Assignment 2 program
Menu Bar – “List” and “Home” buttons 1) Click “List” Show a SORTED list of customers in the panel (sorted by customer name)

18 Assignment 3 2) Click a customer name
Show the accounts belonging to the selected customer 3) Select and Right-click an account Show a menu with “Deposit” and “Withdraw” buttons 4) Click “Deposit”/”Withdraw” Pop up a window with a text box and a “Confirm” button 5) Input an amount in the text box and Click “Confirm” Close the window and Perform deposit/withdraw action Show the updated account summary 6) Click “Home” Return to the home page

19 Assignment 3 Drag and Drop Components to the Window Form
Some useful components MenuStrip ContextMenuStrip Shortcut menu when the user right-clicks the associated control TreeView TextBox Button TextBox MenuStrip Label ContextMenuStrip Button TreeView

20 Assignment 3 Programming with the components
Double-click the components, Click event handler method will be generated automatically More about TreeView Add a node in TreeView myTreeView.Nodes.Add(“myNode1”); Add a node associated with ContextMenuStrip TreeNode myNode = new TreeNode(); myNode.Text = “myNode1”; myNode.ContextMenuStrip = myContextMenuStrip; myTreeView.Nodes.Add(myNode); Add an event handler method for mouse right-click myTeeView.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(myHandler); private void myHandler(object sender, TreeNodeMouseClickEventArgs e) { if (e.Button == MouseButtons.Right) { /* Your code here. You can get the clicked Node by “e.Node” */ } }


Download ppt "IEG3080 Tutorial 7 Prepared by Ryan."

Similar presentations


Ads by Google