版权声明:本文为博主原创文章,转载请注明作者和出处。 https://blog.csdn.net/xinqing5130/article/details/83110928
设计模式是什么?
设计模式,顾名思义,就是软件设计过程中常见问题的解决方案,是经验的总结。
设计模式类型:
一、Builder,构造者模式。它是构造复杂对象的一种方式,尤其是在用相同的方式构造不同对象时,显得尤为有效。
例如,一般在封装实体类时我们通常会用构造方法等形式往实体类传递参数,有可能对不同个数的参数进行初始化,你会定义出不同的构造方法,当然这都没问题,如果是当前实体类中的参数过于多时,这时候再用构造方法传递参数会有点力不从心,很大程度上会影响代码的可读性与美观性。那这个时候构造者模式的出现完美的解决这一问题。
public class User
{
private String firstName; // required
private String lastName; // required
private int age; // optional
private String phone; // optional
private String address; // optional
private User(UserBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
}
//All getter, and NO setter to provde immutability
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "User: "+this.firstName+", "+this.lastName+", "+this.age+", "+this.phone+", "+this.address;
}
public static class UserBuilder
{
private final String firstName;
private final String lastName;
private int age;
private String phone;
private String address;
public UserBuilder(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public UserBuilder age(int age) {
this.age = age;
return this;
}
public UserBuilder phone(String phone) {
this.phone = phone;
return this;
}
public UserBuilder address(String address) {
this.address = address;
return this;
}
//Return the finally consrcuted User object
public User build() {
User user = new User(this);
validateUserObject(user);
return user;
}
private void validateUserObject(User user) {
//Do some basic validations to check
//if user object does not break any assumption of system
}
}
}
构造对象:
public static void main(String[] args) {
User user1 = new User.UserBuilder("Lokesh", "Gupta")
.age(30)
.phone("1234567")
.address("Fake address 1234")
.build();
System.out.println(user1);
User user2 = new User.UserBuilder("Jack", "Reacher")
.age(40)
.phone("5655")
//no address
.build();
System.out.println(user2);
User user3 = new User.UserBuilder("Super", "Man")
//No age
//No phone
//no address
.build();
System.out.println(user3);
}
Output:
User: Lokesh, Gupta, 30, 1234567, Fake address 1234
User: Jack, Reacher, 40, 5655, null
User: Super, Man, 0, null, null
二、prototype,原型模式
原型是实际对象创建前的任何对象的模板,当你要创建一个对象的多个实例,而这些实例又几乎相同或者说差别很小时,就可以使用原型模式。该模式的思想就是将一个对象作为原型,对其进行复制、克隆,产生一个和原对象类似的新对象。
示例:一个原型类,只需要实现Cloneable接口,覆写clone方法
package com.howtodoinjava.prototypeDemo.contract;
public interface PrototypeCapable extends Cloneable
{
public PrototypeCapable clone() throws CloneNotSupportedException;
}
package com.howtodoinjava.prototypeDemo.model;
import com.howtodoinjava.prototypeDemo.contract.PrototypeCapable;
public class Movie implements PrototypeCapable
{
private String name = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Movie clone() throws CloneNotSupportedException {
System.out.println("Cloning Movie object..");
return (Movie) super.clone();
}
@Override
public String toString() {
return "Movie";
}
}
package com.howtodoinjava.prototypeDemo.factory;
import com.howtodoinjava.prototypeDemo.contract.PrototypeCapable;
import com.howtodoinjava.prototypeDemo.model.Movie;
public class PrototypeFactory
{
public static class ModelType
{
public static final String MOVIE = "movie";
}
private static java.util.Map<String , PrototypeCapable> prototypes = new java.util.HashMap<String , PrototypeCapable>();
static
{
prototypes.put(ModelType.MOVIE, new Movie());
}
public static PrototypeCapable getInstance(final String s) throws CloneNotSupportedException {
return ((PrototypeCapable) prototypes.get(s)).clone();
}
}
package com.howtodoinjava.prototypeDemo.client;
import com.howtodoinjava.prototypeDemo.factory.PrototypeFactory;
import com.howtodoinjava.prototypeDemo.factory.PrototypeFactory.ModelType;
public class TestPrototypePattern
{
public static void main(String[] args)
{
try
{
String moviePrototype = PrototypeFactory.getInstance(ModelType.MOVIE).toString();
System.out.println(moviePrototype);
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
}
}
程序输出:
Cloning Movie object..
Movie