Case Study - PowerPoint PPT Presentation

1 / 75
About This Presentation
Title:

Case Study

Description:

A window has several windows and buttons. OO provides an elegant solution to GUI ... Naruto. Attack() Sasuke. Attack() Fighter. Attack() Fighter. Attack() 257 ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 76
Provided by: mch6
Category:
Tags: case | naruto | study

less

Transcript and Presenter's Notes

Title: Case Study


1
  • Case Study
  • GUI

2
Case study
  • GUI (Graphical User Interface) toolkit
  • A typical GUI has many different objects
  • Menu, window, button, scrollbar, . . .
  • Hierarchical (tree-like) structure
  • Like a file directory
  • A window has several windows and buttons
  • OO provides an elegant solution to GUI problems
  • Most successful example Apple Macintosh

3
How to draw GUI objects?
  • Put all concrete objects into a Glyph container
    ask each Glyph to draw itself

Glyph container
Glyph (System provided) Draw()
Glyph Draw()
Add
Glyph Draw()
Concrete Glyph (user provided) Draw()
Glyph Draw()
Glyph Draw()
4
How to draw GUI objects?
  • Glyph window new Button(),
  • new ScrollBar()
  • foreach (g in Window) g.Draw()

Glyph virtual void Draw()
Button void Draw()
ScrollBar void Draw()
Panel void Draw()
5
Upcasting and polymorphism
  • Create concrete objects
  • new Button(), new ScrollBar()
  • Add to an abstract container
  • Container only knows abstract base class Glyph
  • Upcasting
  • Button is upcasted to Glyph
  • Polymorphism
  • All concrete objects are treated uniformly (same)
    as Glyph
  • foreach (g in Window) g.Draw()

6
Composition
  • Is Window a kind of glyph?
  • Yes, window shares many common properties with
    buttons, scroll bars, panel, . . .
  • Is-a relationship inheritance

Glyph
is-a
Window
Button
ScrollBar
Panel
7
Composition
  • But Window can have some buttons, scroll bars,
    etc.
  • Has-a relationship composition
  • Window has a number of glyphs

has-a
Window
Glyph
is-a
Button
ScrollBar
Panel
8
Is-a and Has-a relationships
  • Is-a
  • Inheritance
  • Window is-a Glyph
  • class Window Glyph
  • . . .
  • Has-a
  • Composition
  • Window has-a few buttons, windows
  • class Window
  • Glyph g
  • . . .

9
Composite pattern
  • Combining is-a and has-a
  • class Window Glyph //inheritance
  • Glyph g //composition
  • . . .
  • So that we can have a window inside a window

Glyph
is-a
has-a
Button
ScrollBar
Panel
Window
10
An even more general form of composite pattern
  • Can support any complex structure imaginable
  • e.g. a Window inside a Button
  • Glyph has-a Glyph container
  • class Glyph
  • Glyph g
  • . . .

has-a
Glyph

is-a
Button
ScrollBar
Panel
Window
11
An even more general form of composition pattern
  • class Window Glyph
  • . . .
  • Window inherits the Glyph container from Glyph
  • Support Window inside a Button

has-a
Glyph

is-a
Button
ScrollBar
Panel
Window
12
Limitation of inheritance
  • How to change to a different Button at run time?

Glyph container
Glyph (System provided) Draw()
Glyph Draw()
Add
Glyph Draw()
Concrete Glyph (user provided) Draw()
Glyph Draw()
Glyph Draw()
13
Limitation of inheritance
  • Base classs Draw() is overrided by the subclass
  • The actual Draw() is done by the concrete
    subclass
  • To draw a different look-and-feel button?
  • Tedious, has to replace the old Button object in
    the container by a new one that has a different
    Draw()

14
Delegation
  • Outsource the drawing action to a drawing object
  • So that we can change the look-and-feel by
    changing the drawing object at run-time

Glyph container
Glyph DrawStyle obj Draw()
obj.Draw() ChangeStyle( DrawStyle newobj)
obj newobj
Add
Glyph Draw()
DrawStyle Draw()
Glyph Draw()
Glyph Draw()
15
The essential ideas of OO
  • INHERITANCE (is-a)
  • Similar types of objects that share the same
    interface
  • COMPOSITION (has-a)
  • An object that is made up from other objects
  • DELEGATION
  • To outsource work to another object
  • Delegation has the same syntax as composition

