抽象工厂模式(Abstract Factory)

简单工厂

public class SendFactory {  

    public Sender produce(String type) {  

        if ("mail".equals(type)) {  

            return new MailSender();  

        } else if ("sms".equals(type)) {  

           return new SmsSender();  

        } else {  

            System.out.println("请输入正确的类型!");  

            return null;  

        }  

   }  

}  

 

public class SendFactory {      

   public Sender produceMail(){  

       return new MailSender();  

    }  

      

    public Sender produceSms(){  

       return new SmsSender();  

   }  

}

 

public class SendFactory {  

   public static Sender produceMail(){  

        return new MailSender();  

  }  

      

   public static Sender produceSms(){  

      return new SmsSender();  

  }  

}

 

 抽象工厂:

 public abstract class weapon

    {

        public string Name;

        public void Description()

        {

            Console.Write("我是一把" + this.Name + "\n");

        }

    }



    class Sword1 : weapon

    {

        public Sword1()

        {

            this.Name = "波刃剑";

        }

    }

    class Axe1 : weapon

    {

        public Axe1()

        {

            this.Name = "手斧";

        }

    }

    class Sword2 : weapon

    {

        public Sword2()

        {

            this.Name = "极光剑";

        }

    }

    class Axe2 : weapon

    {

        public Axe2()

        {

            this.Name = "水晶流星";

        }

    }



    //抽象工厂

    interface Factory

    {

         weapon CreateSword();

         weapon CreateAxe();

    }

    class weapon1 : Factory

    {

        public weapon CreateSword()

        {

            return new Sword1();

        }

        public weapon CreateAxe()

        {

            return new Axe1();

        }

    }

    class weapon2 : Factory

    {

        public weapon CreateSword()

        {

            return new Sword2();

        }

        public weapon CreateAxe()

        {

            return new Axe2();

        }

    }

        static int Main()//string[] args

        {

            weapon1 w1 = new weapon1();

            weapon2 w2 = new weapon2();

            w1.CreateSword().Description();

            w1.CreateAxe().Description();

            w2.CreateSword().Description();

            w2.CreateAxe().Description();



            Console.ReadLine();

            return 0;

        }

 

抽象工厂 中的 方法就是一个个工厂方法模式

public abstract class equip

    {

        public string Name;

        public void Description()

        {

            Console.Write("我是一把/件" + this.Name + "\n");

        }

    }



    class Sword1 : equip

    {

        public Sword1()

        {

            this.Name = "波刃剑";

        }

    }

    class Axe1 : equip

    {

        public Axe1()

        {

            this.Name = "手斧";

        }

    }

    class Sword2 : equip

    {

        public Sword2()

        {

            this.Name = "极光剑";

        }

    }

    class Axe2 : equip

    {

        public Axe2()

        {

            this.Name = "水晶流星";

        }

    }

    class armour1 : equip

    {

        public armour1()

        {

            this.Name = "皮铠";

        }

    }

    class armour2 : equip

    {

        public armour2()

        {

            this.Name = "白金铠";

        }

    }

    class gaiter1 : equip

    {

        public gaiter1()

        {

            this.Name = "皮护腿";

        }

    }

    class gaiter2 : equip

    {

        public gaiter2()

        {

            this.Name = "白金护腿";

        }

    }



    //抽象工厂

    interface Factory

    {

        equip CreateSword(string type);

        equip CreateArmour(string type);

    }

    class equip1 : Factory

    {

        public equip CreateSword(string type)

        {

            if (type == "Sword")

            {

                return new Sword1();

            }

            else

            {

                return new Axe1();

            }

        }

        public equip CreateArmour(string type)

        {

            if (type == "armour")

            {

                return new armour1();

            }

            else

            {

                return new gaiter1();

            }

        }

    }

    class equip2 : Factory

    {

        public equip CreateSword(string type)

        {

            if (type == "Sword")

            {

                return new Sword2();

            }

            else

            {

                return new Axe2();

            }

        }

        public equip CreateArmour(string type)

        {

            if (type == "armour")

            {

                return new armour2();

            }

            else

            {

                return new gaiter2();

            }

        }

    }





     static int Main()//string[] args

        {

            equip1 e1 = new equip1();

            equip2 e2 = new equip2();

            e1.CreateSword("Sword").Description();

            e1.CreateSword("Axe").Description();

            e1.CreateArmour("armour").Description();

            e1.CreateArmour("gaiter").Description();

            e2.CreateSword("Sword").Description();

            e2.CreateSword("Axe").Description();

            e2.CreateArmour("armour").Description();

            e2.CreateArmour("gaiter").Description();



            Console.ReadLine();

            return 0;

        }

 

你可能感兴趣的:(abstract)