CSC/ECE 517 Fall 2010/ch2 2c ck: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 56: Line 56:


===Class Approach===
===Class Approach===
'''Start with use cases''':
#'''Start with use cases''':
   As a customer I need to be able to view my balance.
   As a customer I need to be able to view my balance.
   As a customer I need to be able to deposit money into my account.
   As a customer I need to be able to deposit money into my account.
Line 63: Line 63:
   As a teller I need to be able to see all tracnactions this customer has made in the past.
   As a teller I need to be able to see all tracnactions this customer has made in the past.
   ...
   ...
#'''Create a class diagram''':


===Prototype Approach===
===Prototype Approach===

Revision as of 16:37, 18 September 2010

Prototype Based Inheritance

Introduction

Dictionary Definition: An original, full-scale, and usually working model of a new product or new version of an existing product. [1]
"... the prototype approach in some ways corresponds more closely to the way people seem to acquire knowledge from concrete situations" [2]
Since the early period of implementation of Object Oriented Design (OOD), there have been two schools of thought concerning the best method of implementing OOD. The class approach, which is the static method of implementation. And the Prototype approach, which is the dynamic method of implementation.
It is this authors opinion that these discussions and arguments will prevail for as long as OOD itself prevails.

Delegation

The mechanism for implementation of prototyping is called Delegation.[2]
Delegation is a process whereby an existing object dynamically searches for properties and methods first within itself, then if an match is not found, it delegates that search to its parent object. The parent then repeats the exact same delegation process until a match is found. Once a match is found the result is returned to the requester.
While delegation is simple in its design, we will see that it is also elegant. In that it opens the door for several different techniques which allow software developers to morph an existing object, into a much larger and more robust object. While still retaining the original objects simplicity.

Competing Approaches

Class Approach

New objects are made from predefined classes. A class definition must exist to create an instance of a class. When a class is changed, existing objects must be reconstructed and we also need to ensure that nothing else in the application requires the prior classes constructed behavior.

Prototype Approach

New objects are constructed by copying an existing object. After copying we are safe to change the class behavior knowing that we are independent of all other objects. Prototyping is dynamic allowing us to create a real world object as a single instance and then refactor it at run time.

  • We can add to it.
  • We can remove parts from it.
  • We can break parts out and move to a different location in the same delegation tree. Thereby extending the original behavior to more objects in the same object hierarchy.

Extensions

Some static class based computer languages use the term extension or method extension. [3] This is very similar in function to prototype.
However, extensions must exist before an object is instantiated and are therefore are still rigid in design. Extensions are static and limiting and are therefore lacking the flexibility that prototyping provides.
As we will see, some dynamic languages use prototyping to support dynamic inheritance and multi-inheritance. This is a level beyond the facilities that extensions provide.

Contrasting Styles

Class Development Style

  • Must think about objects abstractly and develop a model to simulate behavior.
  • Many people lack the ability to perform well when working with abstract thoughts.
  • Good communication is limited to those that can think abstractly.
  • Real world examples need to be broken down to a model and then reconstructed before they can be worked with.

Prototype Development Style

  • Most people can think clearly about objects that already exist.
  • Most people have the ability to perform well when working with real world objects.
  • Good communication is open to normal methods of everyday interaction.
  • Real world examples can be worked with in real time, there is no need to model behavior.

Analysis

Advantages of Prototype

  • Reduces development time.
  • Reduces development costs.
  • Developers receive quick results and can move on to the next task.

Disadvantages of Prototype

  • Can lead to insufficient analysis.
  • The performance may be degraded by prototype overhead.
  • Can cause systems to be left unfinished and/or implemented before they are ready.
  • Sometimes leads to incomplete documentation.

Real World Example

Model a bank account

Class Approach

  1. Start with use cases:
 As a customer I need to be able to view my balance.
 As a customer I need to be able to deposit money into my account.
 ...
 As a teller I need to be able to view the balances off all accounts for a given customer.
 As a teller I need to be able to see all tracnactions this customer has made in the past.
 ...
  1. Create a class diagram:

Prototype Approach

  Lets just build a back account object
     Account number    abc123
     balance    12
     Deposit(value) if(value>=0) balance += deposit else raise InvalidDepositError
     Withdrawl(value)  if(balance >=withdawal) balance-=withdrawl else raise InsufficientFundsError
     balance()  balance
  Now I can show it to other people and get their inputs.
     I show my account to Mary and she interacts with it.
     Mary says that she has several accounts and often transfers money between them.
     I add the following to my account object.
       transfer from(account, value)
       transfer to(account, value)
     I show the new account object to Bill who is a teller and bill says that he need to see all of the customers transactions because sometimes he is asked a question about a particular transaction.
     Bill also says that if management approves the dispute, he needs to be able to modify a prior transaction.
     I add the following to my account object:
        TransactionHistory [[date location amount],[date location amount],[date location amount]]     
        DisplayTransactionHistory()
        Add Transaction(datetime, location, amount)
        AdjustTransaction(datetime, location, new amount)
     I show the latest account object to Joe who works in the ATM industry.
     Joes says that he needs to be able to transfer amount between my account and an account on another computer automously.
     Joes also says that there is a daily withdrawl limit on all ATM withdrawls and that ATM balances are not updated until the they are verified.
     I add the following to my account object:
       PinNumber
       ATMWithdrawlLimit
       ATMWithdrawlTotal
       TransferAmountNotVerified
       MyIP
       MyPort
       transfer to(account, value, IP, Port)
       receive from(account, value, IP, Port)
      
     I show the latest account object to my friend Candi.
     Candi says that sometimes she would like to give her daughter a card that accesses her account for cash withdrawls but with a very low daily withdrawl limit.
       ATMWithdrawlLimits[[[Pin,Amount],[Pin,Amount],[Pin,Amount]]
UserNames[[Pin, Name], [Pin, Name], [Pin, Name]]
       UserName(Pin)
     Now I have an awesome account object, but I only have one.
     Gee I need a way to copy this object so that I can give accounts to all of my friends.
    
     I then add the following:
       Clone()
     Now I decide that I would like some accounts to maybe not be accessable via ATM. The problem is that my clone method was added last.
     I would really like everything to inherit from clone so that I can make a copy of any of the above extenstions and not carry all of the overhead.
     I used dynamic inheritance to move the clone method from the bottom of the extension tree to the very top. Now all of my extension can be cloned.


References

[1] Yahoo Online Dictionary - Prototype
[2] Using Prototypical Objects to Implement Shared Behavior in Object Oriented Systems By: Henry Lieberman
[3] Method extensions in C#
Prototype and Scriptaculous in Action By: Dave Crane; Bear Bibeault; Tom Locke; Thomas Fuchs ISBN-10: 1-933988-03-7
Self - The power of Simplicity By: David Ungar and Randall B - 1986