Software design: Lecture 42 - Sheraz Pervaiz

19 5 0
Software design: Lecture 42 - Sheraz Pervaiz

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Software design - Lecture 42. The main topics covered in this chapter include: memento design pattern or souvenir design pattern; transactions in databases with undo operation and redo operation; concepts in memento originator; restoring an object – past status; roles in memento design pattern;...

1 Software Design Lecture : 42 Memento    or  Souvenir Design Pattern Transactions in Databases  Undo Operation Redo Operation Calculator Example Consider  the  case  of  a  calculator  object  with  an  undo  operation  such  a  calculator  could  simply  maintain  a  list  of  all  previous  operation  that  it  has  performed  and  thus  would  be  able  to  restore  a  previous  calculation  it  has  performed.  This  would  cause  the  calculator  object  to  become  larger,  more  complex,  and  heavyweight,  as  the  calculator  object  would  have  to  provide  additional undo  functionality  and  should  maintain  a  list  of  all  previous  operations Memento Design Pattern  It is sometimes necessary to capture the internal state of  an  object  at  some  point  and  have  the  ability  to  restore  the object to that state later in time  The state of an object can be defined as the values of its  properties or attributes at any given point of time 6 Motivation  The  Memento  pattern  is  useful  for  designing  a  mechanism to capture and store the state of an object so  that  subsequently,  when  needed,  the  object  can  be  put  back to this (previous) state  This is more like an  “undo operation” Motivation The Memento pattern can be used to accomplish  this  without  exposing  the  object’s  internal  structure Memento  The originator stores all those attributes that are  required for restoring its state in a separate    object referred to as a Memento and returns it to  the client Thus a Memento can be viewed as an object that  contains the internal state of another object, at a     given point of time Concepts in Memento: Originator • The  object  whose  state  needs  to  be  captured  is  referred to as the originator • When  a  client  wants  to  save  the  state  of  the  originator,  it  requests  the  current  state  from  the  originator.  10 Memento Object A  Memento  object  must  hide  the  originator  variable  values  from  all  objects  except  the  originator In other words, it should protect its internal       state  against  access  by  objects  other  than  the  originator 11 Memento  Towards  this  end,  a  Memento  should  be  designed  to  provide  restricted  access  to  other  objects  while  the  originator  is  allowed  to  access  its internal state 12 Restoring an Object – Past  Status When  the  client  wants  to  restore  the  originator  back  to  its  previous  state,  it  simply  passes  the  memento  back  to  the  originator.  The  originator  uses  the  state  information  contained  in  the  memento and puts itself back to the state stored  in the Memento object 13 Roles in Memento Design  Pattern Originator ­ the object that knows how to  save itself Caretaker ­ the object that knows why and  when the Originator needs to save and  restore itself Memento ­ the lock box that is written and read  by the Originator, and shepherd by  the Caretaker 14 Class Diagram of Memento 15 16 • Memento ▫Stores internal state of the Originator object The state can include any number of state variables ▫The Memento must have two interfaces, an interface to the caretaker This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state • Caretaker 17 Originator  Creates a memento object capturing the originators internal state  Use the memento object to restore its previous state Caretaker  Responsible for keeping the memento  The memento is opaque to the caretaker, and the 18 Processing Logic  A Caretaker would like to perform an operation on the Originator while having the possibility to rollback The caretaker calls the create Memento() method on the originator asking the originator to pass it a memento object At this point the originator creates a memento object saving its internal state and passes the memento to the caretaker The caretaker maintains the memento object and performs the operation In case of the need to undo the operation, the caretaker calls the set Memento() method on the originator passing the maintained memento object The originator would 19 Example

Ngày đăng: 05/07/2022, 14:10

Mục lục

    Memento or Souvenir Design Pattern

    Concepts in Memento: Originator

    Restoring an Object – Past Status

    Roles in Memento Design Pattern

    Class Diagram of Memento