27
Lecture 5: Component-based Programming

Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

Embed Size (px)

Citation preview

Page 1: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

Lecture 5:

Component-based Programming

Page 2: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

2MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-2

Objectives

“Components are another term for classes, DLLs, and assemblies. Component-based programming is the logical tendency to design our programs as a set of components instead of a large monolithic app. Given the design of .NET and its Framework Class Library, J# programming is inherently component-based programming... ”

In 3 Parts:

1. Component-based programming

2. Working with .NET components

3. Creating your own .NET components

Page 3: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

3MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-3

Part 1

• Component-based programming…

Page 4: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

4MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-4

Component-based programming

• OO programming is component-based programming– classes are components

• .NET programming is component-based programming– DLLs are components

• Other components you'll use:

– Supplemental UI Library

Page 5: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

5MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-5

.NET components

• .NET components must be installed to run a .NET program– in fact, CLR + FxCL must be installed– these are known collectively as the .NET Framework

HardwareHardware

Operating SystemOperating System

Common Language Runtime (CLR)Common Language Runtime (CLR)

.NET program.NET program.NET Framework.NET Framework

Class Library (FxCL)Class Library (FxCL).NETFramework

Page 6: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

6MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-6

Installing .NET Framework

• .NET Framework is installed as part of Visual Studio .NET

• How to install on systems without VS .NET?• Pre-installed on Windows 2003 and above• Otherwise you must install 2 pieces of software, in this order:

1. .NET Framework 1.1 Redistributable: dotnetfx.exe (20MB)

2. Visual J# .NET Version 1.1 Redistributable: vjredist.exe (7MB)

• Requirements:

– Windows 98 or above

Page 7: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

7MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-7

Installing .NET Framework (cont'd)

• Installing .NET Framework 1.1 Redistributable:

– http://msdn.microsoft.com/netframework/technologyinfo/howtoget/

– also available via Windows Update (IE, Tools menu, recommended)

• Installing Visual J# .NET Version 1.1 Redistributable:

– http://msdn.microsoft.com/vjsharp/downloads/howtoget/default.aspx

• Other components are installed on an "as-needed" basisSupplemental UI Library:

– see http://msdn.microsoft.com/vjsharp/supui/

Page 8: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

8MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-8

Part 2

• Working with .NET components…

Page 9: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

9MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-9

Programming options

• J# programming can really be done at two levels:

1. using Visual Studio .NET

2. at the command-line, much like Sun's JDK

• Let's use the command-line tools since it's easier to understand how .NET component work…

Page 10: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

10MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-10

Command-line development

• Main tools:– vjc: Visual J# compiler– notepad: simple Windows text editor (any editor will

do)

• How to get started?

– open Visual Studio .NET 2003 Command Prompt

• Start menu, • All Programs, • Microsoft Visual Studio .NET 2003, • Visual Studio .NET Tools,• Visual Studio .NET 2003

Command Prompt

Page 11: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

11MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-11

Example

• Let's create a simple GUI app using the Java Class Library– in particular, using the SWING classes of the JCL

• GUIApp:– outputs "ouch!" to the console each time you click the button…

C:\GUIApp> guiapp.exeouch!ouch!ouch!

Page 12: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

12MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-12

GUIApp

• GUIApp is a single source code file "GUIApp.jsl":

public class GUIApp{ public static void main(String[] args) { javax.swing.JFrame frame = new javax.swing.JFrame(); java.awt.Container c = frame.getContentPane(); javax.swing.JButton button1 = new javax.swing.JButton("Click Me");

frame.setSize(100, 100); frame.setLocation(100, 100); frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); c.add(button1); button1.addActionListener(new ButtonHandler());

frame.show(); }}//class

class ButtonHandler implements java.awt.event.ActionListener{ public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("ouch!"); }}//class

