The design pattern of Dark Horse programmer--java

Source: Internet
Author: User

------- Android Training , Java Training , look forward to communicating with you! ----------

There are 23 design patterns in Java

According to the goal, the design pattern can be divided into the creation mode, the structure pattern and the behavior pattern.
The creation pattern is used to process the creation of objects, which are used to deal with the combination of classes or objects, and the behavioral patterns are used to describe how classes or objects interact and assign responsibilities.

The Create pattern is used to process the creation of objects, including the following 5 design patterns:
 Factory method Pattern (Factory mode)
 Abstract Factory mode (Factory pattern)
 Builder mode (constructor pattern)
 Prototype mode (Prototype pattern)
 Single case mode (Singleton pattern)

A structured pattern is used to handle a combination of classes or objects, consisting mainly of the following 7 design patterns:
 Adapter mode (Adapter pattern)
 Bridging mode (bridge pattern)
 Combination mode (Composite pattern)
 Decorator mode (Decorator pattern)
 Appearance mode (facade pattern)
 Enjoy meta mode (Flyweight pattern)
 Agent mode (proxy pattern)

Behavioral patterns are used to describe how classes or objects interact and assign responsibilities, mainly including the following 11 design patterns:
 Responsibility chain mode (Chain of Responsibility pattern)
 Command pattern
 Interpreter mode (interpreter pattern)
 Iterator mode (Iterator pattern)
 Mediator mode (mediator pattern)
 Memo Mode (Memento pattern)
 Observer mode (Observer pattern)
 Status mode (state pattern)
 Strategy mode (strategy pattern)
 Template Method pattern
 Visitor Mode (Visitor pattern)

In the study, we mainly studied two kinds of design patterns: Template method design pattern and the single-case design mode of the sharing meta-design pattern.

Enjoy meta design mode (Flyweight):

There are a lot of small objects, they have many properties of the same, turn them into an object, which different attributes, turn them into the parameters of the method, these different attributes become the external state (External)

Those same attributes become intrinsic (InternalState)

concept: that is, if there are multiple identical objects in a system, it is OK to share only one copy, not each to instantiate an object. For example, a text system, each letter of an object, then the big lowercase letter is a total of 52 Then define the 52 1m
flyweight mode is an improved program mode of efficiency and performance There are many applications.

benefits of the enjoy meta design model: reduce the number of objects and save memory space

The disadvantages of the enjoy meta-design pattern: maintaining shared objects requires additional overhead (using a thread to maintain garbage collection)

Template Method Design Pattern:

problem solved: Some implementations are indeterminate when the internal part of the function is implemented. At this point, we can expose the uncertain parts and let the subclasses to achieve them.

Abstract class gettime{

Public final void GetTime () {//This feature can be final qualified if no replication is required

Long start = System.currenttimemillis ();

Long end = System.currenttimemillis ();

System.out.println ("milliseconds is:" + (End-start));

}

Public abstract Void Code ();//abstract indeterminate function, enabling sub-class replication implementation

}

Class Subdemo extends gettime{

Public Void Code () {//Sub-class replication function method

for (int y=0; y<1000; y++) {

System.out.println ("Y");

}

}

}

What is a template method?

When defining a feature, part of the feature is deterministic, but part of it is indeterminate, and the identified part is used in the indeterminate part, then the indeterminate part is exposed and the subclass of the class is completed.

Single Case design mode:

Design Patterns: The most effective way to solve a class of problems

23 Design Patterns in Java

Problem solved: Ensure that the object uniqueness of a class in memory.

For example: When multiple programs read a configuration file, it is recommended that the configuration file be encapsulated as an object. It is convenient to manipulate the data, and to ensure that multiple programs read the same profile object, it is necessary that the configuration file object is unique in memory.

Runtime () The method is designed by a single case design pattern.

How to ensure the uniqueness of the object?

Thought:

1, do not allow other programs to create the class object.

2, create an object of this class in this class.

3, provide methods for external, let other programs get this object.

Steps:

1, because the creation of objects requires constructor initialization, as long as the constructors in this class are privatized, other programs can no longer create the class object;

2, create an object of this class in a class;

3, define a method that returns the object so that other programs can get this type of object by means of the method. (Effect: controllable)

The code reflects:

1, privatization of the constructor function;

2, create private and static objects of this class;

3, defines a public and static method that returns the object.

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

a hungry man type

Class single{

Private single () {}// privatization constructor.

private static Single S = new single (); creates a private and static object of this class.

public static single getinstance () {// defines a publicly-owned and static method that returns the object.

return s;

}

}

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

lazy type : Lazy loading mode.

Class single2{

Private Single2 () {}

private static Single2 s = null;

public static Single2 getinstance () {

if (s==null)

s = new Single2 ();

return s;

}

}

How to describe things and how to describe them

When you need to guarantee the object of this thing to be unique in memory, you can add the three steps of the code implementation of the above single-instance design pattern.

How do I call a class that joins a singleton design pattern in the main function?

Student ss=student.getintance ();//invocation of a singleton design pattern class public methods provided externally

a hungry man: A single class enters the object, it has already created the object.

Lazy Type: The single class enters memory, the object does not exist, and the object is created only when the GetInstance method is called

------- Android Training , Java Training , look forward to communicating with you! ----------

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The design pattern of Dark Horse programmer--java

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.