16
Design of Callback
  • What is callback?
  • The mechanism of sending messages between objects
  • e.g. a mouse button is clicked, how to send this
    mouse-up event message to a mouse-up handler?
  • Window Server
  • Contains GUI objects (Button, ScrollBar, )
  • If mouse is clicked, window server finds the
    clicked object in its container, and calls the
    objects OnClick() virtual function
  • You provide the implementation of OnClick()

17
Callback by inheritance
  • Build the MyWindow class inherited from Form
    (Form is like Glyph in prev example)
  • Override OnClick()
  • Add concrete MyWindow object to container as Form

Window Server
Container
Form virtual OnClick()
Form (Button) virtual OnClick()
Form (ScrollBar) virtual OnClick()
MyWindow override OnClick()
18
Get callback by inheritance
  • using System
  • using System.Windows.Forms
  • class MyWindow Form
  • static void Main()
  • Application.Run(new MyWindow())
  • //start a window and wait for input event
  • protected override void OnClick(EventArgs e)
  • Console.WriteLine(I(the Form) am clicked)
  • base.OnClick(e) //forward to parent (Form)
  • //in case there are more things to
    do

19
OO in one picture
www.vec.ca/images/Gift-Basket.JPG
20
OO is like a basket of fruits
  • The basket is a container of fruits
  • Important
  • You have just made an abstraction
  • Physically the container has different concrete
    objects (apples and oranges), but because of your
    abstraction, you have generalized them as fruits
  • You dont see apples, oranges, only fruits
  • Program to an interface, not implementation
  • You use the objects as fruits (interface), not
    apples and oranges (implementation)

21
Your abstract view
  • Program to an interface, not an implementation
  • Abstract and flexible programming

Container
Fruit Eat()
Fruit Eat()
22
Your abstract view
  • What if you eat a fruit?
  • Pick a fruit object from the container and eat it
  • fruit.Eat()
  • What happens next?
  • If fruit is an Apple, it tastes like an apple
  • If it is an Orange, it tastes like an orange
  • Your leave the actual implementation to the
    hidden concrete objects

23
The concrete view
  • What actually happens

Container
Fruit Eat()
Fruit Eat()
Fruit Eat()
Banana Eat()
Orange Eat()
Apple Eat()
Orange Eat()
Apple Eat()
24
Library application
Container
Lendable Borrow()
Lendable Borrow()
Journal Borrow()
DVD Borrow()
Book Borrow()
Lendable Borrow()
Lendable Borrow()
25
Bank application
Container
Account Deposit()
Account Deposit()
USD Deposit()
Current Deposit()
Saving Deposit()
Account Deposit()
Account Deposit()
26
Game application
  • Attack() is done by collision detection of
    rectangles

Container
Fighter Attack()
Fighter Attack()
Sasuke Attack()
Sakura Attack()
Naruto Attack()
Fighter Attack()
Fighter Attack()
27
Callback by delegation
  • Store a handler pointer into an object of type
    EventHandler
  • Add the EventHandler object to Form

Window server
Container
obj Handler()
EventHandler obj.Handler()
Form
Point to a handler function
28
Callback by delegation
  • class MyWindow Form
  • static void Main()
  • Application.Run(new MyWindow())
  • public MyWindow()
  • this.MouseUp new EventHandler(this.Handler())
  • //MouseUp is a container
  • private void Handler(object sender,System.EventAr
    gs e)
  • Console.WriteLine(call from 0,sender.ToStrin
    g())

object
function pointer
Handler function
29
Differences between the two approaches
  • Callback by inheritance is easier to understand
  • The mechanism is more direct and simple
  • Callback by delegation is more powerful
  • Can callback to multiple handlers (multicasting)
    by adding several EventHandler objects to the
    MouseUp container in Form
  • Can change the callback handler at run-time by
    changing the EventHandler object

30
Delegate how to Build your own handler object
  • delegate keyword
  • A built-in C keyword that helps you to create
    your own EventHandler object easily
  • public delegate void MyHandler(Student s)
  • This instruction creates the class MyHandler for
    you
  • A MyHandler object can encapsulate any function
    pointer with a matched signature (void and
    Student)

