Three common design patterns of Java learning

Source: Internet
Author: User
Tags money back

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.