Common java design patterns, java Design Patterns

Source: Internet
Author: User

Common java design patterns, java Design Patterns

1. Factory design model.

Reference: http://www.cnblogs.com/zhouqiang/archive/2012/07/20/2601365.html

There are three types: Simple Factory, Factory Method, and Abstract Factory)

Simple Factory:

   

1. Similar Products Abstract The interface IProduct. The specific product implements this interface.

2. Define the Factory class and provide a static getProduct method for getting a specific product.

To obtain a specific product, you can directly call Factory. getProduct. if there are multiple similar products, you can set parameters in the getProduct method, or define multiple getProduct Methods: getProductA () getProductB () getProductC ()

Factory method:

   

1. The product abstracts the interface IProduct, and the specific product implements these interfaces.

2. Define the factory interface ifacloud and define different factory FactoryA, FactoryB... Different factories provide different products. These factories do not use static methods.

To get a specific product, first obtain the specific factory (new) and get a product according to a factory.

Abstract Factory:

   

1. Product abstraction interface IProduct. different series of sub-Products Abstract interfaces IProductA and IProductB. Specific Products implement these interfaces.

2. Define the factory interface ifacloud and define different factory Factory1, Factory2... Different factories provide different series of products, which are not provided using static methods.

For example

Product interface: Door tire parts

Product implementation: BMW door Volkswagen door BMW tires Volkswagen tires BMW parts Volkswagen parts

Factory: Workshop

Specific Factory: BMW workshop (with BMW products) Volkswagen workshop (with Volkswagen products)

2. Singleton design mode:

The objective is to ensure that certain types of objects are unique in the memory. There are two types: hungry and lazy:

Hungry Chinese Style: initialization upon loading

   

class Singleton{

private Singleton(){}

private static Singleton instance = new Singleton();

public static Singleton getInstance() { return instance; }

}

Lazy: Create an instance when it is used

   

class Singleton{

private Singleton(){}

private static Singleton instance;

public static Singleton getInstance() {

   if (instance == null)

     synchronized (Singleton.class) {

         if (instance == null) instance = new Singleton();

     }

   return instance;

}

}

3. Prototype ):

In java, clone is used to obtain new objects by cloning the prototype.

   

Class to implement the Cloneable interface, override the clone method

4. Builder mode:

   

1. Provide classes used to provide components of products, such as: Production Workshop Builder :( provide various methods for production: Door tire parts, etc)

2. classes that provide how to assemble products, such as: assembly workshop ctor (available method: Assemble construct)

To get the product, first we have a production workshop. The Assembly Workshop will call each method of the production workshop to produce the required parts and assemble them to provide the product.

Director dire= new director (); Product product = Director. construct (new Builder ());

To be continued


Which design patterns are commonly used in java?

There are 23 design modes in total!

According to the purpose, the design patterns can be divided into creation patterns, structural patterns, and behavior patterns.
The creation mode is used to process the object creation process, the structure mode is used to process the combination of classes or objects, and the behavior mode is used to describe how classes or objects interact and how responsibilities are assigned.

The creation mode is used to process the object creation process. It mainly includes the following five design modes:
Factory Method Pattern)
Abstract Factory Pattern)
Builder Pattern)
Prototype Pattern)
Singleton Pattern)

The structural mode is used to process the combination of classes or objects. It mainly includes the following seven design modes:
Adapter Pattern)
Bridge Pattern)
Composite Pattern)
Decorator Pattern)
Facade Pattern)
Flyweight Pattern)
Proxy Pattern)

The behavior pattern is used to describe how classes or objects interact and how responsibilities are assigned. It mainly includes the following 11 design patterns:
Chain of Responsibility Pattern)
Command Pattern)
Interpreter mode (Interpreter Pattern)
Iterator Pattern)
Mediator Pattern)
Memory memorandum mode (Memento Pattern)
Observer Pattern)
State Pattern)
Strategy Pattern)
Template Method Pattern)
Visitor Pattern)

We recommend that you have a good book: "software tips: the point of design patterns", in which 23 examples of design patterns are very vivid and easy to understand, as well as the application of design patterns in JDK, I saw a big harvest! Baidu searches for "design patterns", and the first design patterns in Baidu encyclopedia are the first to push the book. If the number of views is more than, it will not be wrong. Let's share some good things!
I wish you an early learning of the design model!

Java 23 design patterns, which are commonly used

Factory mode, factory method mode, Singleton mode, Facade mode, Observer mode, and Bridge mode are commonly used, different projects have different design directions, and the design patterns that can be referenced are also different. There is no definite number, but the above models use more.

I looked for other models and listed them.

======================================
Common Java Design Patterns
Creation Mode

1. FACTORY-try to catch up with MM. The chicken wings of McDonald's and the chicken wings of KFC are what MM loves. Although the taste is different, whether you take MM to McDonald's or KFC, just say "four chicken wings" to the waiter. McDonald's and KFC are Factory for chicken wings production.

Factory model: the customer class and the factory class are separated. A consumer needs a product at any time and only requests from the factory. Consumers can accept new products without modification. The disadvantage is that the factory class must be modified when the product is modified. For example, how to create and provide data to clients.

2, BUILDER-MM is the most love to hear is "I love you" this sentence, to see different places of MM, to be able to use their dialect to say this sentence to her oh, I have a multi-language translation machine with a key for each language above. To see MM, I only need to press the corresponding key, it will be able to say "I love you" in the corresponding language, and foreign MM can also easily develop, This is my "I love you" builder. (This must be better than the US military's translation server in Iraq)

Construction mode: separates the internal appearance of a product from the product generation process, so that a building process generates product objects with different internal appearances. The construction mode allows the product's internal appearance to change independently, so that the customer does not have to know the details of the product's internal composition. The construction model enforces a step-by-step construction process.

3. factory method-ask MM to go to McDonald's for a hamburger. Different MM has different tastes. It is annoying to remember every one. I usually use the Factory Method mode, take the MM to the waiter and say "I want a hamburger". What kind of hamburger should I ask the MM to tell the waiter directly.

Factory method mode: the Core factory category is no longer responsible for the creation of all products. Instead, it submits the specific creation work to the subclass to become an abstract factory role, it is only responsible for providing the interface that must be implemented by a specific factory class, without touching the details of which product class should be instantiated.

4. PROTOTYPE-chat with MM via QQ, so I must talk about some affectionate words. I have collected a lot of emotional words, and I just need to copy the words and put them in QQ, this is my prototype. (100 yuan a copy, do you want)

Original Model mode: specify the type of the object to be created by giving a prototype object, and then create more objects of the same type by copying the prototype object. The original model mode allows you to dynamically add or remove product classes. The product class does not need to have any predefined level structure. The original model mode applies to any level structure. The disadvantage is that each class must have a clone method.

5. SINGLETON-I have 6 beautiful wives. My husband is me. I am Sigleton, our husband. All they need to say is "husband, that's me (I had a dream just now. How can this be a good thing)

Singleton mode: Singleton mode ensures that a class has only one instance, and instantiate the instance and provides the singleton mode to the entire system. The Singleton mode can only be used when there is a real "Single Instance" demand.

Structural Mode

6. ADAPTER-I met Sarah, a beautiful girl from Hong Kong at a friend's party. But I can't speak Cantonese, and she can't speak Mandarin. I have to turn to my friend kent for help, as an Adapter between me and Sarah, Sarah and I can talk to each other (I don't know if he will play with me)

Adapter (transformer) mode: converts an interface of a class into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together. The adaptation class can be returned according to the parameters... the remaining full text>

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.