设计模式:
通过很多东西抽取出来的一个模型
设计模式分类:
创建型:创建对象,工厂模式,单例模式
结构型:对象间的关系,装饰模式
AnimalFactory
AnimalTest
工厂方法模式
AnimalTest
结果:
其它模式就不多做笔记。。。重在理解。。。
通过很多东西抽取出来的一个模型
设计模式分类:
创建型:创建对象,工厂模式,单例模式
结构型:对象间的关系,装饰模式
行为型:对象能够做什么,魔板模式
简单的工厂模式:
package fanctory;
public abstract class Animal {
public abstract void eat();
}
package fanctory;
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("猫吃鱼。。。。");
}
}
package fanctory;
public class Dog extends Animal{
@Override
public void eat() {
System.out.println("狗吃骨头");
}
}
AnimalFactory
package fanctory;
public class AnimalFactory {
private AnimalFactory() {
// TODO Auto-generated constructor stub
}
public static Dog getDog(){
return new Dog();
}
public static Cat getCat(){
return new Cat();
}
public static Animal creatAnimal(String type){
if("dog".equals(type)){
return new Dog();
}else if("cat".equals(type)){
return new Cat();
}else{
return null;
}
}
}
AnimalTest
package fanctory;
/**
*
* @author Angus
* 工厂模式
*/
public class AnimalTest {
public static void main(String[] args) {
// Dog d = new Dog();
// d.eat();
//可以考虑别人来创建对象,既工厂创建。。。
Dog d = AnimalFactory.getDog();
d.eat();
Cat c = AnimalFactory.getCat();
c.eat();
//这样基本的工厂模式完成。。
//但是发现如果有新的动物需要添加,我们就要新建一个类,修改工厂类,添加功能
Animal a = AnimalFactory.creatAnimal("dog");
a.eat();
}
}
工厂方法模式
package factory2;
public abstract class AnimalFactory {
public abstract Animal creatAnimal();
}
package factory2;
public class DogFactory extends AnimalFactory{
@Override
public Animal creatAnimal() {
return new Dog();
}
}
package factory2;
public class CatFacttory extends AnimalFactory{
@Override
public Animal creatAnimal() {
// TODO Auto-generated method stub
return new Cat();
}
}
AnimalTest
package factory2;
/**
*
* @author Angus
* 工厂方法模式
* 这样没添加一个动物 只需要增加一个动物类和一个动物工厂类 不会修改代码。。
* java思想:扩展不修改。。
*/
public class AnimalTest {
public static void main(String[] args) {
//狗
AnimalFactory af = new DogFactory();
Animal a = af.creatAnimal();
a.eat();
//猫
af = new CatFacttory();
a = af.creatAnimal();
a.eat();
}
}
单例模式
饿汉式:
package single;
/**
* 单例模式
* @author Angus
* 在内存中只存在一个对象。。。
* 1:外界不能随意创建对象,私有构造方法
* 2;类本身创建一个对象
* 3:通过公共的方式给别人使用
*/
public class SingleDemo {
public static void main(String[] args) {
Student s1 = Student.getStudent();
Student s2 = Student.getStudent();
System.out.println(s1==s2); //true
s1.show();
s2.show();
}
}
/*
* 饿汉式
*/
class Student{
//私有构造方法 这样就无法new对象
private Student(){
}
//私有静态防止外部使用
private static Student s = new Student();
//公共方法使用
public static Student getStudent(){
return s;
}
public void show(){
System.out.println("show");
}
}
结果:
懒汉式:
package single;
/**
* 单例模式
* @author Angus
* 在内存中只存在一个对象。。。
* 1:外界不能随意创建对象,私有构造方法
* 2;类本身创建一个对象
* 3:通过公共的方式给别人使用
*/
public class SingleDemo {
public static void main(String[] args) {
Student s1 = Student.getStudent();
Student s2 = Student.getStudent();
System.out.println(s1==s2); //true
s1.show();
s2.show();
}
}
/*
* 懒汉式
* 延迟加载,
* 但是多线程会有安全问题,所以加锁
*/
class Student{
//私有构造方法 这样就无法new对象
private Student(){
}
//私有静态防止外部使用
private static Student s = null;
//公共方法使用
public synchronized static Student getStudent(){
if(s ==null){
s = new Student();
}
return s;
}
public void show(){
System.out.println("show");
}
}
其它模式就不多做笔记。。。重在理解。。。
至此java基础复习完毕。。。。
未来三个月到四个月内复习完j2EE所有内容。。