Creating Properties in a Class • Declare all variables in a class as privateencapsulation • private variable values are only available to procedures in the class • When objects are crea
Trang 1Week 9 Building Multitier Programs with
Classes
Trang 22- 2
Contents
• 1 Review to OOP
– Create properties – Create methods
• 2 3-tier application
• 3.Example
• 4 Exercises
Trang 42- 4
1 Creating Properties in a Class
• Declare all variables in a class as private(encapsulation)
• private variable values are only
available to procedures in the class
• When objects are created from a class,
values must be assigned values and values must be passed from the class
Creating Classes
Trang 5A Read-Only Property
class Myclass
{
private int myVar=0;
public int MyProperty {
get { return myVar; } }
}
class Myclass
{
private int myVar=0;
public int MyProperty {
get { return myVar; } }
}
Creating Classes
Trang 62- 6
Constructors
•
• initializes an object when created
• Same name as its class
• similar to a method
• have no explicit return type
A constructor is a method that
automatically executes when an
object is instantiated.
A constructor is a method that
automatically executes when an
object is instantiated.
Creating Classes
Trang 7Constructors with parameters
• for some situations, we need a
constructor that accepts parameters
• Parameters are added to a constructor in the same way that they are added to a
method
Creating Classes
Trang 8Two methods have the same name but a different list of arguments
A signature is the argument list of a method or procedure
A signature is the argument list of a method or procedure
Creating Classes
Trang 9Retrieving shared Variables
• When placed on a static keyword, the
keyword makes values accessible without instantiating the object of the class
class Myclass
{
private static int myVar=0;
public static int MyProperty
{ get { return myVar; } }
}
class Myclass
{
private static int myVar=0;
public static int MyProperty
{ get { return myVar; } }
}
Creating Classes
Trang 102- 10
Create method
• Subroutines
Trang 11Throwing and Catching
• You can make an exception
• Use the try/catch block to enclose code that could cause an exception
– Exampe
Creating Classes
Trang 122- 12
3-tier application
• What is 3-tier model?
• Physical & logic model
• What is business tier
• Business object & original object
• Sending message between tier-s
Trang 132 What is 3-tier model?
• “Plug-in” new components
Applications designed in components (services) where each section performs part of the necessary actions.
Applications designed in components (services) where each section performs part of the necessary actions.
Object-Oriented Programming
Trang 14Business Object Validation Calculations Business logic Business rules
User Services
Trang 15What is 3-tier model?
Multitier Applications
• Presentation tier the user interface
You can change the method of delivery without changing the processing
You can change the method of delivery without changing the processing
Trang 17What is 3-tier model? More detail
Trang 18• presents information to the user, and
collects user input
Trang 19What is 3-tier model?
UI
• logic to decide what the user sees, the
navigation paths, and how to interpret user input
• accepts user input and then provides it to the business logic, where it can be
validated, processed
• In NET, UI code is almost event-driven
• Only request to middle-tier
Trang 202- 20
What is 3-tier model?
Business logic
• Definition from Microsoft
"The combination of validation edits, logon verifications, database lookups, policies, and algorithmic
transformations that constitute an enterprise's way of doing business".
• must reside in a separate tier from the UI code
• Request to DAL
Trang 21What is 3-tier model?
Trang 222- 22
What is 3-tier model?
Data storage and management
• Database servers such as SQL Server or MS Acccess, Oracle
• physical creation, retrieval, update, and
deletion of data
Trang 23Physical & logic model Framework Design
Trang 242- 24
3-tier in logic and physical
UI Win.app
UI
Middle-tier VB6, VB.Net, C#, Java
Data –tier (DAL) VB6, VB.Net, C#, Java
Data store
Server 1
Data store Server 3 Data store
Server 2
Trang 25Physical & logic model
Single tier System
• Good for standalone environments,
• install everything on a single client
workstation
• n-tier systems can run on a single machine
Trang 27Physical & logic model 3-tier
• can centralize all access to the
database on a single machine
Trang 282- 28
Business objects and Object
• Object: encapsulate, abstract entity or
Trang 29Why using Business tier?
• Cannot assume that incoming data is valid
• Not all incoming data is from the user
• Data from the database may also be invalid
– Translated data
– Updates to data from other applications
– Errors ???
Trang 30+Can not raise message in middle, data tier
+Can not generate SQL command in middle, UI tier
Trang 31Example
• Hanoi tower
– Using event for message sending from business tier to UI.
• Contact phone application
– Using 2 data store: SIM & PHONE
– Using exception
• Using Class Library (Class_Library.PPT)
Trang 322- 32
3.Class Library
Make library for others projrct
Menu File->All new Project->Class Library From main project->Add Refference
Example
Trang 33How to make a 3-tier app