工厂模式

包含简单工厂模式、工厂模式和抽象工厂模式。

比较常见的实例化对象模式。


此处我是用例子实现几个模式方便理解。

简单工厂模式:

保险理赔包含几个基本类型:驾驶员身体受伤、汽车损坏、人员伤亡、多种事故。使用简单工厂方法模式设计并实现一个可以创建不同保险理赔对象的应用。


代码:

Insurance:

 packagecn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain;

public abstract class Insurance {

    public abstract String display();

}


BodyInjure:

packagecn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain;

public class BodyInjure extends Insurance {

//人员伤亡

    @Override

    public String display() {

        return "发生人员伤亡事件";  }

}


CarDamage:

packagecn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain;

public class CarDamage extends Insurance{

 @Override

    public String display() {

        return "汽车损坏了!";

 }}


DriverInjury:

packagecn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain;

public class DriverInjury extends Insurance{

@Override

    public String display() {

        return "驾驶员身体受伤了";

 }

}


KindsOfAccident:

packagecn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain;

public class KindsOfAccident extends Insurance{

@Override    public String display() {

        return "发生多种事故";}

}


InsuranceFactory:

packagecn.edu.scau.cmi.xuxiaojia.SimpleFactory.domainFactory;

importcn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.BodyInjure;

importcn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.CarDamage;

importcn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.DriverInjury;

importcn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.Insurance;

importcn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.KindsOfAccident;

public classInsuranceFactory {

    public Insurance getInsuranceType(StringinsuranceType) {

        Insurance insurance=null;

        if(insuranceType.compareTo("BodyInjure")==0){

           return new BodyInjure();

        }elseif(insuranceType.compareTo("CarDamage")==0) {

           return new CarDamage();

        }elseif(insuranceType.compareTo("DriverInjury")==0) {

           return new DriverInjury();

        }else {

           return new KindsOfAccident();  }

   }

}


Client类:

packagecn.edu.scau.cmi.xuxiaojia.SimpleFactory.client;

importcn.edu.scau.cmi.xuxiaojia.SimpleFactory.domainFactory.InsuranceFactory;

public classClient {

public static void main(String[] args) {

        // TODO Auto-generated method stub

        Strings=InsuranceFactory.getInsuranceType("BodyInjure").display();

        System.out.println(s);

   }

}


工厂模式:

实例题目类似简单工厂模式,实体类也不变就不贴了。

下面是工厂类:

Factory:

package cn.edu.scau.cmi.xuxiaojia.Factory.domainFactory;

import cn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.Insurance;

public abstract class Factory {

 public abstract Insurance Manufacture();

}


BodyInjureFactory:

package cn.edu.scau.cmi.xuxiaojia.Factory.domainFactory;

import cn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.BodyInjure;

import cn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.Insurance;

public class BodyInjureFactory extends Factory{

 @Override

 public Insurance Manufacture() {

 return new BodyInjure();

 }}


CarDamageFactory:

package cn.edu.scau.cmi.xuxiaojia.Factory.domainFactory;

import cn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.CarDamage;

import cn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.Insurance;

public class CarDamageFactory extends Factory{

@Override

 public Insurance Manufacture() {

 return new CarDamage();

 }

}


DriverInjureFactory:

package cn.edu.scau.cmi.xuxiaojia.Factory.domainFactory;

import cn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.DriverInjury;

import cn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.Insurance;

public class DriverInjureFactory extends Factory{

 @Override

 public Insurance Manufacture() {

 return new DriverInjury();

 }

}


KindsOfAccidentFactory:

package cn.edu.scau.cmi.xuxiaojia.Factory.domainFactory;

import cn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.Insurance;

import cn.edu.scau.cmi.xuxiaojia.SimpleFactory.domain.KindsOfAccident;

public class KindsOfAccidentFactory extends Factory{

 @Override

 public Insurance Manufacture() {

 return new KindsOfAccident();

 }

}


