CSC/ECE 517 Fall 2010/ch2 2c ck

From Expertiza_Wiki
Revision as of 02:11, 18 September 2010 by Jazzman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Prototype

Definition: 2. An original, full-scale, and usually working model of a new product or new version of an existing product. Citation: Yahoo Dictionary Online: http://education.yahoo.com/reference/dictionary/entry/prototype In this case the term prototype is a fully working model of new version of an existing class. This is synanomous with some computer languages use of the term extension or class extension. Origin: "... the prototype approach in some ways corresponds more closely to the way people seem to acquire knowledge from concrete situations" Citation Lieberman The prototype behaviour was first defined in the language Self. In Self all objects are prototypes. Main Difference between class based and prototpe based:

  Class based languages - New objects are made from classes. The class defintion must exist to create a class.
  When a class is changed existing objects must be reconstructed and we need to ensure that nothing needs the prior behaviour.
  Prototype based languages - New objects are constructed a new object by copying an existing object.
  After copying it we are safe to change it knowning that we are independent of all other objects.

Inheritence -

  Inheritence is performed at run time. It's dynamic.
  This allows us to create a real world object as a single instance and then refactor it and run time, by breaking parts out of it and then inheriting from the parts that we broke out.

Different development styles: Class Based:

  Must think about object abstractly and develope a model before experimenting with it.

Prototype based:


Example: Model a back account: Class approach:

     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 customer I need to be able to withdrawl from my account.
         As a customer I need to be able to transfer funds between my accounts.
         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.
         As a teller I need to be able to modify a transaction amount that a customer has proven is invalid.

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.

Advantages: Reduces development time. Reduces development costs. Developers receive quick results and can move on to the next task. Disadvantages: 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. References: 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 http://selflanguage.org Henry Lieberman Using Prototypical Objects to Implement Shared Behavior in Object Oriented Systems http://web.media.mit.edu/~lieber/Lieberary/OOP/Delegation/Delegation.html