The creative model of Java Classic 23 design patterns (I.)

Source: Internet
Author: User

The design pattern is called the procedure Ape's internal strength, before fragmented saw a large part, but oneself has summed up. So this is summed up here. It is worth mentioning that design patterns are not unique to Java. Because of always doing Android. Here we use Java as the carrier. The most classic design patterns are 23, divided into three large types: the creation mode (5), the structural mode (7), the behavioral mode (11), 5 + 7 +11 = 23. Online Search is also a lot of, here but personal record. This document records the three types of factory methods (Factory method), abstract Factory (Factory), and singleton patterns in the creative mode. Strive to be thorough.

first, the factory method

The factory method involves four concepts, for the sake of popular understanding, first borrow the online sample. Men with women to KFC to eat, the first man said to hamburger. The details of what hamburger let mm. Personally, this example is not very appropriate and easy makes people misunderstand. The simplest example is that there are very many factories. Each plant is capable of producing specific products. For a clear understanding. Or a combination of Java design patterns (Crazy Java Alliance Edition) to introduce.

1, product is an interface, the details of what did not say.

Public interface Work {
void DoWork ();
}

2, Concreteproduct. Details of the product. That is to achieve the above common work interface, but do different things. The concept of a class is of course the same thing.

public class Studentwork implements work {
public void DoWork () {
SYSTEM.OUT.PRINTLN ("Student * homework!");
}
}

public class Teacherwork implements work {
public void DoWork () {
System.out.println ("Teacher approval assignment!");
}
}

Two detailed products are defined here. The job of the student is homework, the teacher's job is to approve the homework.

3, Creator, creator. corresponding to the product above. The purpose of creator is to obtain product. Get the product.

Public interface Iworkfactory {
Work getwork ();
}

4, Concretecreator. A detailed creator.

The creator gets work. Then all the detailed products meet the requirements. Each product is packaged in a factory.

public class Studentworkfactory implements Iworkfactory {


Public work getwork () {
return new Studentwork ();
}


}


public class Teacherworkfactory implements Iworkfactory {


Public work getwork () {
return new Teacherwork ();
}


}

Test code such as the following:

public class Test {


public static void Main (string[] args) {
Iworkfactory studentworkfactory = new Studentworkfactory ();
Studentworkfactory.getwork (). DoWork ();

Iworkfactory teacherworkfactory = new Teacherworkfactory ();
Teacherworkfactory.getwork (). DoWork ();
}

}
Applicability:

1. When a class does not know the class of the object it must create.

2. When a class wants its subclass to specify the object it creates.

3. When you want to entrust the responsibility of creating objects to one of several helper classes. And you want to be a helper that is the agent of this information localization.

second, abstract factory

With the example above, it is very good to understand the abstract factory.

For example, go to the store to buy things, the store is factory, you have to say directly. The clerk will get you anything.

In conjunction with the above example, a Workfactory implementation iworkfactory interface is defined. A reference is passed in the Workfactory, based on whether the parameter is new, Studentwork, or teacherwork, and then return. This is the abstract factory.

This is the simplest and most straightforward understanding. For a clear understanding. Here is a more complex example.

1,abstractproduct abstract Products

Public interface ICat {
void Eat ();
}


Public interface Idog {


void Eat ();
}

2, Concreteproduct detailed products

public class Blackcat implements ICat {
public void Eat () {
System.out.println ("The Black Cat is eating!");
}
}


public class Whitecat implements ICat {


public void Eat () {
Sy*tem.out.prin*ln ("The W*ite Cat is eating!*);
}
}

public class Blackdog implements Idog {


public void Eat () {
System.out.println ("The Black Dog is eating");
}


}


public class Whitedog implements Idog {


public void Eat () {
System.out.println ("The White Dog is eat*ng!");
}


}

3, Abstractfactory abstract Factory, interface.

Public interface Ianimalfactory {


ICat Createcat ();

Idog Createdog ();
}

4.

Concretefactory the detailed factory to achieve the above interface.

public class Blackanimalfactory implements Ianimalfactory {


Public ICat Createcat () {
return new Blackcat ();
}


Public Idog Createdog () {
return new Blackdog ();
}


}


public class Whiteanimalfactory Imp*ements ianimalfactory {


Public ICat Createcat () {
return new Whitecat ();
}


Public Idog Createdog () {
return new Whitedog ();
}


}

Test code:

public static void Main (string[] args) {
Ianimalfactory blackanimalfactory = new Blackanimalfactory ();
ICat Blackcat = Blackanimalfactory.createcat ();
Blackcat.eat ();
Idog Blackdog = Blackanimalfactory.createdog ();
Blackdog.eat ();

Ianimalfactory whiteanimalfactory = new Whiteanimalfactory ();
ICat Whitecat = Whiteanimalfactory.createcat ();
Whitecat.eat ();
Idog Whitedog = Whiteanimalfactory.createdog ();
Whitedog.eat ();
}

Note: You can see this complex sample with interface to express the factory according to different needs to create different products, and simple sample is to determine the number of different products.

Use occasions:

1. A system is independent of the creation, composition, and presentation of its products.
2. When a system is to be configured by one of multiple product families.
3. When you want to emphasize the design of a series of related product objects for joint use *
4 when you provide a Product class library, and you want to display only their interfaces instead of implementing them.

Three, single case mode

It's a lot more than that, and it's very clear to me that I won't repeat it.

Points:

1 is for the security of thread invocation. Plus the sync lock. Use double locking for efficiency.

2, in addition to such a classic single exception there are other singleton mode.


The creative model of Java Classic 23 design patterns (I.)

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.