Several common design patterns in Java

Source: Internet
Author: User

Design Patterns

1. Overview

1) Design pattern: is a set of repeated use, most people know, through the classification of the purpose, code design experience summary;

2) Classification:

Create pattern (Create object): Singleton mode, abstract Factory mode, builder mode, Factory mode, prototype mode.

Behavioral mode (the function of the object) : Adapter mode, bridge mode, decoration mode, combination mode, appearance mode, enjoy meta mode, proxy mode.

Structural patterns (the composition of objects): Template method mode, command mode, iterator mode, observer pattern, Mediator mode, Memo mode, interpreter mode, state mode, policy mode, responsibility chain mode, visitor mode.

2. Simple Factory mode

1) Overview: Also called the Static Factory method pattern, which defines a specific factory class that is responsible for creating instances of some classes

2) Realization:

                         public abstract class  Animal {public abstract void eat ();} public class cat extends animal{@Overridepublic  void eat ()  { System.out.println ("Cat eats Fish");}} public class dog extends animal{@Overridepublic  void eat ()  { System.out.println ("Dog eats Meat");}} Public class animalfactory {public static animal get (String name) {if ( Name.equals ("Dog")) {Return new dog ();} Else if (Name.equals ("Cat")) {Return new cat ();} Else{return null;}}} Public class test {public static void main (String[] args)  { Animalfactory.get ("Cat"). Eat (); Animalfactory.get ("Dog"). Eat ();}} 


3, factory method mode

1) Overview: Factory method Mode abstract The factory class is responsible for defining the interfaces that create objects, and the creation of specific objects is implemented by the concrete classes that inherit the abstract factory

2) Realization:

public interface Animalfactory {

Public abstract Animal get ();

}

--------------------------

public class Dogfactory implements Animalfactory {

@Override

Public Animal get () {

return new Dog ();

}

}

-------------------------------------

Animalfactory af = new dogfactory ();

Animal Animal = Af.get ();

Animal.eat ();

4. Single case mode

Overview: Guaranteed class has only one object in memory

1) A Hungry man type

public class Student {Private final static Student s = new Student ();p rivate Student () {super ();} public static Student get () {return s;}}


2) Lazy Type

public class Teacher {private static Teacher T = Null;public Teacher () {super ();} public static Teacher get () {if (t = = null) {t = new Teacher ();} return t;}}


3) Runtime Class (Implementation of Singleton mode)

Overview: Each Java application has a runtime class instance that enables the application to connect to the environment in which it is running. The current run-time object can be obtained through the GetRuntime method.

Public Process exec (String command) executes DOS command

5. Template design mode

1) Overview: Define the skeleton of an algorithm, and defer the specific algorithm to the subclass to implement

2) Realization:

                         public abstract class  Gettime {public long gettime () {long starttime = system.currenttimemillis (); Work ( );//Some time-consuming operation Long endtime = system.currenttimemillis (); return endtime - starttime;} Public abstract void work ();} public class extendsget extends gettime{@Overridepublic  void work ()  {for ( int i = 0;i < 1000;i ++) {System.out.print (i);} System.out.println ();}} Public class test {public static void main (String[] args)  {ExtendsGet  eg = new extendsget (); Long time = eg.gettime (); System.out.println (time);}} 


6. Decoration mode

1) Overview: Using an instance of a subclass of the decorated class, the instance of the subclass is given to the decorator class at the client. is an alternative to inheritance

2) Implementation: (The IO stream will wrap the file into an efficient character stream)

                         public interface phone {void call ();} public class callphone implements phone{@Overridepublic  void call ()  { SYSTEM.OUT.PRINTLN ("call");}} public class phonedecorate extends callphone{private phone phone;public  Phonedecorate (Phone phone)  {super (); this.phone = phone;} @Overridepublic  void call ()  {phone.call ();}} Public class musicphone extends phonedecorate{public musicphone (Phone phone)  {super (phone);} @Overridepublic  void call ()  {super.call (); System.out.println ("Listen to Music");}} Public class videophone extends phonedecorate{public videophone (Phone phone)  {super (phone);} @Overridepublic  void call ()  {super.call (); System.out.println ("watch video");}} PUblic class test {public static void main (String[] args)  {new  Videophone (New musicphone (New callphone ())). Call ();}}


7. Observer mode

1) Overview: A one-to-many dependency, allowing multiple observer objects to listen to a Subject object at the same time;

2) Realization:

                         public class WorkMan {    String  Manname;public workman (String manname)  {super (); Manname = manname;}} Public class job {string jobname;int jobmoney;public job (String jobname,  int jobmoney)  {super (); This.jobname = jobname;this.jobmoney = jobmoney;}} public class headhunter {private arraylist<job> jobal = new  Arraylist<> ();p rivate arraylist<workman> manal = new arraylist<> (); Public void addworkman (WORKMAN&NBSP;WM) {manal.add (WM);} Public void delworkman (WORKMAN&NBSP;WM) {manal.remove (WM);} Public void addjob (job job) {jobal.add (job); sent (job);} Public void deljob (job job) {Jobal.remove (Job);} Private void sent (job job)  {for (workman wm : manal) {System.out.println (Wm. manname + , "Hello! Have a " + job.jobname + " job, Salary " + job.jobmoney + " yuan, interested parties please timely interview ");}} Public class test {public static void main (String[] args)  {Headhunter  headhunter = new headhunter (); Workman wm1 = new workman ("Zhang San"); Workman wm2 = new workman ("John Doe"); Workman wm3 = new workman ("Harry"); Headhunter.addworkman (WM1); Headhunter.addworkman (WM2); Headhunter.addworkman (WM3); Job job1 = new job ("Driver",  8000); Job job2 = new job ("Nanny",  9000); Job job3 = new job ("cleaner",  6000); Headhunter.addjob (JOB1); Headhunter.addjob (JOB2); Headhunter.addjob (JOB3); Headhunter.delworkman (WM3); Headhunter.deljob (JOB3); Headhunter.addworkman (new  WorkMan ("Zhao Liu")); Headhunter.addjob (new&nbsp Job ("Security",  7000);}}


Several common design patterns in Java

Related Article

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.