creational design patterns in java

Want to know creational design patterns in java? we have a huge selection of creational design patterns in java information on alibabacloud.com

Decorator mode for Java design patterns

Application Scenarios for Adorner mode:1, need to extend the functionality of a class.2, dynamic for an object to add functionality, but also can be dynamically revoked. (Inheritance cannot do this, the inherited functionality is static and cannot be dynamically deleted.) )Source interface:1 Public Interface sourceable {23 void method (); 4 }SOURCE class:1 Public class Implements sourceable {2 @Override3public void Method () {4 System.out.println ("Source.method () ..."); 5

Producer and consumer model--java design Patterns

=Newproductor (product);5 Pool.submit (proc);6Productor PROC2 =Newproductor (product);7 Pool.submit (PROC2);8ListNewArraylist();9 for(intI =0;i){TenCustomer cus =NewCustomer (product); One Cusgroup.add (cus); A pool.submit (cus); - } -Thread.Sleep (1000*1); theProc.stopproc ();//Termination of production -Proc2.stopproc ();//Termination of production -Cusgroup.foreach (Cus-cus.stop ());//Close Customer -Proc.stop ();//Close the producer, (Close the consumer first, or the consum

Java Design Patterns Rookie series (22) The model and implementation of the mediator pattern

house. By them choose the house they want */@Overridepublic void Recommendhouse () {zhangsan.selecthouse (); Lisi.selecthouse ();}} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) {Mediator mediator = new Myme Diator (); Mediator.createmediator (); Mediator.recommendhouse ();}}Iii. SummaryThe mediator pattern only needs to concern with the Mediator class relationship, the relationship between the detailed class and the class and the call to m

The observer pattern for Java design patterns

