コード例 #1
0
        public static void TestAdapter()
        {
            // Round fits round, no surprise.
            RoundHole hole = new RoundHole(5);
            RoundPeg  rpeg = new RoundPeg(5);

            if (hole.Fits(rpeg))
            {
                Console.WriteLine("Round peg r5 fits round hole r5.");
            }

            SquarePeg smallSqPeg = new SquarePeg(2);
            SquarePeg largeSqPeg = new SquarePeg(20);
            // hole.fits(smallSqPeg); // Won't compile.

            // Adapter solves the problem.
            SquarePegAdapter smallSqPegAdapter = new SquarePegAdapter(smallSqPeg);
            SquarePegAdapter largeSqPegAdapter = new SquarePegAdapter(largeSqPeg);

            if (hole.Fits(smallSqPegAdapter))
            {
                Console.WriteLine("Square peg w2 fits round hole r5.");
            }
            if (!hole.Fits(largeSqPegAdapter))
            {
                Console.WriteLine("Square peg w20 does not fit into round hole r5.");
            }
        }
コード例 #2
0
 public SquarePegAdapter(SquarePeg peg)
 {
     Peg = peg;
 }