java object oriented application interface 22: factory mode

/*

  • Application interface: factory mode
  • 1 factory pattern: to achieve a separation of the creator of the caller, the specific process is about creating an object shield isolated, achieve greater flexibility.
  • Instead of new plant manipulation method. We will choose to implement the class, create objects unified management and control. Thereby decoupling the caller with our implementation class.
  • 2 factory pattern classification:
  • 2.1 Simple factory pattern: any product used to produce the same level structure.
  • Simple static factory pattern is also called factory pattern is generally factory class using the static method, returns to different instances with different parameters of the object received.
  • As long as the caller knows what he wants, where to get, do not need to know how to create, the caller and the creator of separation.
  • Cons: For adding new products, without modifying the code, it can not be extended. Violation of the principles of open and closed (open to extension; closed for modification)
  • 2.2 Factory Method mode: the same hierarchical structure for producing a fixed product. (Add any product support)
  • Different methods largest factory mode and the simple mode in that factory, only a simple factory mode (for a project or a separate module concerned) factory class,
  • And a set of factory model factory class implements the same interface.
  • 2.3 abstract factory pattern: for all the different products of the product family. (For adding new products, do nothing; support increased product family)
  • The difference between abstract factory pattern and factory method pattern is that the complexity of the need to create objects on. And the abstract factory pattern is three inside the most abstract, most general.
  • Abstract factory pattern intended to: provide an interface to the client, you can create multiple product family products subject
  • 3 factory pattern design principles applied
  • 3.1OCP (closing principle, Open-Closed Principle)
  • A software entity should be open for extension, but closed for modification. To address the changes in the new code by way of demand, changes in demand for risk control, reduced maintenance costs
  • 3.2DIP (Dependency Inversion principle, Dependence Inversion Principle)
  • To an interface for programming, not for the realization of programming. Abstract-oriented programming, decoupling calling and caller.
  • If the association B A, then try to implement an interface such that B, then a relationship with the interface A, are not associated with the implementation class B
  • 3.3LOD (Demeter, Law Of Demeter)
  • Possible packaging requirements, as far as possible independent, as far as possible the use of low-level access modifier. This is reflected in the typical package characteristics
  • Direct link between Dmitry principle requires fewer classes as much as possible, access to two classes to achieve through an intermediary class

*/

package com.atguigu.contact;

public class Object_InterfaceFactory {
	public static void main(String[] args) {
		
		//无工厂模式直接造对象
		AirPlane helicopter = new Helicopter();
		AirPlane fighter = new Fighter();
		helicopter.fly();
		fighter.fly();
		System.out.println("----------------");
		//简单工厂模式,通过简单工厂类获取对象
		AirPlaneFactory airPlaneFactory = new AirPlaneFactory();
		AirPlane a = airPlaneFactory.getAirPlane("helicopter");
		AirPlane b = airPlaneFactory.getAirPlane("fighter");
		AirPlane c = airPlaneFactory.getAirPlane(null);//空指针,无法调取fly()方法
		a.fly();
		b.fly();
	  //c.fly();//空指针,无法调取fly()方法
		System.out.println("----------------");
		//工厂模式,通过专门的工厂类造对象
		AirPlane d = new HelicopterFactory().getAirPlane();
		AirPlane e = new FighterFactory().getAirPlane();
		d.fly();
		e.fly();
		System.out.println("----------------");
		
		
	}
}

interface AirPlane{
	void fly();
}

class Helicopter implements AirPlane{
	public void fly() {
		System.out.println("直升机可以悬停");
	}
}

class Fighter implements AirPlane {
	public void fly() {
		System.out.println("战斗机可以空战");
	}
}
//简单工厂类
class AirPlaneFactory{
	AirPlane getAirPlane(String s) {
		if(s == "helicopter") {
			return new Helicopter();
		}else if(s == "fighter") {
			return new Fighter();
		}else {
			return null;
		}
	}
}

//工厂接口

interface Factory{
	public abstract AirPlane getAirPlane();
}

//单独的直升机工厂
class HelicopterFactory implements Factory{

	@Override
	public AirPlane getAirPlane() {
		// TODO Auto-generated method stub
		return new Helicopter();
	}
	
}
//单独的战斗机工厂
class FighterFactory implements Factory{

	@Override
	public AirPlane getAirPlane() {
		// TODO Auto-generated method stub
		return new Fighter();
	}
	
}
Published 47 original articles · won praise 1 · views 1048

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/104427276