classConcreteSubjectImplementsSubject {PrivateListNewArraylist(); @Override Public voidaddobserver (Observer Observer) {observerlist.add (Observer); } @Override Public voidremoveobserver (Observer Observer) {observerlist.remove (Observer); } @Override Public voidnotifyobservers () { for(Observer observer:observerlist) {observer.update (); } }}Defining the abstract observer role Package Com.observerpattern; /** @author*/Publicinterface Observer { publicvoid update ();Define specific ob

The observer pattern for Java design Patterns

coupled, interdependent, and thus less reusable. The observer pattern is loosely coupled, so reusability can be improved.When do I use observer mode? When an object model has two facets, one aspect depends on the other. The two are encapsulated in separate objects so that they can be independently changed and reused. When a change to an object needs to change other objects at the same time, it is not known how many objects need to be changed. When an object must notify other ob

10-Proxy pattern for 23 design Patterns in Java

Interface Station{void Selltickets ();}Class Trainstation implements station{Public Trainstation (){}public void Selltickets (){System.out.println ("Trainstation selling tickets");}}Class Trainstationproxy implements station{Private Trainstation Atrainstation=null;Public Trainstationproxy (){Atrainstation=new trainstation ();}public void Selltickets (){Atrainstation.selltickets ();}}public class Proxypatterntest{public static void Main (string[] args){Trainstationproxy atrainstationproxy=new tra

23 Design Patterns in Java 6-appearance mode (facade pattern)

=customer;}public void Doservice (){SYSTEM.OUT.PRINTLN ("Massage service for" +customer);}}Class Packageservice{String Customer=null;Shopping ashopping=null;Meal Ameal=null;Massage amassage=null;Public Packageservice (String customer){This.customer=customer;Ashopping=new shopping (customer);Ameal=new Meal (customer);Amassage=new Massage (customer);}public void Doservice (){Ashopping.doservice ();Ameal.doservice ();Amassage.doservice ();}}public class Facadepatterntest{public static void Main (st

11 of 23 Design Patterns in Java-command pattern

Abstract class Command{public abstract void Excute ();}Class Attackcommand extends Command{Soldier Soldier=null;Public Attackcommand (Soldier Soldier){This.soldier=soldier;}public void Excute (){Soldier.attack ();}}Class Defendcommand extends Command{Soldier Soldier=null;Public Defendcommand (Soldier Soldier){This.soldier=soldier;}public void Excute (){Soldier.defend ();}}Class Soldier{public void Attack (){SYSTEM.OUT.PRINTLN ("Soldier Attack");}public void Defend (){System.out.println ("Soldier

A single-instance pattern of Java design patterns

-safe creation instances synchronized(Singleton.class) { //Check again if the instance exists, and if it does not exist, actually create the instance if(Instance = =NULL) {instance=NewSingleton (); } } } returninstance; }}Lazy Loading Internal implementations:Features: Delayed load and thread safety are implemented PackageSingleton; Public classSingleton {PrivateSingleton () {}/*** Class and the inner class, that is, the static me

Java Learning-The application of interfaces: Design patterns for factory methods

1 Public classTestfactorymethod {2 Public Static voidMain (string[] args) {3Iworkfactory i =Newstudentworkfactory ();4 i.getwork (). DoWork ();5 6Iworkfactory I1 =Newteacherworkfactory ();7 i1.getwork (). DoWork ();8 }9 }Ten One Interfaceiworkfactory{ A Work getwork (); - } - classStudentworkfactoryImplementsiworkfactory{ the - @Override - PublicWork Getwork () { - return Newstudentwork (); + } - + } A classTeacherworkfactoryImplementsiworkfactory{ at -

12 of 23 Design Patterns in Java-decoration mode (decorator pattern)

Class Girl{public void Showappearance () {System.out.println ("The Girl:face without Make Up");}} Class Takeflower extends Girl{girl girl=null;public takeflower (Girl Girl) {this.girl=girl;} public void Showappearance () {girl.showappearance (); Takeflower ();} public void Takeflower () {System.out.println (' Take Flower ');}} Class Takenecklace extends Girl{girl girl=null;public takenecklace (Girl Girl) {this.girl=girl;} public void Showappearance () {girl.showappearance (); Takenecklace ();} p

Java design Pattern (16)--template method pattern for behavioral patterns

first, the concept  Concept   UML diagram     Role     Usage Scenarios  In fact, we often used abstract class is the pattern to use, so basically belong to a better understanding of a pattern.Second, practice  According to the above role, give the corresponding code Abstract templates/*** Abstract Template * *@authorAdministrator **/ Public Abstract classAbstracttemplate {/*** Declaration and implementation of template methods*/ Public voidTemplatemethod () {//calling methods implemented by

Java design Pattern--a singleton pattern of creating patterns

I. Overview  Concept Decoupling abstraction from implementation so that abstractions and implementations can run independently  UML diagram  Role:    role Relationship  Second, practice  Set up the appropriate classes according to the roles above  Abstract roles/**@author Administrator * **/publicabstractclass Abstraction { protected implenmentor impl; Public void operation () { impl.operationimpl (); }}  Correcting abstract roles/**@author Administrator *

"Mediator pattern", the mediator pattern of Java design patterns

("Landlord Lee", mediator); Landlordb = new landlord ("Landlord Li", mediator); R Enter = new Renter ("Small Lu", mediator);//Landlord Registration intermediary Mediator.registerlandlord (LANDLORDA); Mediator.registerlandlord ( LANDLORDB)///Mediator.registerrenter (renter);//Ask the tenant to send the rental information renter.sendmessage ("Rent a house near Tianhe Park, the price of about 1000 yuan one months"); System.out.println ("--------------------------");//landlord a Send home rental me

------Broker Pattern for Java 23 design Patterns

("User1 exe!"); } } public class User2 extends User { Public User2 (mediator mediator) { Super (mediator); } @Override public void work () { System.out.println ("User2 exe!"); } } Test class: public class Test { public static void Main (string[] args) { Mediator Mediator = new Mymediator (); Mediator.createmediator (); Mediator.workall (); } } Output:User1 exe!User2 exe!Reprinted fr

-------Interpreter for Java 23 design Patterns

) { this.num2 = num2; } } public class Test { public static void Main (string[] args) { Calculate the value of a 9+2-8 int result = new minus (). Interpret (new Context (New Plus () . Interpret (new Context (9, 2)), 8)); SYSTEM.OUT.PRINTLN (result); } } Finally output the correct result: 3.Basically, the interpreter pattern is used to make all kinds of interpreters, such as regular expressions and so on!Reprinted from Https://www.

24 Design Patterns and 7 principles in Java

, and if the encapsulation is not important. --------------------------------------------------------------------------------------------- ----- Seven design principles: 1. Single RESPONSIBILITY PRINCIPLE: one class is responsible for one responsibility.2, the Richter replacement principle "LISKOV SUBSTITUTION PRINCIPLE": Inheritance and derivation of the rules.3, the dependency inversion principle "dependence inversion PRINCIPLE": high-level mo

Six basic guidelines for JAVA design patterns to follow

Six basic guidelines for JAVA design patterns to followa single principle of responsibility: (Responsibility pinciple) A class is responsible for only one responsibility. When more than one responsibility is required, new classes need to be added to take responsibility for new responsibilities, rather than personality codes in the class.If a class takes on too ma

Applying design Patterns in Java--singleton

give a code to implement the Registrar. The benefit of creating a singleton pattern with the registrar mechanism is that it is easy to manage and can control multiple different types of singleton instances at the same time. Summary 1. The singleton model can be easily expanded to produce a specified number of instances. 2. In the design Patterns Java Companio

Common design Patterns for Java------

layer, they have to communicate through the interface, this layered mode in the software design with a lot of, must be paid attention to.3) In each layer, there are many small modules, a small module external should also be a whole, then a module external should also provide interfaces, other places need to use the function of this module, should be through this interface.2. Parameters and return values in the interface definition:1) The interface mu

Total Pages: 15 1 .... 11 12 13 14 15 Go to: Go

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.