Client类:

package cn.edu.scau.cmi.xuxiaojia.Factory.client;

import cn.edu.scau.cmi.xuxiaojia.Factory.domainFactory.Factory;

public class Client {

 public static void main(String[] args) {

 String s=Factory.getInsuranceType("BodyInjure").Manufacture().display();

 System.out.println(s);

 }

}


抽象工厂模式:

现在有两个运营公司,一个是苹果公司,一个是小米公司,两个公司都有手机、电脑和智能手表三种产品。


Phone接口:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface;

public interface Phone {

 abstract String getDeclaration();

}


Watch接口:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface;

public interface Watch {

  abstract String getDeclaration();

}


Computer接口:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface;

public interface Computer {

  abstract String getDeclaration();

}



IMac类:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Computer;

public class IMac implements Computer {

 @Override

 public String getDeclaration() {

 // TODO Auto-generated method stub

 return "大家好,我是苹果电脑";

 }}


IPhone类:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Phone;

public class Iphone implements Phone {

 public String getDeclaration() {

        return "大家好,我是苹果手机";

 }

}


Iwatch类:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Watch;

public class IWatch implements Watch {

 @Override

 public String getDeclaration() {

 // TODO Auto-generated method stub

 return "大家好,我是苹果手表";

 }

}


MiComputer类:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Computer;

public class MiComputer implements Computer {

@Override

 public String getDeclaration() {

 // TODO Auto-generated method stub

 return "大家好,我是小米电脑";

 }

}


MiPhone类:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Phone;

public class MiPhone implements Phone {

 @Override

 public String getDeclaration() {

 // TODO Auto-generated method stub

 return "大家好,我是小米手机";

 }

}


MiWatch类:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Watch;

public class MiWatch implements Watch {

 @Override

 public String getDeclaration() {

 // TODO Auto-generated method stub

 return "大家好,我是小米手表";

 }

}


AbstractFactory:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.factory;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Computer;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Phone;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Watch;

public abstract class AbstractFactory {

 public static AbstractFactory getFactory(String brand) {

  AbstractFactory abFactory=null;

 if(brand=="Apple") {

 abFactory=new IFactory();

 }else {

 abFactory=new MiFactory();

 }

  return abFactory;

 }

 public abstract Phone createPhone();

 public abstract Computer createComputer();

 public abstract Watch createWatch();

}


IFactory类:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.factory;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain.IMac;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain.IWatch;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain.Iphone;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Computer;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Phone;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Watch;

public class IFactory extends AbstractFactory{

 @Override

 public Phone createPhone() {

 // TODO Auto-generated method stub

 return new Iphone();

 }

 @Override

 public Computer createComputer() {

 // TODO Auto-generated method stub

 return new IMac();

 }

 public Watch createWatch() {

 return new IWatch();

 }

}


MiFactory类:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.factory;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain.MiComputer;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain.MiPhone;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domain.MiWatch;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Computer;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Phone;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Watch;

public class MiFactory extends AbstractFactory {

 @Override

 public Phone createPhone() {

 // TODO Auto-generated method stub

 return new MiPhone();

 }

 @Override

 public Computer createComputer() {

 // TODO Auto-generated method stub

 return new MiComputer();

 }


 @Override

 public Watch createWatch() {

 // TODO Auto-generated method stub

 return new MiWatch();

 }

}


Client类:

package cn.edu.scau.cmi.xuxiaojia.AbstractFactory.client;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.domainInterface.Phone;

import cn.edu.scau.cmi.xuxiaojia.AbstractFactory.factory.AbstractFactory;

public class Client {

 public static void main(String[] args) {

 // TODO Auto-generated method stub

 Phone phone=AbstractFactory.getFactory("Apple").createPhone();

 String declaration=phone.getDeclaration();

 System.out.println(declaration);

 }

}

你可能感兴趣的:(工厂模式)