Immutable views of mutable types

前端 未结 5 1555
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 10:14

I have a project where I need to construct a fair amount of configuration data before I can execute a process. During the configuration stage, it\'s very convenient to have

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 10:30

    The approach you describe works great if the "client" (the consumer of the interface) and the "server" (the provider of the class) have a mutual agreement that:

    • the client will be polite and not try to take advantage of the implementation details of the server
    • the server will be polite and not mutate the object after the client has a reference to it.

    If you do not have a good working relationship between the people writing the client and the people writing the server then things go pear-shaped quickly. A rude client can of course "cast away" the immutability by casting to the public Configuration type. A rude server can hand out an immutable view and then mutate the object when the client least expects it.

    A nice approach is to prevent the client from ever seeing the mutable type:

    public interface IReadOnly { ... }
    public abstract class Frobber : IReadOnly
    {
        private Frobber() {}
        public class sealed FrobBuilder
        {
            private bool valid = true;
            private RealFrobber real = new RealFrobber();
            public void Mutate(...) { if (!valid) throw ... }
            public IReadOnly Complete { valid = false; return real; }
        }
        private sealed class RealFrobber : Frobber { ... }
    }
    

    Now if you want to create and mutate a Frobber, you can make a Frobber.FrobBuilder. When you're done your mutations, you call Complete and get a read-only interface. (And then the builder becomes invalid.) Since all the mutability implementation details are hidden in a private nested class, you can't "cast away" the IReadOnly interface to RealFrobber, only to Frobber, which has no public methods!

    Nor can the hostile client create their own Frobber, because Frobber is abstract and has a private constructor. The only way to make a Frobber is via the builder.

提交回复
热议问题