Java Note 7__ Interface application/object class/Simple Factory mode/static proxy mode/adapter mode

Source: Internet
Author: User

/*** Interface Application*/ Public classMain { Public Static voidMain (string[] args) {person P=NewPerson ("Fish7"); P.setisay (NewBeforegong ());        P.say (); P.setisay (NewAftergong ());    P.say (); }}classperson{//only the runtime is associated with the say behavior, which is called interface-oriented programming .    PrivateString name; PrivateIsay Isay;//to use an interface as a property (combination) in a class         PublicPerson (String name) { This. Name =name; }     Public voidSetisay (Isay Isay) {//you can pass Beforegong or you can pass Aftergong .         This. Isay =Isay; }     Public voidsay () {Isay.say (); }}Interfaceisay{//Abstract The behavior of speech into an interface     Public voidSay ();//abstract The changeable behavior, define it as an interface, and provide different implementations.}classBeforegongImplementsisay{ Public voidsay () {System.out.println ("I ' m a man."); }}classAftergongImplementsisay{ Public voidsay () {System.out.println ("I ' m a woman~"); }}/*** Strategy Design pattern: Defines a series of algorithms that encapsulate each algorithm and can be replaced with each other using the * policy mode to make the algorithm independent of the customer application using it * OO design principles: * 1. Interface-oriented programming (for abstract programming) * 2. Package changes * 3. Multiple use combinations, less inheritance */

/*** Object class: Parent class for all classes*/ Public classMain { Public Static voidMain (string[] args) {A a=NewA ("a");        System.out.println (a); A b=NewA ("a"); A C=NewA ("B"); System.out.println (A.equals (b)); //Output TrueSystem.out.println (A.equals (c));//Output Falsemethod (a); }     Public Static voidmethod (Object obj) {if(objinstanceofA) {A T1=(A) obj;        T1.eat (); }    }}classa{String name; A (String name) { This. Name =name; }     PublicString toString () {//overriding the ToString () method in the Obejct class        return"Fuck u"; }     Public BooleanEquals (Object obj) {//override the Equals () method in the Obejct class to determine whether two object values are equal        if(objinstanceofA) {A A=(A) obj; if( This. Name.equals (a.name))return true; Else                return false; }Else{            return false; }    }    voideat () {System.out.println ("I ' m eating ...."); }}

/*** Simple Factory mode: A Factory object determines which product class instances are created * if Clothdoll or barbiedoll has changed, just change it in dollfactory, without requiring the client to change*/ Public classMain {//Client     Public Static voidMain (string[] args) {Doll R1= Dollfactory.getinstance ("cloth");        System.out.println (R1.info ()); Doll R2= Dollfactory.getinstance ("Barbie");    System.out.println (R2.info ()); }}classdollfactory{//Server     Public StaticDoll getinstance (String name) {if("cloth". Equals (name))return NewClothdoll (); if("Barbie". Equals (name))return NewBarbiedoll (); return NULL; }}Interfacedoll{ PublicString info ();}classClothdollImplementsdoll{ PublicString info () {return"I ' m a clothdoll ..."; }}classBarbiedollImplementsdoll{ PublicString info () {return"I ' m a barbiedoll ..."; }}

/*** Static proxy mode: Provides a proxy for other objects to control access to this object **/ Public classMain { Public Static voidMain (string[] args) {person P1=NewPerson ("Fish7"); Proxyer m=NewProxyer (p1);//create a delegate and pass in the delegateM.date ();//Blind Date    }}Interfacesubject{//the Theme Interface     Public voidDate ();//Blind Date}classPersonImplementssubject{//by the agent    PrivateString name; Person (String name) { This. Name =name; }     Public voidDate () {System.out.println ("I ' M" +name+ ", I ' m dating ...."); }}classProxyerImplementssubject{PrivateSubject target;//Agent target ObjectProxyer (subject target) { This. target =Target; }    Private voidBefore () {//things to do before a blind dateSystem.out.println ("Find the matching person for the agent"); }    Private voidAfter () {//what to do after a blind dateSystem.out.println ("The end of this blind date"); }     Public voidDate () {before ();        Target.date ();    After (); }}

/*** Adapter mode: Convert the interface of one class to another interface that the customer wants * Adapter mode makes it possible for those classes that are incompatible with the interface to work together **/ Public classMain { Public Static voidMain (string[] args) {//System.out.println ();Powera A =NewPluga ();        Start (a); Powerb b=NewPLUGB (); Poweradapter PA=NewPoweradapter (b);//AdapterStart (PA); }     Public Static voidStart (Powera s) {S.insert (); }}classPoweradapterImplementspowera{//adapter, "turn the B" into "a" port .    PrivatePowerb b;//interfaces to be adapted     PublicPoweradapter (Powerb b) { This. B =b; }     Public voidInsert () {b.connect (); }}Interfacepowera{//power supply A interface     Public voidInsert ();}Interfacepowerb{//power supply B interface     Public voidConnect ();}classPlugaImplementspowera{ Public voidInsert () {System.out.println ("The Power a connector has been plugged in and started working ...."); }}classPlugbImplementspowerb{ Public voidConnect () {System.out.println ("The Power B interface has been plugged in and started working ...."); }}

Java Note 7__ Interface application/object class/Simple Factory mode/static proxy mode/adapter mode

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.