First, adapter design mode
In simple terms, it is an indirect class that selectively overwrite an interface.
Interface window{public void Open ()//open window public void close ()//Close window public void icon ();//Minimize public void Unicon ();// Maximized}abstract class Windowadapter implements window{public Void Open () {}public void Close () {}public void icon () {}public vo ID Unicon () {}};class Mywindow extends windowadapter{public void open () {System.out.println ("open window! ") ;}}; public class Adpaterdemo{public static void Main (String args[]) {Window win = new Mywindow (); Win.open ();}}
second, the factory design mode
Design a choice to eat oranges or apples, the general design may be directly instantiated in the main class object, but through the factory design pattern through an indirect class can reduce the main class (client) of the code amount
Interface fruit{public void Eat ();} Class Apple implements Fruit{public void Eat () {System.out.println ("eat apples ... ") ;}}; Class Orange implements Fruit{public void Eat () {System.out.println ("eat oranges ... ") ;}}; Class factory{//factory class public static Fruit Getfruit (String className) {Fruit F = null, if ("Apple". Equals (ClassName)) {f = new A Pple ();} if ("Orange". Equals (ClassName)) {f = new orange ();} return f;}; public class Interdemo{public static void Main (String args[]) {Fruit f = factory.getfruit (Args[0]); if (f!=null) {f.eat ();} }}
Three, the agent design mode
Take debt collection as an example
Interface give{public void Givemoney ();} Class Realgive implements Give{public void Givemoney () {System.out.println ("Give me the money back ...) ") ;}}; Class Proxygive implements give{//agent private Give Give = null;p ublic proxygive (Give Give) {this.give = Give;} public void before () {System.out.println ("Preparation: knives, ropes, rebar, steel, pistols, Drugs");} public void Givemoney () {this.before (); This.give.giveMoney ();//Represents the real collector to complete the operation of the Debt collection This.after (); public void After () {System.out.println ("Destroy all incriminating evidence");}; public class Proxydemo{public static void Main (String args[]) {Give Give = new Proxygive (new Realgive ()); Give.givemoney () ;}};
Three common design patterns of Java learning