public class GUIApp{ public static void main(String[] args) { javax.swing.JFrame frame = new javax.swing.JFrame(); java.awt.Container c = frame.getContentPane(); javax.swing.JButton button1 = new javax.swing.JButton("Click Me");

frame.setSize(100, 100); frame.setLocation(100, 100); frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); c.add(button1); button1.addActionListener(new ButtonHandler());

frame.show(); }}//class

class ButtonHandler implements java.awt.event.ActionListener{ public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("ouch!"); }}//class

Page 13: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

13MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-13

Compilation

• Compile using the Visual J# compiler "vjc"– reference Supplemental UI Library to use SWING

– compiler switches: /t: build a console-based .EXE /r: reference the specified .NET component (.DLL)

C:\GUIApp> dir04/20/2004 673 GUIApp.jsl

C:\GUIApp> vjc /t:exe GUIApp.jsl /r:vjssupuilib.dllMicrosoft (R) Visual J# .NET Compiler version 7.10.3077.0For Microsoft (R) .NET Framework version 1.1.4322Copyright...

C:\GUIApp> dir04/20/2004 20,480 GUIApp.exe

Page 14: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

14MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-14

Run!

• Run the .EXE from the command-line:

C:\GUIApp> guiapp.exeouch!ouch!ouch!

Page 15: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

15MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-15

.NET Framework SDK

• .NET Framework SDK contains the command-line tools

– SDK = Software Development Kit

– .NET equivalent of Sun's JDK

– installed as part of Visual Studio .NET

– available separately as a free download (100MB):• http://msdn.microsoft.com/netframework/technologyinfo/howtoget/

Page 16: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

16MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-16

Issues with using Swing

• Advantage of using Swing:

You can recompile without modification usings Sun’s JDK and run on any Java platform.

• Disadvantage of using Swing:

Swing programming by hand is hard and tedious and does not let you take full advantage of the Visual Studio integrated IDE.

Page 17: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

17MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-17

Example #2

• A better example of SWING– Visual Sorting demo…

