开发者

What is the name of this pattern?

开发者 https://www.devze.com 2023-01-28 22:17 出处:网络
At first I called in the ISnapshotService, this is what it does CountrySnapshot snapshot = snapshotService.CreateSnapshot<Country, CountrySnapshot>(country);

At first I called in the ISnapshotService, this is what it does

CountrySnapshot snapshot = snapshotService.CreateSnapshot<Country, CountrySnapshot>(country);

For this to work Country must look like this

public class Country : ISnapshottable<CountrySnapshot>
{
  public CountrySnapshot CreateSnapshot(ISnapshotService snapshotService) { }
}

and CountrySnapshot must look like this

public class CountrySnapshot : ISnapshotFor<Country> { }

I used the name "Snapshot" because I initially intended this service to create immutable snapshots of classes for an end-of-period snapshot of a system for accounting purposes. However it has occurred to me that sometimes the result will not be an immutable snapshot, for example a StockL开发者_运维技巧ocation might create a StockAudit (which is not a snapshot.)

So, keeping in mind that this will no longer just create snapshots I need to come up with better names for these 3 interfaces.

  1. ISnapshotService
  2. ISnapshottable - Country implements this to indicate it can create a CountrySnapshot
  3. ISnapshotFor - CountrySnapshot implements this to show it is created from a Country

Any suggestion welcome. Thanks


This is a factory pattern. see http://en.wikipedia.org/wiki/Factory_method_pattern


Seems a lot like memento. Snapshot would be a Memento and Snapshottable would be an Originator that's outsourced its saving capabilities to a SnapshotService.

EDIT: Agreed, this is not memento, although the structure is similar. What it could be is a case of either Temporal Object or - what do you know - Snapshot.


I think it is more like a prototype pattern, because it creates new objects based on the state of an existing object.

I have gone for

  1. IPrototypeService
  2. IPrototype
  3. IPrototypedFrom
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号