Java design Pattern: Single state mode, Factory mode, Agent mode, observer mode sample __java

Source: Internet
Author: User

Single State mode

It is not strange that public class Singleton {private Singleton () {}//define yourself an instance within yourself. Note that this is private only for internal calls private static Singleton instance = new Singleton (); This provides a static method for accessing this class externally, and you can access the public static Singleton getinstance () {return instance} directly.

Factory mode

Package com; Products Interface interface Product {public void GetName ();}//Specific product A class ProductA implements product {public void GetName () {S Ystem.out.println ("I am ProductA");} Specific product B class PRODUCTB implements product {public void GetName () {System.out.println ("I am ProductB");} Factory class public class Productcreator {public Product createproduct (String type) {if (' A '. Equals (type)) {return new Prod Ucta (); } if ("B". Equals (Type)) {return new PRODUCTB ();} else return null; public static void Main (string[] args) {Productcreator creator = new Productcreator (); Creator.createproduct ("A") Name (); Creator.createproduct ("B"). GetName ();}

Agent Mode 1

Import Java.lang.reflect.InvocationHandler; Import Java.lang.reflect.Method; Import Java.lang.reflect.Proxy; public class Proxytest {public static void main (string[] args) {Salecomputer salecomputer = new Computermaker (); Invocationhandler handler = new Saleproxy (salecomputer); Salecomputer proxy = (salecomputer) proxy.newproxyinstance (Salecomputer.getclass (). getClassLoader (), Salecomputer.getclass (). Getinterfaces (), handler); Proxy.salecomputer ("Floral"); } interface salecomputer{//selling computer public void Salecomputer (String type);} class Computermaker implements salecomputer{Pu Blic void Salecomputer (String type) {System.out.println ("sell a" + type + "Brand computer.) "); } } Class Saleproxy implements invocationhandler{Object delegate; public Saleproxy (object delegate) {this.delegate = Delegat E public object invoke (object proxy, Method method, object[] args) throws Throwable {Givemouse (); return Method.invoke (DE Legate,args); private void Givemouse () {System.out.println ("Send mouse .... ..."); ".}}

Agent Mode 2

public class ProxyTest1 {public static void main (string[] args) {SaleComputer1 salecomputer = new ComputerMaker1 (); SaleComputer1 proxy = new SaleProxy1 (salecomputer); Proxy.salecomputer ("Hung Run"); } interface salecomputer1{//interface public void Salecomputer (String type);} class ComputerMaker1 implements salecomputer1{/ /implementation class public void Salecomputer (String type) {System.out.println ("sell a" + type + "Brand computer.) "); } } Class SaleProxy1 implements salecomputer1{/proxy class SaleComputer1 delegate; public SaleProxy1 (Object delegate) { This.delegate = (SaleComputer1) delegate; public void Salecomputer (String type) {This.givemouse (); Delegate.salecomputer (type);} private void Givemouse () {Syste M.OUT.PRINTLN ("Send the mouse .... ..."); ".}

Observer mode:

Package Com.model; Import java.util.Observable; Import Java.util.Observer; The/** Java API provides us with observer interfaces and observable classes to implement the so-called observer pattern. The observable (Observer) class allows you to notify other objects (Implementation interface Observer, observer) when changes occur to them. */public class testobserver{public static void Main (string[] args) {Produce produce = new produce (); Nameobserver nameobserver = new Nameobserver (); Priceobserver priceobserver = new Priceobserver (); Produce.addobserver (Nameobserver); Produce.addobserver (Priceobserver); Produce.setname ("Apple"); Produce.setprice (100); }///An observable class produce extends observable{private string name, private Integer price, public string GetName () {retur n Name; public void SetName (String name) {this.name = name; setchanged ();//Set Change point notifyobservers (name);//Notify Observer} public Integ Er getprice () {return price} public void Setprice (Integer price) {this.price = Price; setchanged (); Notifyobservers (pr ICE); }///Two Observer class Nameobserver implements observer{public void update (observable o, Object Arg) {if (arg instanceofString) {System.out.println ("observed by Observer: Product name changed to:" + arg);}}} Class Priceobserver implements observer{public void update (observable o, Object Arg) {if (arg instanceof Integer) {System . OUT.PRINTLN ("observed by the Observer: The price has been changed to:" + arg);}}

 

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.