public VisualSorting(){ super("VisualSorting"); URL u = VisualSorting.class.getResource("Random.jpg"); icon= new ImageIcon(u);

// create AlgorithmPanels, and the mainControlPanel populateAlgorithmsPane(); mainControlPanel = new MainControlPanel (this);

// add the algorithmPanel, and mainControlPanel to the contentPanel getContentPane().setLayout (new GridBagLayout()); GridBagConstraints gConstraints = new GridBagConstraints(); gConstraints.anchor = GridBagConstraints.CENTER; gConstraints.fill = GridBagConstraints.BOTH; gConstraints.gridx = GridBagConstraints.RELATIVE; gConstraints.gridy = GridBagConstraints.RELATIVE; gConstraints.gridwidth = GridBagConstraints.REMAINDER; gConstraints.gridheight = 650; . . .

public VisualSorting(){ super("VisualSorting"); URL u = VisualSorting.class.getResource("Random.jpg"); icon= new ImageIcon(u);

// create AlgorithmPanels, and the mainControlPanel populateAlgorithmsPane(); mainControlPanel = new MainControlPanel (this);

// add the algorithmPanel, and mainControlPanel to the contentPanel getContentPane().setLayout (new GridBagLayout()); GridBagConstraints gConstraints = new GridBagConstraints(); gConstraints.anchor = GridBagConstraints.CENTER; gConstraints.fill = GridBagConstraints.BOTH; gConstraints.gridx = GridBagConstraints.RELATIVE; gConstraints.gridy = GridBagConstraints.RELATIVE; gConstraints.gridwidth = GridBagConstraints.REMAINDER; gConstraints.gridheight = 650; . . .

public static void main(String args[]){ VisualSorting sd = new VisualSorting(); sd.setSize((int) (520 * 1.61), 520); sd.setResizable(false); sd.reset(); sd.show();}

public static void main(String args[]){ VisualSorting sd = new VisualSorting(); sd.setSize((int) (520 * 1.61), 520); sd.setResizable(false); sd.reset(); sd.show();}

Page 18: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

18MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-18

Part 3

• Creating your own .NET components…

Page 19: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

19MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-19

Motivation

• Professionals create their own components for many reasons:

– Team programming: each builds their own component and tests separately.

– Multi-language programming: components in different languages are built (e.g. VB, J#, C++) and then integrated.

– Component reuse– Component updating: Components can be updated

independently to fix bugs

Page 20: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

20MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-20

Example: N-Tier design

• Business applications commonly follow an N-Tier design– application is logically divided by task areas– physically built as a set of components

Business.dll

Presentation

Tier

Data AccessTier

e.g. read and write from

customer files or database

DataTier

e.g. data source, like

files or database

Business LogicTier

e.g. purchase products or pay

employees

GUI.exe DataAccess.dll

Page 21: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

21MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-21

Component-based BankingApp

• Let's rebuild the BankingApp as a set of components:

transactions.txt

customers.txt

App.jslApp.jslCustomer.jslCustomer.jsl

CustomersIO.jslCustomersIO.jslTransactions.jslTransactions.jsl

Transactions.dllBankingApp.exe Customers.dll

Page 22: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

22MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-22

(1) Customers.dll

• First, build the lower-level data access tier, Customers.dll:– in this case we target (/t:) a library, not an exe

– Rather than /t:library we can abbreviate to /t:l

C:\BankingApp> dir *.jsl04/20/2004 1,039 App.jsl04/20/2004 743 Customer.jsl04/20/2004 1,603 CustomersIO.jsl04/20/2004 1,443 Transactions.jsl

C:\BankingApp> vjc /t:library Customer.jsl CustomersIO.jsl /out:Customers.dllMicrosoft (R) Visual J# .NET Compiler version 7.10.3077.0For Microsoft (R) .NET Framework version 1.1.4322Copyright...

C:\BankingApp> dir *.dll04/20/2004 20,480 Customers.dll

Page 23: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

23MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-23

(2) Transactions.dll

• Second, build the middle-level business tier, Transactions.dll:– reference Customers.dll to gain access to definition of Customer

C:\BankingApp> dir *.jsl04/20/2004 1,039 App.jsl04/20/2004 743 Customer.jsl04/20/2004 1,603 CustomersIO.jsl04/20/2004 1,443 Transactions.jsl

C:\BankingApp> vjc /t:library Transactions.jsl /r:Customers.dll /out:Trans actions.dllMicrosoft (R) Visual J# .NET Compiler version 7.10.3077.0For Microsoft (R) .NET Framework version 1.1.4322Copyright...

C:\BankingApp> dir *.dll04/20/2004 20,480 Customers.dll04/20/2004 20.480 Transactions.dll

Page 24: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

24MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-24

(3) BankingApp.exe

• Third, build the presentation tier .EXE, BankingApp.exe:– references both Customers.dll and Transactions.dll

C:\BankingApp> dir *.jsl04/20/2004 1,039 App.jsl04/20/2004 743 Customer.jsl04/20/2004 1,603 CustomersIO.jsl04/20/2004 1,443 Transactions.jsl

C:\BankingApp> vjc /t:exe App.jsl /r:Customers.dll /r:Transactions.dll /out:Ba nkingApp.exeMicrosoft (R) Visual J# .NET Compiler version 7.10.3077.0For Microsoft (R) .NET Framework version 1.1.4322Copyright...

C:\BankingApp> dir *.dll;*.exe04/20/2004 20,480 Customers.dll04/20/2004 20.480 Transactions.dll04/20/2004 20,480 BankingApp.exe

Page 25: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

25MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-25

Run!

• BankingApp runs exactly as before…

Page 26: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

26MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-26

If .NET can't find component?

• If a referenced .DLL cannot be found, program fails to run…• Example:

– move Customers.dll out of folder, and try running app…

move to desktop…

Page 27: Lecture 5: Component-based Programming. 2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET 5-2 Objectives “Components are another term for classes,

27MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-27

Summary

• .NET is built using component-based programming• J# programming is component-based programming• Professional programming is component-based programming