Lecture 5:
Component-based Programming
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
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-2
2
Part 1
• Component-based programming…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-3
3
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
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-4
4
.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
.NET program
.NET Framework
Class Library (FxCL)
.NET
Framework
Common Language Runtime (CLR)
Operating System
Hardware
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-5
5
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
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-6
6
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" basis
Supplemental UI Library:
– see http://msdn.microsoft.com/vjsharp/supui/
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-7
7
Part 2
• Working with .NET components…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-8
8
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…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-9
9
Command-line development
• Main tools:
– vjc:
– notepad:
Visual J# compiler
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
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-10
10
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>
ouch!
ouch!
ouch!
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
guiapp.exe
5-11
11
GUIApp
• GUIApp is a single source code file "GUIApp.jsl":
public class GUIApp
{
public static void main(String[]
{
javax.swing.JFrame frame
=
java.awt.Container c
=
javax.swing.JButton button1 =
args)
new javax.swing.JFrame();
frame.getContentPane();
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
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-12
12
Compilation
• Compile using the Visual J# compiler "vjc"
– reference Supplemental UI Library to use SWING
C:\GUIApp>
04/20/2004
dir
673
GUIApp.jsl
C:\GUIApp> vjc /t:exe GUIApp.jsl /r:vjssupuilib.dll
Microsoft (R) Visual J# .NET Compiler version 7.10.3077.0
For Microsoft (R) .NET Framework version 1.1.4322
Copyright...
C:\GUIApp>
04/20/2004
dir
20,480 GUIApp.exe
– compiler switches:
/t:
/r:
build a console-based .EXE
reference the specified .NET component (.DLL)
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-13
13
Run!
• Run the .EXE from the command-line:
C:\GUIApp>
ouch!
ouch!
ouch!
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
guiapp.exe
5-14
14
.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/
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-15
15
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.
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-16
16
Example #2
• A better example of SWING
– Visual Sorting demo…
public static void main(String args[])
{
VisualSorting sd = new VisualSorting();
public VisualSorting()
sd.setSize((int) (520 * 1.61), 520);
{
sd.setResizable(false);
super("VisualSorting");
sd.reset();
URL u = VisualSorting.class.getResource("Random.jpg");
sd.show();
icon= new ImageIcon(u);
}
// create AlgorithmPanels, and the mainControlPanel
populateAlgorithmsPane();
mainControlPanel = new MainControlPanel (this);
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
// 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;
.
.
.
5-17
17
Part 3
• Creating your own .NET components…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-18
18
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
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-19
19
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
GUI.exe
Business.dll
DataAccess.dll
Presentation
Tier
Business Logic
Tier
Data Access
Tier
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
e.g. purchase
products or pay
employees
e.g. read and
write from
customer files or
database
Data
Tier
e.g. data
source, like
files or
database
5-20
20
Component-based BankingApp
• Let's rebuild the BankingApp as a set of components:
App.jsl
Transactions.jsl
Customer.jsl
CustomersIO.jsl
customers.txt
BankingApp.exe
Transactions.dll
Customers.dll
transactions.txt
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-21
21
(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>
04/20/2004
04/20/2004
04/20/2004
04/20/2004
dir *.jsl
1,039
743
1,603
1,443
App.jsl
Customer.jsl
CustomersIO.jsl
Transactions.jsl
C:\BankingApp> vjc /t:library Customer.jsl CustomersIO.jsl /out:Custo
mers.dll
Microsoft (R) Visual J# .NET Compiler version 7.10.3077.0
For Microsoft (R) .NET Framework version 1.1.4322
Copyright...
C:\BankingApp>
04/20/2004
dir *.dll
20,480
Customers.dll
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-22
22
(2) Transactions.dll
• Second, build the middle-level business tier, Transactions.dll:
– reference Customers.dll to gain access to definition of Customer
C:\BankingApp>
04/20/2004
04/20/2004
04/20/2004
04/20/2004
dir *.jsl
1,039
743
1,603
1,443
App.jsl
Customer.jsl
CustomersIO.jsl
Transactions.jsl
C:\BankingApp> vjc /t:library Transactions.jsl /r:Customers.dll /out:Trans
actions.dll
Microsoft (R) Visual J# .NET Compiler version 7.10.3077.0
For Microsoft (R) .NET Framework version 1.1.4322
Copyright...
C:\BankingApp>
04/20/2004
04/20/2004
dir *.dll
20,480
Customers.dll
20.480
Transactions.dll
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-23
23
(3) BankingApp.exe
• Third, build the presentation tier .EXE, BankingApp.exe:
– references both Customers.dll and Transactions.dll
C:\BankingApp>
04/20/2004
04/20/2004
04/20/2004
04/20/2004
dir *.jsl
1,039
743
1,603
1,443
App.jsl
Customer.jsl
CustomersIO.jsl
Transactions.jsl
C:\BankingApp> vjc /t:exe App.jsl /r:Customers.dll /r:Transactions.dll /out:Ba
nkingApp.exe
Microsoft (R) Visual J# .NET Compiler version 7.10.3077.0
For Microsoft (R) .NET Framework version 1.1.4322
Copyright...
C:\BankingApp>
04/20/2004
04/20/2004
04/20/2004
dir *.dll;*.exe
20,480
Customers.dll
20.480
Transactions.dll
20,480
BankingApp.exe
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-24
24
Run!
• BankingApp runs exactly as before…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-25
25
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…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-26
26
Summary
• .NET is built using component-based programming
• J# programming is component-based programming
• Professional programming is component-based programming
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
5-27
27
Descargar

4. Component-based Programming