Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Customer[] customers = new Customer[100];
            customers[0] = new NormalCustomer();
            customers[1] = new NormalCustomer();
            customers[2] = new SubscriberCustomer();
            customers[3] = new NormalCustomer();
            customers[4] = new SubscriberCustomer();
            int normalCount     = 0;
            int subscriberCount = 0;

            foreach (Customer customer in customers)
            {
                if (customer != null)
                {
                    string customerName = customer.GetType().ToString();
                    // get name without namespace
                    customerName = customerName.Substring(customerName.LastIndexOf(".") + 1);
                    switch (customerName)
                    {
                    case "NormalCustomer":
                        normalCount++;
                        break;

                    case "SubscriberCustomer":
                        subscriberCount++;
                        break;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void TestMethod()
        {
            //普通客户
            Customer normalCustomer = new NormalCustomer();

            normalCustomer.Money = 100m;
            normalCustomer.SetChargeBehavior(new NormalChargeBehavior());
            normalCustomer.CustomerPay(normalCustomer.Money);

            //Vip客户
            Customer vipCustomer = new VipCustomer();

            normalCustomer.Money = 100m;
            vipCustomer.SetChargeBehavior(new VipChargeBehavior());
            vipCustomer.CustomerPay(normalCustomer.Money);
        }