AbstractFactory

public interface Animal {
/**所有动物都会吃*/
 public void eat();
}
public class Tiger implements Animal {
 public void eat() {
  System.out.println("老虎要吃饭!");
 }
 public void run() {
  System.out.println("老虎要跑步!");
 }
}
public class Parrot implements Animal {
 public void eat() {
  // TODO Auto-generated method stub
  System.out.println("鹦鹉要吃饭");
 }
 public void fly() {
  System.out.println("鹦鹉要飞翔");
 }
====================================

public interface AbstractFactory {
 Tiger createTiger();
 Dolphin createDolphin();
 Parrot createParrot();
}
==============================
public class AsiaTiger extends Tiger {
 public void run() {
  System.out.println("亚洲虎想跑步");
 }
 public void eat() {
  System.out.println("亚洲虎想吃饭");
 }
}
public class AsiaParrot extends Parrot{
 public void eat() {
  // TODO Auto-generated method stub
  System.out.println("亚洲鹦鹉要吃饭");
 }
 public void fly() {
  System.out.println("亚洲鹦鹉要飞翔");
 }
}
public class AsiaDophin extends Dolphin {
 public void swim() {
  System.out.println("亚洲海豚想跑步");
 }
 public void eat() {
  System.out.println("亚洲海豚想吃饭");
 }
}
public class AsiaFactory implements AbstractFactory {
 public Dolphin createDolphin() {
  // TODO Auto-generated method stub
  return new AsiaDophin();
 }
 public Parrot createParrot() {
  // TODO Auto-generated method stub
  return new AsiaParrot();
 }
 public Tiger createTiger() {
  // TODO Auto-generated method stub
  return new AsiaTiger();
 }
}
 
=====================================
public class AfricaTiger extends Tiger{
 public void run()
 {
  System.out.println("非洲虎想跑步");
 }
 public void eat()
 {
  System.out.println("非洲虎想吃饭");
 }
}
public class AfricaParrot extends Parrot{
 public void eat() {
  // TODO Auto-generated method stub
  System.out.println("非洲鹦鹉要吃饭");
 }
 public void fly() {
  System.out.println("非洲鹦鹉要飞翔");
 }
}
public class AfricaDophin extends Dolphin {
 public void swim() {
  System.out.println("非洲海豚想跑步");
 }
 public void eat() {
  System.out.println("非洲海豚想吃饭");
 }
}

public class AfricaFactory implements AbstractFactory {
 public Dolphin createDolphin() {
  // TODO Auto-generated method stub
  return new AfricaDophin();
 }
 public Parrot createParrot() {
  // TODO Auto-generated method stub
  return new AfricaParrot();
 }
 public Tiger createTiger() {
  // TODO Auto-generated method stub
  return new AfricaTiger();
 }
}
 
=====================================================
 
public class Client {
 /**
  * @param args
  * @throws IllegalAccessException
  * @throws InstantiationException
  * @throws ClassNotFoundException
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  AbstractFactory factory = new AfricaFactory();
  Tiger tiger = factory.createTiger();
  tiger.eat();
  tiger.run();
  Dolphin dolphin = factory.createDolphin();
  dolphin.eat();
  dolphin.swim();
  Parrot parrot = factory.createParrot();
  parrot.eat();
  parrot.fly();
  factory = new AsiaFactory();
  tiger = factory.createTiger();
  tiger.eat();
  tiger.run();
  dolphin = factory.createDolphin();
  dolphin.eat();
  dolphin.swim();
  parrot = factory.createParrot();
  parrot.eat();
  parrot.fly();
 }
}
定义:客户端要创建一些产品,而这些产品又归属不同产品类别,可以使用抽象工厂模式,抽象工厂类负责定义创建对象的接口,这一系列对象的创建工作由实现抽象工厂的具体工厂类来具体完成;
原理:四部分组成,抽象工厂类、实现抽象工厂类的具体类、抽象类、实现抽象类的具体类
 
优点:1、客户端不负责对象创建,把任务交给了具体的工厂类;2、当一系列相互关联的产品被设计到一个工厂类时候,客户端调用变得非常简单;
缺点:有新的产品加进来,需要修改抽象工厂类的设计

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值