标签:tco 封装 单一职责 equal 抽象工厂 依赖 param creat 特定
简单工厂模式用于实现逻辑的简单封装,并通过公共的方法提供对象的实例化服务,在添加新的类时,只需要做少量的修改。
/**
* 车辆(抽象类)
*/
public abstract class Vehicle {
abstract public void run();
}
/**
* 小汽车具体类
*/
public class Car extends Vehicle {
@Override
public void run() {
System.out.println("小汽车在公路上呼啸而过");
}
}
/**
* 自行车具体类
*/
public class Bike extends Vehicle {
@Override
public void run() {
System.out.println("自行车跑的慢");
}
}
public enum VehicleType {
Bike,Car
}
/**
* 车辆工厂类
*/
public class VehicleFactory {
/**
* 静态工厂模式
*
* 工厂只负责Vehicle类的实例化,符合单一职责原则。
* 用户只调用Vehicle接口,减少耦合,符合依赖倒置原则。
* @param type
* @return
*/
public static Vehicle create(VehicleType type){
if (type.equals(VehicleType.Bike)){
return new Bike();
}
if (type.equals(VehicleType.Car)){
return new Car();
}
else return null;
}
}
public class Client {
public static void main(String[] args) throws Exception {
Vehicle bike = VehicleFactory.create(VehicleType.Bike);
bike.run();//自行车跑的慢
Vehicle car = VehicleFactory.create(VehicleType.Car);
car.run();//小汽车在公路上呼啸而过
}
}
工厂类逻辑简单,只负责Vehicle类的实例化,符合单一职责原则;
用户只调用Vehicle接口,减少耦合,符合依赖倒置的原则;
但是当增加一种新类的时候,需要对VehicleFactory工厂类进行修改,这样就打破了开闭原
则。解决这个问题可以在每个产品类中添加newInstance()方法来解决,该方法返回与自身
类型相同的新实例。
工程方法模式是在静态工厂模式上的改进,工厂类被实例化。用于实例化特定产品类的代码被转移到实现抽象方法的子类中。这样就不需要修改就可以扩展工厂类。
/**
* 车辆(抽象类)
*/
public abstract class Vehicle {
/**
* 车辆运行
*/
public abstract void running();
}
/**
* 汽车类
*/
public class Car extends Vehicle {
@Override
public void running() {
System.out.println("小汽车开始运行");
}
}
/**
* 卡车类
*/
public class Truck extends Vehicle {
@Override
public void running() {
System.out.println("卡车开始运行");
}
}
/**
* 车辆抽象工厂
*/
public abstract class VehicleFactory {
/**
* 创建车辆
* @return
*/
protected abstract Vehicle createVehicle();
}
/**
* 汽车具体工厂类
*/
public class CarFactory extends VehicleFactory {
@Override
protected Vehicle createVehicle() {
return new Car();
}
}
/**
* 卡车工厂类
*/
public class TruckFactory extends VehicleFactory {
@Override
protected Vehicle createVehicle() {
return new Truck();
}
}
public class Client {
public static void main(String[] args) {
VehicleFactory carFactory = new CarFactory();
Vehicle car = carFactory.createVehicle();
car.running();
VehicleFactory truckFactory = new TruckFactory();
Vehicle truck = truckFactory.createVehicle();
truck.running();
}
}
抽象工厂模式是简单工厂模式的扩展版本。
它不再是创建单一类型的对象,而是创建一系列相关的对象。
如果说工厂方法中只包含一个抽象产品类,那么抽象工厂模式中则包含多个抽象产品类。
/**
* 车辆抽象类
*/
public abstract class Vehicle {
String color;
//abstract Vehicle class
public void testVehicle() {
// implementation here
}
public void setColor(String color) {
this.color=color;
}
public abstract void running();
}
/**
* 汽车抽象类
*/
public abstract class Car extends Vehicle {
}
/**
* 小轿车具体类
*/
public class SedanCar extends Car {
@Override
public void running() {
System.out.println("小轿车开始跑");
}
}
/**
* 跑车具体类
*/
public class SportCar extends Car {
@Override
public void running() {
System.out.println("跑车开始飞驰");
}
}
/**
* 卡车抽象类
*/
public abstract class Truck extends Vehicle {
}
/**
* 小卡车具体类
*/
public class SmallTruck extends Truck {
@Override
public void running() {
System.out.println("小卡车慢慢的跑");
}
}
/**
* 大卡车具体类
*/
public class LargeTruck extends Truck {
@Override
public void running() {
System.out.println("大卡车平稳的跑");
}
}
/**
* 抽象工厂类
*/
public abstract class VehicleFactory {
protected abstract Vehicle createVehicle(String item);
public Vehicle orderVehicle(String size, String color)
{
Vehicle vehicle = createVehicle(size);
vehicle.testVehicle();
vehicle.setColor(color);
return vehicle;
}
}
/**
* 汽车工厂
*/
public class CarFactory extends VehicleFactory {
@Override
protected Vehicle createVehicle(String size) {
if (size.equals("small"))
return new SportCar();
else if (size.equals("large"))
return new SedanCar();
return null;
}
}
/**
* 卡车工厂
*/
public class TruckFactory extends VehicleFactory {
@Override
protected Vehicle createVehicle(String size) {
if (size.equals("small"))
return new SmallTruck();
else if (size.equals("large"))
return new LargeTruck();
return null;
}
}
public class Client {
public static void main (String s[]) {
VehicleFactory carFactory = new CarFactory();
Vehicle car = carFactory.orderVehicle("small", "blue");
System.out.println("car.color:" + car.color);
car.running();
VehicleFactory truckFactory = new TruckFactory();
Vehicle truck = truckFactory.orderVehicle("large", "yellow");
System.out.println("truck.color:" + truck.color);
truck.running();
}
}
标签:tco 封装 单一职责 equal 抽象工厂 依赖 param creat 特定
原文地址:https://www.cnblogs.com/beanbag/p/13168501.html