设计模式

作者: Ludwigvan | 来源:发表于2017-09-14 12:14 被阅读17次

1.对象模式三剑客:建设者模式  工厂模式  抽象工厂模式

1、建设者模式:

public class User {

private String name;

private static int test=1;

private String pwd;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPwd() {

return pwd;

}

public void setPwd(String pwd) {

this.pwd = pwd;

}

private User(String name,String pwd){

this.name = name;

this.pwd = pwd;

}

@Override

public String toString() {

return test+name+pwd;

}

public static class UserBuilder{

private String name;

private String pwd;

public UserBuilder setName(String name) {

this.name = name;

return this;

}

public UserBuilder setPwd(String pwd) {

this.pwd = pwd;

return this;

}

public User build(){

System.out.println(toString());

return new User(name,pwd);

}

@Override

public String toString() {

return test+name+pwd;

}

}

}

链式调用:

User user =new User.UserBuilder().setName("ss").setPwd(".").build();

user.toString();

2、工厂模式:

Meal fruit = Meal.valueOf("banana");

Meal vegetable = Meal.valueOf("carrot");

class Meal {

private String type;

public Meal(String type) {

this.type = type;

}

public String getType() {

return this.type;

}

// Example of factory method - different object is created depending on current context

public static Meal valueOf(String ingredient) {

if (ingredient.equals("banana")) {

return new Meal("fruit");

}

return new Meal("vegetable");

}

}

3.抽象工厂:

public class FactoryTest {

// Test method which is the client

@Test

public void test() {

Kitchen factory = new KitchenFactory();

KitchenMeal meal = factory.getMeal("P.1");

KitchenMeal dessert = factory.getDessert("I.1");

assertTrue("Meal's name should be 'protein meal' and was '"+meal.getName()+"'", meal.getName().equals("protein meal"));

assertTrue("Dessert's name should be 'ice-cream' and was '"+dessert.getName()+"'", dessert.getName().equals("ice-cream"));

}

}

// abstract factory

abstract class Kitchen {

public abstract KitchenMeal getMeal(String preferency);

public abstract KitchenMeal getDessert(String preferency);

}

// concrete factory

class KitchenFactory extends Kitchen {

@Override

public KitchenMeal getMeal(String preferency) {

if (preferency.equals("F.1")) {

return new FastFoodMeal();

} else if (preferency.equals("P.1")) {

return new ProteinMeal();

}

return new VegetarianMeal();

}

@Override

public KitchenMeal getDessert(String preferency) {

if (preferency.equals("I.1")) {

return new IceCreamMeal();

}

return null;

}

}

// abstract product

abstract class KitchenMeal {

public abstract String getName();

}

// concrete products

class ProteinMeal extends KitchenMeal {

@Override

public String getName() {

return "protein meal";

}

}

class VegetarianMeal extends KitchenMeal {

@Override

public String getName() {

return "vegetarian meal";

}

}

class FastFoodMeal extends KitchenMeal {

@Override

public String getName() {

return "fast-food meal";

}

}

class IceCreamMeal extends KitchenMeal {

@Override

public String getName() {

return "ice-cream";

}

}

相关文章

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

  • 设计模式笔记汇总

    目录 设计原则 “依赖倒置”原则 未完待续... 设计模式 设计模式——策略模式 设计模式——装饰者模式 设计模式...

  • 设计模式

    《C#设计模式》 《C#设计模式》-设计模式概述 《C#设计模式》-面向对象设计原则 《C#设计模式》-单例模式 ...

  • 浅谈JS的一些设计模式

    @(书籍阅读)[JavaScript, 设计模式] 常见设计模式 设计模式简介 设计模式概念解读 设计模式的发展与...

  • 前端设计模式

    JS设计模式一:工厂模式jS设计模式二:单例模式JS设计模式三:模块模式JS设计模式四:代理模式JS设计模式五:职...

  • 设计模式之工厂模式

    设计模式之工厂模式 标签(空格分隔): 设计模式 工厂模式 设计模式的感念 设计模式的应用 工厂设计模式的产生 工...

  • JavaJavascript基础进阶(十七)JS中常用的设计模式

    单利设计模式、构造原型设计模式、发布订阅设计模式、promise设计模式 单利模式 构造原型设计模式 最贴近OOP...

  • 设计模式 - 目录

    设计模式01 - 单例模式 设计模式02 - 工厂模式 设计模式03 - 建造者模式 设计模式04 - 适配器模式...

  • 第1章 设计模式概述

    一、设计模式的概念 二、设计模式的历史 三、设计模式的要素 四、设计模式的分类 ■ 创建型设计模式 ■ 结构型设计...

  • iOS设计模式(3)适配器模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(4)抽象工...

网友评论

    本文标题:设计模式

    本文链接:https://www.haomeiwen.com/subject/ztxdsxtx.html