Chủ Nhật, 5 tháng 5, 2013

Memento

1. Khái niệm

Memento pattern lưu lại trạng thái của đối tượng mà không vi phạm tính bao đóng. Memento pattern cung cấp cơ chế khôi phục lại trạng thái của đối tượng khi cần thiết.

2. Vấn đề thực tế

Mỗi người chúng ta hẳn ai cũng đã từng sử dụng một chương trình soạn thảo văn bản nào đó như WinWord, notepad, hay Writer (openoffice)...Như chúng ta đã biết, các chương trình soạn thảo văn bản đa phần đều hỗ trợ tính năng phục hồi thao tác vừa thực hiện, hay còn gọi là Undo. Tính năng Undo hỗ trợ người dùng phục hồi lại trạng thái của văn bản ở những lần trước đó. Đối với một máy tính, khi thực hiện nhiều phép tính, làm thế nào để có thể lưu lại các trạng thái của các số liệu đã được nhập vào để tham gia tính toán?

3. Giải pháp

Memento pattern sẽ giúp chúng ta giải quyết được vấn đề trên.

Memento pattern là mô hình giúp ứng dụng có thể lưu lại trạng thái của các object đã khởi tạo (hoặc có thể lưu cả object đó nếu cần thiết). Việc lưu trữ này cho phép ứng dụng có thể khôi phục lại trạng thái của object đó ở bất kỳ thời điểm nào trong danh sách lưu trữ. Memento giúp cho việc khôi phục trạng thái object trở nên dễ dàng hơn, tuy nhiên, chúng ta không nên lạm dụng vì nó sẽ chiếm nhiều tài nguyên bộ nhớ, nếu kiểm soát không cẩn thận có thể phát sinh ra các lỗi ngoài ý muốn trong quá trình xử lý vì thiếu bộ nhớ để lưu trữ và xử lý.

4. Mô hình




5. Code sample


public interface CalculatorMemento {
    public String getValue1();
    public String getValue2();
}

public interface CalculatorOriginator {
    public CalculatorMemento saveStateToMemento();
    public void getStateFromMemento(CalculatorMemento memento);
}

public class CalculatorMementoImpl implements CalculatorMemento{
    private String value1;
    private String value2;
    
    public CalculatorMementoImpl(String v1, String v2){
        value1 = v1;
        value2 = v2;
    }
    
    @Override
    public String getValue1() {
        return value1;
    }

    @Override
    public String getValue2() {
        return value2;
    }
    
}

public class CalculatorOriginatorImpl implements CalculatorMemento, CalculatorOriginator{
    private String value1;
    private String value2;
    
    public CalculatorOriginatorImpl(String v1, String v2){
        value1 = v1;
        value2 = v2;
    }
    
    @Override
    public CalculatorMemento saveStateToMemento(){
        return new CalculatorMementoImpl(value1, value2);
    }
    
    @Override
    public void getStateFromMemento(CalculatorMemento memento){
        value1 = memento.getValue1();
        value2 = memento.getValue2();
    }
    
    @Override
    public String getValue1() {
        return value1;
    }

    @Override
    public String getValue2() {
        return value2;
    }
}

public class CalculatorCareTaker {
    public static List<CalculatorMemento> list = new ArrayList<CalculatorMemento>();
    
    public void addMemento(CalculatorMemento memento){
        list.add(memento);
    }
    
    public CalculatorMemento getMemento(int index){
        return list.get(index);
    }
}

public class MementoPattern {
    public static void main(String[] args){
        CalculatorCareTaker caretaker = new CalculatorCareTaker();
        
        CalculatorOriginatorImpl calculator = new CalculatorOriginatorImpl("1", "2");
        caretaker.addMemento(calculator.saveStateToMemento());
        
        calculator = new CalculatorOriginatorImpl("3", "4");
        caretaker.addMemento(calculator.saveStateToMemento());
        
        calculator = new CalculatorOriginatorImpl("5", "6");
        caretaker.addMemento(calculator.saveStateToMemento());
        
        calculator = new CalculatorOriginatorImpl("7", "8");
        caretaker.addMemento(calculator.saveStateToMemento());
        
        System.out.println("Current calculator value1: " + calculator.getValue1());
        System.out.println("First calculator value1: " + caretaker.getMemento(0).getValue1());
        System.out.println("Second calculator value1: " + caretaker.getMemento(1).getValue1());
        
        
    }
}

6. Mối liên hệ với các pattern khác 

2 nhận xét:

  1. Tieti Mango & Habanero Pepper Jelly - Tieti's Tieti
    The Tieti Mango & Habanero titanium bracelet Pepper ford edge titanium 2019 Jelly is a unique titanium stud earrings & ecm titanium simple titanium belly button rings jelly created by Tieti. This jelly is made with fresh tomatoes, fresh mangoes and a thick

    Trả lờiXóa
  2. Your odds of hitting a number are now are|are actually} not 35/1 but 37/1, a big disadvantage. The desk more_bets is a version of the bets desk augmented by each pocket's winnings on the 0/00 break up. So out of 5,000 simulations, we would anticipate to see roughly $\frac \times 5000$, or 2,368, purple spins. Remote control by the central server each lowered this workforce whereas dashing up turnaround time from {several|a number of} days to instantaneous. This increased effectivity also permits casinos reduce back} danger and more precisely meet their day by day efficiency metrics, each large cash savers. For a slot machine, there 다 파벳 우회 주소 could be one hundred potential profitable outcomes.

    Trả lờiXóa