
00:00:00
外卖卷:非常划算 扫码领劵 省点小钱钱
设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了重用代码、让代码更容易被他人理解、保证代码可靠性,以及更加简单方便的复用成功的设计和体系结构。
设计模式确定了所包含的类和实例,它们的角色、协作方式以及职责分配。每一个设计模式都集中于一个特定的面向对象设计问题或设计要点,描述了什么时候使用它,在另一些设计约束条件下是否还能使用,以及使用的效果和如何取舍。按照设计模式的目的可以分为三大类。
创建型模式与对象的创建有关;结构型模式处理类或对象的组合;行为型模式对类或对象怎样交互和怎样分配职责进行描述。
这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用 new 运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加灵活。
创建型模式关注点是如何创建对象,其核心思想是要把对象的创建和使用相分离,这样使得两者能相对独立地变换。
这些设计模式关注类和对象的组合。继承的概念被用来组合接口和定义组合对象获得新功能的方式。
这些设计模式特别关注对象之间的通信。
意图:
适用性:
实现代码:
/**
* 简单工厂模式
*/
public class SimpleFactory {
public static void main(String[] args) {
Product productA = Factory.createProduct("A");
productA.info();
Product productB = Factory.createProduct("B");
productB.info();
}
}
class Factory {
public static Product createProduct(String type) {
Product product = null;
switch(type) {
case "A":
product = new ProductA();
break;
case "B":
product = new ProductB();
break;
default:
System.out.println("没有 " + type + " 类型的产品!");
}
return product;
}
}
abstract class Product {
public abstract void info();
}
class ProductA extends Product {
@Override
public void info() {
System.out.println("产品的信息:A");
}
}
class ProductB extends Product {
@Override
public void info() {
System.out.println("产品的信息:B");
}
}/**
* 工厂方法模式
*/
public class FactoryMethod {
public static void main(String[] args) {
Factory factoryA = new FactoryA();
Product productA = factoryA.createProduct();
productA.info();
Factory factoryB = new FactoryB();
Product productB = factoryB.createProduct();
productB.info();
}
}
interface Factory {
Product createProduct();
}
class FactoryA implements Factory {
@Override
public Product createProduct() {
return new ProductA();
}
}
class FactoryB implements Factory {
@Override
public Product createProduct() {
return new ProductB();
}
}
interface Product {
void info();
}
class ProductA implements Product {
@Override
public void info() {
System.out.println("产品的信息:A");
}
}
class ProductB implements Product {
@Override
public void info() {
System.out.println("产品的信息:B");
}
}意图:
适用性:
实现代码:
/**
* 抽象工厂模式
*/
public class AbstractFactory {
public static void main(String[] args) {
Factory factory1 = new Factory1();
ProductA productA1 = factory1.createProductA();
productA1.info();
ProductB productB1 = factory1.createProductB();
productB1.info();
Factory factory2 = new Factory2();
ProductA productA2 = factory2.createProductA();
productA2.info();
ProductB productB2 = factory2.createProductB();
productB2.info();
}
}
interface Factory {
ProductA createProductA();
ProductB createProductB();
}
class Factory1 implements Factory {
@Override
public ProductA createProductA() {
return new ProductA1();
}
@Override
public ProductB createProductB() {
return new ProductB1();
}
}
class Factory2 implements Factory {
@Override
public ProductA createProductA() {
return new ProductA2();
}
@Override
public ProductB createProductB() {
return new ProductB2();
}
}
interface ProductA {
void info();
}
interface ProductB {
void info();
}
class ProductA1 implements ProductA {
@Override
public void info() {
System.out.println("产品的信息:A1");
}
}
class ProductA2 implements ProductA {
@Override
public void info() {
System.out.println("产品的信息:A2");
}
}
class ProductB1 implements ProductB {
@Override
public void info() {
System.out.println("产品的信息:B1");
}
}
class ProductB2 implements ProductB {
@Override
public void info() {
System.out.println("产品的信息:B2");
}
}意图:
适用性:
意图:
适用性:
意图:
适用性:
单利模式有饿汉式和懒汉式两种实现方式,饿汉式在类加载时就创建对象,可能存在资源资源浪费问题。懒汉式存在线程安全问题。
单例模式的实现步骤:
public class SingleTon01 {
public static void main(String[] args) {
GirlFriend gf = GirlFriend.getInstance();
System.out.println(gf);
}
}
class GirlFriend {
private String name;
private GirlFriend(String name) {
this.name = name;
}
private static GirlFriend gf = new GirlFriend("girl");
public static GirlFriend getInstance() {
return gf;
}
@Override
public String toString() {
return "GirlFriend{" +
"name='" + name + '\'' +
'}';
}
}public class SingleTon02 {
public static void main(String[] args) {
Cat cat = Cat.getInstance();
System.out.println(cat);
}
}
class Cat {
private String name;
private Cat(String name) {
this.name = name;
}
private static Cat cat;
public static Cat getInstance() {
if (cat == null) {
cat = new Cat("jack");
}
return cat;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
'}';
}
}意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
意图:
适用性:
评论