31
Example of using delegate to generate handler
  • public delegate void CallbackDelegate(String s)
  • //a Callback class is generated automatically
  • Main()
  • ArrayList list new ArrayList()
  • Client c new Client()
  • list.Add(new Callback(c.Notify())
  • foreach ( (Callback) obj in list)
  • obj(server)
  • Class Client
  • private void Notify(String s)
  • Console.WriteLine(call from 0,s)

The function pointer
This object function is called from list
32
Game application
  • Implementation reuse is best done by delegation

Container
Fighter Attack()
Fighter Attack()
ConcreteFighter Image _image Weapon
_w Draw() Attack() _w.Attack()
Fighter Attack()
Weapon Attack()
Fighter Attack()
Fire Attack()
Sand Attack()
33
Visual Studio Window Forms
  • Tool that helps you to design a GUI easily
  • Open a new project and choose Window Forms
  • Drag and drop GUI into the Form

34
Add a Button
  • In ToolBox window, drag a Button object and drop
    it to the position you want in the Form

35
What Visual Studio has done
  • Initially Visual Studio has an empty container
  • Your action (dragging a button) causes
  • A button is created and added to the container
  • Visual Studio then asks objects in the container
    to draw themselves, that is why you see the button

36
Change Buttons properties
  • Can resize and move the button, and use the
    Properties window of the button to change the
    name.

37
What Visual Studio has done
  • Your action (select the button) causes
  • Collision detection
  • Visual Studio checks the mouse pointer position
    and sees which object in its container is hit
  • Find the button object
  • Your further action acts on the button invokes
    methods inside the button which change the
    attributes of the button

38
The generated code (view by right click on the
form)
  • public class Form1 System.Windows.Forms.Form
  • private System.Windows.Forms.Button button1
  • static void Main()
  • Application.Run(new Form1())
  • public Form1()
  • InitializeComponent()
  • private void InitializeComponent()
  • this.button1 new System.Windows.Forms.Button()
  • //. . .
  • protected override void Dispose( bool disposing
    )
  • //. . .

39
Notes
  • Application.Run(new Form1())
  • Run() is a static method that has a loop to
    process the event messages
  • The Main() is very small !
  • Constructor of Form1() calls InitializeComponent()
  • InitializeComponent() simply create all the GUI
    controls and add them to the container of Form1
  • Dispose()
  • Call by application in exit to free unmanaged
    resources (more about this later)

40
InitializeComponent()
  • region Windows Form Designer generated code
  • private void InitializeComponent()
  • this.button1 new System.Windows.Forms.Button()
  • this.button1.Text "Click Me !"
  • this.Controls.Add(this.button1) //add to
    container
  • . . .
  • endregion
  • Controls is a Property
  • this.Controls return a reference to a container

41
What Visual Studio has done
  • InitializeComponent() is the main part in the
    code generation (the rest are just boilerplate
    code)
  • Visual Studio simply asks each object in its
    container to output code based on its stored
    information
  • A button would simply generate the constructor
    code and the Text code, then adds itself to the
    container this.Controls
  • this.Controls is used by window server for
    collision detection, etc

42
Class diagram of Windows Form
Object
MarshalByRefObject
Control.ControlCollection
1
Component
Control

ScrollableControl
ButtonBase
PictureBox
Button
Panel
ContainerControl
Form
43
Class diagram of Windows Form
  • Where is the container?
  • How does this class structure supports
    composition (has-a) relationship?
  • Each Control has a ControlCollection (container)
  • Each ControlCollection can have many Controls
  • What is the equivalent of class Glyth in this
    structure?
  • Control

44
Invalidate()
  • A single command that invokes the action of
    redrawing all the GUI items in Form
  • Invoke whenever the window is dirty
  • e.g. resize, expose,
  • Window server calls this.Invalidate(), which
    sends a message to the message queue that will be
    processed by the Application.Run() loop
  • Form ask all Controls in its container to draw
    itself
  • Each Control asks Controls in its container to
    draw itself (recursion)

45
Unmanaged resource
  • Unmanaged resource is resource that is not
    managed by the garbage collector (e.g. memory
    obtained from Cs malloc(), must be deleted by
    the user)
  • Managed resource is a key advantage of C/Java
    over C/C
  • When garbage collector is invoked, it calls
    Finalize() automatically
  • Finalize() is the C destructor (opposite of
    constructor)
  • Can have user code to remove unmanaged resource
  • If you dont have unmanaged resource, then you
    dont need to write object.Finalize()

46
Dispose()
  • Problem with Finalize()
  • Finalize()can only be invoked by GC, but GC is
    only called when system runs out of memory
  • For critical resources, this is too late
  • e.g. opened file, opened database connection,
    want to close as soon as possible
  • Solution
  • Called Dispose() explicitly
  • If you use Dispose(), then you should call
    GC.SuppressFinalize()to suppress Finalize()
  • Otherwise the object will be clear twice

47
Dispose()
  • Finalize() is a method in System.Object
  • Supported by all classes
  • Dispose() is NOT a method in System.Object
  • For an object to support Dispose(), must inherit
    the IDisposable interface and implement the only
    function Dispose()
  • Base class Form support IDisposable , it provides
    a do-nothing default
  • You cannot use Dispose() to remove managed
    resource, managed resource can only be released
    by garbage collector

48
Using
  • The special construct Using calls Dispose()
    automatically whenever it is out of scope
  • Standard usage
  • static void Main()
  • using (Form1 frm new Form1())
  • Application.Run(frm)
  • //frm.Dispose() called here

49
  • SOA
  • Web Services

50
.Net Remoting layer
  • How to simplify network programming?
  • Some key terms
  • Object serialization
  • Proxies
  • Marshaling by reference / value
  • Server activated objects

51
Serialization
  • Convert the state of an object into a linear
    sequence of byte stream, and send it to another
    computer
  • To make an object serializable, label the class
    with Serializable attribute
  • Serializable
  • public class Student
  • . . .
  • Default is no serialization, for security reason

52
Serialization Formatter
  • Different formats for sending the serialized
    objects
  • Binary format
  • SOAP (Simple Object Access Protocol) format
  • XML format
  • using System.Runtime.Serialization.Formatters.Bina
    ry
  • using System.Runtime.Serialization.Formatters.Soap
  • Using System.Xml.Serialization.XmlFormatter
  • SOAP is XML-based
  • SOAP and XML use ascii characters, can be read by
    human

53
Saving objects to file by serialization
  • //save object to file
  • Student s new Student()
  • FileStream fs File.Create(student.dat)
  • BinaryFormatter format new BinaryFormatter()
  • format.Serialize(fs, s)
  • //retrieve the object from file
  • FileStream fs File.OpenRead(student.dat)
  • Student s (Student)format.Deserialize(fs)

54
.NET Remoting
  • Client
  • The one who wants to use a remote object
  • Server
  • The one who provides the remote object
  • Marshaling
  • Describes how a remote object is passed to client
  • Marshal-by-value (MBV)
  • Client gets a copy of the remote object
  • Marshal-by-reference (MBR)
  • Client gets a proxy of the remote object

55
  • MBV
  • Server passes a copy of remote object to client
  • Any modification by the client wont affect the
    original object in the server side
  • MBR
  • Server passes a proxy of the object to the
    client, the proxy acts as a reference to the
    object
  • Any modification by the client changes the
    original object in the server side

56
Key elements in Remoting
  • Proxy
  • The local object in the client side that
    impersonates the remote object
  • Formatter
  • Channel TCP / HTTP

Client object
Remote object
Proxy
Channel
Formatter
Formatter
57
Client-server programming using .NET Remoting
  • A simple client-server program in OO
  • Client gets a copy or a proxy of the remote
    object
  • Use the object to send a message to server
  • Server returns a message to client
  • Example creates 3 projects
  • SimpleClient.cs
  • SimpleServer.cs
  • Remote.cs
  • Class for the remote object
  • Use by both SimpleClient and SimpleServer

58
Remote.cs
  • //The MBR object at the server side
  • using System
  • namespace RemoteLib
  • public class RemoteHello MarshalByRefObject
  • public void SendMsg(string str) //from
    client
  • Console.WriteLine("0",str)
  • public string ReceiveMsg() //servers
    response
  • return "hello world"

59
SimpleServer.cs
  • using System
  • using System.Runtime.Remoting
  • using System.Runtime.Remoting.Channels
  • using System.Runtime.Remoting.Channels.Http
  • using RemoteLib
  • namespace SimpleServer
  • class Server
  • static void Main()
  • HttpChannel c new HttpChannel(12345)
  • ChannelServices.RegisterChannel(c)
  • RemotingConfiguration.RegisterWellKnownServiceT
    ype(
  • typeof(RemoteHello), "RemoteHelloObj.soap",
  • WellKnownObjectMode.Singleton) //start the
    server
  • Console.ReadLine() //to quit, just type
    something

60
SimpleClient.cs
  • using System
  • using System.Runtime.Remoting
  • using System.Runtime.Remoting.Channels
  • using System.Runtime.Remoting.Channels.Http
  • using RemoteLib
  • class Client
  • static void Main()
  • HttpChannel c new HttpChannel()
  • ChannelServices.RegisterChannel(c)
  • object remoteObjActivator.GetObject(typeof(Remot
    eHello),
  • "http//localhost12345/RemoteHelloObj
    .soap")
  • RemoteHello simple (RemoteHello) remoteObj
    //casting
  • simple.SendMsg("hello from client")
  • Console.WriteLine(From server0",
    simple.ReceiveMsg())

61
History of Distributed Computing
  • 1980 Remote Procedure Call (RPC)
  • (to invoke functions in a remote machine)
  • 1990 ORPC (Object-RPC)
  • (to invoke methods in a remote object)
  • 1990 CORBA by OMG (Object Management Group)
  • Common Object Request Broker Architecture
  • To support communication at object level in a
  • heterogeneous environment

62
Key Components in CORBA
  • IDL Interface Definition Language
  • ORB The middleware that takes care of the
  • communication between objects, e.g.
  • method invocation, parameters passing

client
IDL interface
IDL interface
server
ORB
stub
skeleton
Network
63
Other similar models
  • DCOM by Microsoft
  • (Distributed Component Object Model)
  • RMI by Java
  • (Remote Method Invocation)
  • Limitations
  • DCOM and Java are platform specific
  • For CORBA, both sides must run the same ORB
  • ORBs from different vendors may have
    interoperability problems

64
  • Interoperability issue
  • Advent of e-commerce, want distributed computing
    to be more widely available
  • With DCOM, RMI, or CORBA, the chance that both
    ends run the same platform is not high
  • 2000 IBM and Microsoft proposed the web
  • services which forms the basis for SOA
  • (Service Orientated Architecture)

65
SOA
  • Power plant example
  • A hundred years ago, each family had its own
    power generator
  • Complex, you need to know how to fix the machine
  • What happened next
  • Development of power transmission grid
  • Simple interface at client side (just a switch)
  • Complexity of power generation at the power plant
    side (the server)
  • Web services plan to follow the same development
  • Complex server, thin clients

66
  • The ultimate web service

67
Key Issues in SOA
  • How to describe the interface of a remote object?
  • By WDSL (Web Services Description Language)
  • Describes the interface in XML (replace IDL)
  • How to communicate with the remote object?
  • By SOAP (Simple Object Access Protocol)
  • To invoke a method, send a message that has the
    name of the method and parameters
  • How to find objects that provide the services?
  • By UDDI (Universal Description, Discovery and
    Integration)
  • A directory service to search for web services

68
Web services model
Server
Client
HTTP/SOAP
Concrete Object
Proxy object
URI
Interface
69
WSDL is just a XML file
portType is similar to class
  • name"getPrice" / on

Method in a class
70
  • WSDL
  • Use XML to describe a class
  • .NET can generate the WSDL file automatically
    from a class definition
  • Client can download the WSDL file, and use it to
    create proxy objects
  • Many companies already provide the web services,
    e.g. Amazon, Google

71
A SOAP request to the server
  • POST /InStock HTTP/1.1 Host www.stock.org
  • xmlnssoap"http//www.w3.org/2001/12/soap-envelop
    e"

  •   
  • HSBC     

Schema
Namespace of SOAP
Schema of your namespace m
parameter
method
72
A SOAP response from server
  • HTTP/1.1 200 OK
  • xmlnssoap"http//www.w3.org/2001/12/soap-envelop
    e"
  • "
  • 34.5

73
XML-Scheme
  • Schema is a document that describes the abstract
    structure of a data set
  • Like what you have done to describe the structure
    of a class
  • XML schema provides a list of names (types) that
    are used to define complex data structure
    (similar to how you define a class or structure)
  • e.g a string type can be described by xsstring

74
Why use XML?
  • Syntax-wise XML file is similar to HTML
  • The main reason of using XML is that it supports
    namespaces
  • Namespace solved the problem of name collision
  • An XML namespace is a collection of names,
    identified by the URI reference
  • Elements in the XML namespace is defined by the
    schema

75
Key advantages of web services
  • Free (Web Services) vs fee economic model (
    CORBA)
  • Communication via HTTP port 80 channel
  • Firewall friendly
  • Use widely accepted standards
  • Method invocation is via SOAP messages encoded in
    XML
  • Interface definition is by WSDL, also in XML
  • Interoperability problem solved by using protocol
    based on SOAP and XML
  • Any node that supports HTTP, WSDL, and SOAP can
    be connected to SOA
Write a Comment
User Comments (0)
About PowerShow.com