模板模式

Template.java

Java代码 复制代码
  1. public abstract class Template   
  2. {   
  3.     public void fitment(){   
  4.         decorate();   
  5.         floor();   
  6.         paint();   
  7.     }   
  8.   
  9.     public abstract void floor();   
  10.   
  11.     public abstract void paint();   
  12.   
  13.     public abstract void decorate();   
  14. };  
public abstract class Template
{
	public void fitment(){
		decorate();
		floor();
		paint();
	}

	public abstract void floor();

	public abstract void paint();

	public abstract void decorate();
};



Team1.java

Java代码 复制代码
  1. public class Team1 extends Template   
  2. {   
  3.     public void floor(){   
  4.         System.out.println("Team1-----floor");   
  5.     }   
  6.   
  7.     public void decorate(){   
  8.         System.out.println("Team1-----decorate");   
  9.     }   
  10.   
  11.     public void paint(){   
  12.         System.out.println("Team1-----paint");   
  13.     }   
  14. };  
public class Team1 extends Template
{
	public void floor(){
		System.out.println("Team1-----floor");
	}

	public void decorate(){
		System.out.println("Team1-----decorate");
	}

	public void paint(){
		System.out.println("Team1-----paint");
	}
};



Team2.java

Java代码 复制代码
  1. public class Team2 extends Template   
  2. {   
  3.     public void floor(){   
  4.         System.out.println("Team2-----floor");   
  5.     }   
  6.   
  7.     public void decorate(){   
  8.         System.out.println("Team2-----decorate");   
  9.     }   
  10.   
  11.     public void paint(){   
  12.         System.out.println("Team2-----paint");   
  13.     }   
  14. };  
public class Team2 extends Template
{
	public void floor(){
		System.out.println("Team2-----floor");
	}

	public void decorate(){
		System.out.println("Team2-----decorate");
	}

	public void paint(){
		System.out.println("Team2-----paint");
	}
};



Main.java

Java代码 复制代码
  1. public class Main   
  2. {   
  3.     public static void main(String[] args){   
  4.         Template t = new Team1();   
  5.         t.fitment();   
  6.         Template t2 = new Team2();   
  7.         t2.fitment();   
  8.     }   
  9. };  

你可能感兴趣的:(java)