What is a useful pattern to maintaining an object state in a one to many relationship?

I am looking for a design for my application, here are the players(classes) involved.

struct Transform {
    // Uses a matrix to transform the position.
    // Also acts acts as the state of a Dialog.
    Position transform(Position p);

    //other methods.
};

struct Dialog {
     // There are multiple dialog for the user to transform the output.

     Transform& t;
     void ChangeTranformation(){t.rotate(360);}
}

struct Algorithm {
    //gives us a position based on an implementation. For example this can return points on a circle or line.

    Transform& t;
    Position m_p;
    Dialog& d;
    Position GetCurrentPosition(){ return t.transform(m_p);}

    //other methods.
}

Properties I need:

  1. Each algorithms has one dialog and each dialog can have many algorithms associated with it.
  2. When the user selects an algorithm a dialog associated with that algorithm is displayed.
  3. If the user selects a different algorithm then re-selects back the state is restored in the dialog.

Basically I want a good design pattern to maintain the state of the dialog given that many algorithms use it and they can be switched back and forth. Does anyone have any suggestions?


Here is a use case:

Dialog1 has a single edit box to control the radius.

Algorithm1 generates points on a unit circle.

Algorithm2 is the same as Algorithm1.

The user has selected Algorithm1 and entered 2 into the edit box. This will generate points on a circle of radius 2.

The user then selects Algorithm2 and enters 10 into the edit box of Dialog1. This will generate points on a circle of radius 10.

Finally Algorithm1 is selected again. The edit box of Dialog1 should show 2 and points on a circle of radius 2 should be generated.

11

You don’t need fancy patterns. You need to separate the model from the user interface. The algorithms and their current input data form the model.

The dialogs are part of the user interface. When an algorithm is selected, the corresponding dialog is displayed, showing the current data for the algorithm. If the user changes the data, that change is passed to the algorithm.

All you need is a one-to-one relationship between algorithms and their input data, and a many-to-one relationship between algorithms and dialogs.

2

Basically I want a good design pattern to maintain the state of the
dialog given that many algorithms use it and they can be switched back
and forth.

Sounds like a job for Memento.

The memento pattern is a software design pattern that provides the
ability to restore an object to its previous state…

You’re trying to associate the state of a Dialog with an Algorithm. As the current algorithm is being removed, have the current it request a memento from the dialog. The memento is an opaque (from the algorithm’s perspective) object that encodes the dialog’s state. When the newly selected algorithm is added to the window, it gives the memento that it had previously requested (if one exists) to the dialog, and the dialog uses it to configure itself.

That said, preserving the state of the dialog is doing things backward. The purpose of a dialog box is to let the user see, and perhaps modify, the data that the program operates on. In your case, it sounds like the algorithms are part of that. When the user changes something in the dialog box, that should cause a corresponding change in the current algorithm, and said change should be stored in someplace other than the controls of the dialog box. If you organize your program that way you can let the selection of the algorithm drive the content of the dialog. This is exactly how you’d do it if you use the Model-View-Controller paradigm (or one of it’s cousins). If the user changes something in the dialog, that change modifies the algorithm (i.e. the model). If the user selects a different algorithm, the current state of the new algorithm is used to set the values in the dialog.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật