Web based classes
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Web based classes

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Posts
    324

    Lightbulb Web based classes

    Overview

    Don't try running this code - it's psudo code meant as an example of how Classes work. If anyone is interested I will post some real ones for you to have a play with. I thought that for the purposes of this example it would be fun to try to write a human resources class to handle employee hiring and firing. Let's say that Software company XYZ have decided to sack all of their human resource managers and hire instead people who might, with a little bit of luck and best of three attempts, be able to find their arse with both hands. And let's say that you have been tasked with writing a web based piece of software to replace the pointy haired maggots .

    Why would we want to use classes?

    Classes allow us to reuse code. We can store our common routines in classes and reuse it around our site as often as we need. This cuts both debugging time (because after a while you will have tested your base classes into the ground) and development time (because after a while you can start off virtually any project with half of the dog-work complete).

    Instantiation

    To use our class in our web based scripts we need to create an instace of our class. The following example shows how we can instantiate an instance of our clsHumanResources class and assign it to the HR variable. Once instantiated we can call the methods and properties defined in the class in the format:

    HR.<method>(<Properties>)
    Code:
    'Include the path to our class file
     
    <%
    'Instantiate an instance or clsHumanResources
    'and assign it to the variable HR
    Set HR = NEW clsHumanResources
    %>
    Publicly exposed members of the class

    Whenever I talk about publicly exposed members people always seem to have some sort of coughing fit . Cant imagine why. But this concept is quite important.

    When the class has been instantiated only those methods defined as public are availible to the calling script. Private members can only be called from within the class itself. This is important because you wouldn't want someone to accidentally access the hr.salary vlaue for your CEO. This information is private and therefore must be a private member of the class.

    Publicly exposed members (stop sniggering) can be used to change the private variable stored within the class. For instance, though we don't want to return the HR.salary value, we may wish to change that value. We can provide public members (inrement_salary and decrement_salray) to allow acces to this kind of functionallity.

    The publicly exposed members in the clsHumanResources are:

    Hire_Employee
    The Hire_Employee method is called to begin the hiring process, which calls private methods to arrange interviews and negotiate salarys. When the method has found and hired a suitable employee it returns the employee number of the new employee.

    Initialise
    Becasue the Internet is a stateless environment the scope of any variable is only as long as the execution of the calling script. Each time the class is instantiated you would call the initialise method to load the private employee variables into the class

    Save
    If the calling script makes changes to the information held in the class if must save those new values before the calling script quits and the resourses that it used are released. This is accomplished by calling ths Save method.

    Issue_Warning
    The clsHumanResources class contains a function for Sack_Employee, but I didn't want to make this member public, just in case managers decided to sack people because they looked funny. So to sack someone you have to issues them with a warning. This method accepts a severity level so that gross_misconduct (defined as a constant) can result in automatic dismissal. If the Waring count gets to three then the private Sack_employee method is fired.

    Employee_Quit
    When an employee quits the class check the retirement age constant to see if the employee has reached retirement age. If they have then the class fires the Gold_Clock method, else proceeds to process their paperwork and cancel their salary.

    Decrement_Salary/Increment Salary
    These should be self explaintory.

    Supporting Classes

    Within classes it is possible to reference other classes. It is possible to have multi tiers of generic classes all contributing to the final result, and reducing the amount of coding per problem that you face. These classes are instantated and referenced in the same way as shown above.

    clsSecurityGuard
    The clsSecurityGuard has one public member. That member is Escort_Off_Premises. If it helps you visualise you can imagine that the security guards name is Bernard.

    clsWhosBoss
    I almost called this the office_politics class. Certainly the logic of such a class would be tortured at best.

    The Human resources Class

    Code:
    'Include the supporting class files
     
     
    <%
    
    'Initialise supporting classes
    'You can have classes within classes within classes
    Set WhosBoss = NEW clsWhosBoss
    Set SecurityGuard = NEW clsSecurityGuard
    
    'Constants - these have global scope
    Const Glass_Celing_Salary = 60000
    Const Retirement_Age = 60
    
    'These constants are used with the p_severity value
    'of the issue_warning method
    Const Misdemeanor = 1
    Const Misconduct = 2
    Const Gross_Misconduct = 3
    
    'Start of class code
    Class clsEmployee
    
    'Public Variables------------------------------------------------->
    'Public variables are accessed using the get and let statements
    'm_Current_Project holds a reference to the public variable at
    'local scope level
    Private m_Current_Project
    
    'Public Variables------------------------------------------------->
    'These variables are NOT accessible to the calling program
    Private Employee_number
    Private Employee_age
    Private Job_Title
    Private Employee_Gender
    Private Salary
    Private WarningCount
    
    'Public Functions------------------------------------------------->
    
    Function Hire_Employee(p_Job_title, p_Starting_Salary, p_Start_Date)
    
    	do
    		do until found(suitable_candidate)
    		loop
    	loop until interview_result(p_Candidate_Number) = true
    
    	'...
    	'Set private Employee variables here
    	'...
    	Save
    
    	Hire_Employee = employee_number
    
    End Function
    
    'Private Functions------------------------------------------------->
    
    Private Function Interview_Result(p_Candidate_Number)
    
    	if Is_Human(p_Candidate_Number) and & _
    	social_skills(p_Candidate_Number) > 1 then
    		if Negotiate = true then
    			Interview_Result = true
    		else
    			SecurityGuard.Escort_Off_Premises(p_Candidate_Number)
    		end if
    	else
    		SecurityGuard.Escort_Off_Premises(p_Candidate_Number)
    	end if
    			
    
    End Function
    
    Function Negotiate(p_Top_Figure, p_Bottom_Figure)
    
    	do until p_Top_Figure = p_Bottom_Figure or Interview_Length > 2 hours 
    	loop
    
    	if p_Top_Figure = p_Bottom_Figure then
    		Negotiate = true
    	else
    		Negotiate = false
    	end if
    
    End Function
    
    'Public Methods------------------------------------------------->
    'These methods can be accessed by the calling routine
    
    Sub Initialise(p_Employee_Number)
    
    	Employee_number = p_Employee_Number
    	'...
    	'some code here to load the other private vars into the class
    	'...
    
    End Sub
    
    
    Sub Save()
    
    	'...
    	'some code here to load the other private vars into the class
    	'...
    
    End Sub
    
    
    Sub Issue_Warning(p_Severity)
    
    	WarningCount = WarningCount + 1
    
    	Select Case p_severity
    		case Misdemeanor
    			'Warning issued - no further action
    		case Misconduct
    			'Warning Issued
    			Decrement_Salary(1000)
    		case Gross_Misconduct
    			'Dissmissal on spot
    			Sack_Employee(p_Employee_Number)
    	End select
    
    		If WarningCount = 3 then
    			Sack_Employee(p_Employee_Number)
    		end if
    End Sub
    
    
    Sub Employee_Quit(p_Employee_Number)
    
    	If Employee_age => Retirement_Age then
    		'Employee has reached retirement age
    		Gold_Clock(p_Employee_Number)
    	else
    		Stop_Wages(p_Employee_Number)
    	end if
    	
    End Sub
    
    Sub Increment_Salary(p_Value)
    
    	'Code Added by by PHB January 1919
    	'to reflect new social realities
    	Select case Employee_Gender
    		case "Female"
    			if p_Value + Salary > Glass_Celing_Salary then
    				Salary = Glass_Celing_Salary
    			else
    				Salary = p_Value + Salary
    			end if
    		case Else
    			Salary = p_Value + Salary
    	end select
    
    End Sub
    
    
    Sub Decrement_Salary(p_Value)
    
    	Salary = salary - p_value
    
    End Sub
    
    'Private Methods------------------------------------------------->
    'These methods are private - they can only be called 
    'from within this class
    
    Private Sub Sack_Employee(p_Employee_Number)
    'This method is private because yoo can't just sack someone
    'You first have to call the Issue warning method which will
    'call the 
    	
    	Stop_Wages(p_Employee_Number)
    	SecurityGuard.Escort_Off_Premises(p_Employee_Number)
    
    End Sub
    
    Private Sub Gold_Clock(p_Employee_Number)
    
    	'Code edited by PHB Arpil 1st 1980
    	'Salary = salary / 2
    	'Throw_Party(p_Employee_Number)
    	'Give_Gift(p_Employee_Number)
    
    	Stop_Wages(p_Employee_Number)
    	SecurityGuard.Escort_Off_Premises(p_Employee_Number) 
    	'Make sure employee doesn't steal any pens on way out
    
    End Sub
    
    Private Sub Stop_Wages(p_Employee_Number)
    
    	Issue_Pink_Slip(p_Employee_Number)
    	Decrement_Salary(Salary)
    	Hire_Employee(Job_Title, (Salary/2), date(now))
    
    End Sub
    
    
    'Property Change event------------------------------------------------->
    'NOTE web based class do no have events, so I raise a call to a function 
    'that allows you to set the str_Current_Project to "cancel" to abort the
    'changes.
    
    Public Property Get Current_Project
       Current_Project = m_Current_Project
    End Property
    
    Public Property Let Current_Project(str_Current_Project)
       If OnChange_Current_Project(str_Current_Project) <> "cancel" then
          m_Current_Project = str_Current_Project
       end if
    End Property
    
    Function OnChange_Current_Project(New_Value)
    
    	'I'd like to think the following were true:
    	'If value_to_company(project(new_value)) > & _
    	'value_to_company(Current_project) then
    	'But in fact this is what happens:
    
    	if Whosboss.BossOf(Employee_Number) = & _
    	Whosboss.Whos_Asking(Employee_Number) then
    		New_Value = New_Value
    	else
    		New_Value = "cancel"
    	end if
    
    End Function
    
    End Class
    %>
    If you enjoyed this article you might also like these others:
    Backing up the IIS metabase.
    Building your own IDS tripwire.
    Credit card security
    Dumping SQL data to a text file
    Hunting down skript kiddies
    Search Engine submission 'exploit'
    Forced shutdown of a remote nt/2k server
    Securing an installation of IIS 4. (No, seriously)
    Remote DSN Connections, using WinAPIs and the registry
    Scripting Internet Connections Under Window$
    \"I may not agree with what you say, but I will defend to the death your right to say it.\"
    Sir Winston Churchill.

  2. #2
    Senior Member
    Join Date
    Apr 2002
    Posts
    1,050
    another fine tutorial ntsa *round of applause for ntsa* brilliant work man
    By the sacred **** of the sacred psychedelic tibetan yeti ....We\'ll smoke the chinese out
    The 20th century pharoes have the slaves demanding work
    http://muaythaiscotland.com/

  3. #3
    Antionline Quitter..Srsly
    Join Date
    Aug 2001
    Posts
    457
    another long read ...great one dude
    \"\"A weak mind is like a microscope, which magnifies trifling things but cannot receive great ones.\" G.K. Chesterton, 19th-century English essayist and poet\"

  4. #4
    Senior Member
    Join Date
    Apr 2002
    Posts
    324
    Thanks!
    \"I may not agree with what you say, but I will defend to the death your right to say it.\"
    Sir Winston Churchill.

  5. #5
    Banned
    Join Date
    Jul 2002
    Posts
    4
    yah man, props for that 1

  6. #6
    Senior Member
    Join Date
    Apr 2002
    Posts
    324
    props? Sorry - I'm English, but thanks (I think)
    \"I may not agree with what you say, but I will defend to the death your right to say it.\"
    Sir Winston Churchill.

  7. #7
    Banned
    Join Date
    Nov 2001
    Posts
    188
    ntsa, i think someone would have my back saying that you put out some of AO's best tutorials.

  8. #8
    er0k
    Guest
    ntsa: umm... if only i wasnt so lazy i would read that... hella good job though, im sure its great! hehe

  9. #9
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    Call me stupid, but although it's helpful, I don't think much modelling has gone on in the development of the above example.

    Surely a HumanResources object is a bit abstract. Also, what real-world entity does it represent? Why is it plural when there's only one of them?

    Surely if you want to fire an employee, it makes more sense to find the Employee object you want to fire then call

    employee.Fire(Reason)

    Rather than using an abstract non-relevant utility "object" like HumanResources.

    Just my tuppence worth

  10. #10
    Senior Member
    Join Date
    Apr 2002
    Posts
    324
    Slarty >

    Read the first sentence again. This is psudo code meant only as an example of what a class structure looks like. It was meant to be abstract.

    I felt that the best way to begin this series of articles on the concepts of object oriented web classing (starting here and ending with multithreaded ActiveX component development for web pages) was to present the basic ideas in the abstract with a hint of comedy, so as to be readily accessible by any reader with a little BASIC knowledge.

    Even at this basic level it seems that it was too much for Er0k. But this board has many users at many levels of competence, and if you feel yourself to be more competent that the intended audience of this article, I apologise.

    I also apologise if you fail to appreciate the value of the abstract when explaining a concept and I further apologise if you fail to see the humour. But as a southerner I am sure you will appreciate that you get nowt for 2p.

    And as for not making the FireEmployee method public I feel sure you will find the (detailed) reason as to why it is not if you take the time to actually read the article.

    If you don't like my articles I suggest you stop reading them.

    TIA
    \"I may not agree with what you say, but I will defend to the death your right to say it.\"
    Sir Winston Churchill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •