java ---- interfaces and common design patterns

interface

  It can be understood as a special kind of abstract class. Use interface keyword defines an interface.
  Abstract class: is a thing with the functions.
  Interface: It is a thing that has extra features.
  The choice between them, the interface is recommended, if the function of the parent class has member functions to achieve, there are only a function definition function can have subclasses, but not implemented, can abstract class.

Interface advantages:

  1. Improve the reusability of code.
  2. Decoupling.
  3. Custom programming specifications.
  4. Multi-Interface implementation.

In jdk1.7 and before:
  Attributes: Use public static final public static constant modification.
    Modifications can not write, but there is a default.
    eg: public static final int a = 5;
    and int a = 5; are the same.
  Methods: public abstract public abstract method modification.
    Modifications can not write, but there is a default.
    eg: public abstract void haha () ;
    and int hehe (); it is abstract.
  note:

  1. Interfaces can not be instantiated.
  2. The interface need to implement, implement the interface using the keyword implements, implements the interface to have the interface functions (much like the inheritance).
  3. Interface using:
    1) a specific category: implements the interface, it is necessary to rewrite all the abstract methods in this interface + new demand.
    2) implementation of the abstract class: abstract features + rewritable portion added on demand.
  4. Class can only implement the interface, the interface can not inherit, but the class can only inherit class, can not be achieved Class A, single inheritance class, the class that implements the interface and more.
  5. The interface and implementation class can not inherit, the interface can only inherit interface can not implement an interface, the interface can be multiple-inheritance interfaces.
  6. Achieve a first class after inheritance.

jdk1.8 new features:

  1. Static methods
    Use: the interface can only be called by name
  2. The default method of
    use: Use only by implementing object class

Common Design Patterns

Singleton : a guarantee that a class can only build instance.
  Hungry Chinese-style: when you create an instance of the class is first loaded - the thread-safe
  lazy type: create an instance when the first call to the function - thread-safe, high efficiency
  implementation steps:

  1. Constructor privatization
  2. Reference private static class
  3. Public static access methods
//饿汉式单例
public class Single {
	//2.私有的静态的该类的引用
	private static Single single=new Single();
	//1.构造器私有化
	private Single(){  //不允许随意再外部创建对象
	}
	//3.公共的静态的访问方式
	public static Single newInstance(){
		return single;
	}
	//懒汉式单例
	public class SingleTon {
	//2.私有的静态的该类的引用
	private static SingleTon single=null;
	//1.私有构造
	private SingleTon(){}
	//3.公共静态的访问方式
	//A B C
	/*public  static SingleTon newInstance(){
		//A B C
		if(single==null){
			// A B C
			single=new SingleTon();
		}
		return single;
	}*/
	//锁方法
	/*public synchronized static SingleTon newInstance(){
		//A B C
		if(single==null){
			// A B C
			single=new SingleTon();
		}
		return single;
	}*/
	//锁代码块
	public static SingleTon newInstance(){
		//A B C
		synchronized (SingleTon.class) {
			if(single==null){
				// A B C
				single=new SingleTon();
			}
		}
		return single;
	}
}
	public class Test {//测试类
	public static void main(String[] args) {
		Single single1=Single.newInstance();
		Single single2=Single.newInstance();
		System.out.println(single1==single2);  //true
		
		SingleTon s1=SingleTon.newInstance();
		SingleTon s2=SingleTon.newInstance();
		System.out.println(s1==s2);            //true
	}
}

Static proxy mode

  • Real Role: Project Manager
  • Acting Role: hr
  • Agent behavior

Features proxy mode:
  1. real role and the role of agents implement the same interface | parent
  2. agent role holds true character references
  3. Agent Behavior

public class StaticProxy {
	public static void main(String[] args) {
		UserManager UserManager=new UserManager();
		Hr hr=new Hr(UserManager);
		hr.addUser();
	}
}
//相同的接口
interface AddUser{
	void addUser();
}
//真实角色:项目经理
class UserManager implements AddUser{
	@Override
	public void addUser() {
		System.out.println("项目经理录用人");
	}
}
//代理角色:hr
class Hr implements AddUser{
	//代理角色持有真实角色的引用
	private UserManager manager;
	public Hr() {
		// TODO Auto-generated constructor stub
	}
	public Hr(UserManager manager) {
		super();
		this.manager = manager;
	}
	@Override
	public void addUser() {
		System.out.println("------筛选简历---------");
		System.out.println("------通知面试---------");
		manager.addUser();
		System.out.println("---------------------");
	}
}

Simple factory pattern
  abstract Product role: role of specific products implement interfaces | inherits the parent class.

public class Factory {
	public static void main(String[] args) {
		//接口多态  接口指向不同的实现类
		Car car=factory("BMW");
		car.run();
	}
	//工厂
	public static Car factory(String type){
		Car car=null;
		if("Mes".equals(type)){ //比较字符串的内容使用equals方法
			car=new Mes();
		}else{
			car=new BMW();
		}
		return car;
	}
}
//抽象产品角色
interface Car{
	void run();
}
//具体产品角色
class BMW implements Car{
	@Override
	public void run() {
		System.out.println("坐在..哭");
	}
}
class Mes implements Car{
	@Override
	public void run() {
		System.out.println("坐在引擎盖上哭...");
	}
}

Guess you like

Origin blog.csdn.net/qq_41899248/article/details/90809677