设计模式部分总结
来源:互联网 发布:淘宝 管控 临时下架 编辑:程序博客网 时间:2024/06/15 15:31
设计模式部分总结
1.简单工厂模式
public abstract class Animal {
public abstract void eat();
}
public class Cat extends Animal{
public void eat(){
System.out.println("猫吃鱼");
}
}
public class Dog extends Animal{
public void eat(){
System.out.println("狗吃肉");
}
}
public class AnimalFactory{
private AnimalFactory(){
}
public static Animal createAnimal(String type){
if("dog".equals(type)){
return new Dog();
}else if("cat".equals(type)){
return new Cat();
}else{
return null;
}
}
}
public class AnimalDemo{
public static void main(String[] args){
Animal a = AnimalFactory.createAnimal("dog");
a.eat();
a = AnimalFactory.createAnimal("cat");
a.eat();
a = AnimalFactory.ctrateAnimal("pig");
if(a != null){
a.eat();
}else{
System.out.println("暂时不提供这种动物!");
}
}
}
2.单例模式
A:单例模式的思想
保证类在内存中只有一个对象
B:如何实现类在内存中只有一个对象?
a:把构造方法私有
b:在成员位置自己创建一个对象
c:通过一个公共的方法提供访问
C:饿汉式(不会出问题的单例模式)
public class Student {
private Student(){
}
private static Student s = new Student();
public static Student getStudent(){
return s;
}
}
public calss StudentDemo{
public static void main(String[] args){
Student s1 = Student.getStudent();
Student s2 = Student.getStudent();
System.out.println(s1 == s2);//true
}
}
D:懒汉式(可能存在线程安全问题)
public calss Student{
private Student(){
}
private static Student s = null;
public synchronized static Student getStudent(){
if(t == null){
t = new Student();
}
return t;
}
}
public class StudentDemo{
public static void main(String[] args){
Student s1 = Student.getStudent();
Student s1 = Student.getStudent();
System.out.println(s1 == s2);//true
}
}
1.简单工厂模式
public abstract class Animal {
public abstract void eat();
}
public class Cat extends Animal{
public void eat(){
System.out.println("猫吃鱼");
}
}
public class Dog extends Animal{
public void eat(){
System.out.println("狗吃肉");
}
}
public class AnimalFactory{
private AnimalFactory(){
}
public static Animal createAnimal(String type){
if("dog".equals(type)){
return new Dog();
}else if("cat".equals(type)){
return new Cat();
}else{
return null;
}
}
}
public class AnimalDemo{
public static void main(String[] args){
Animal a = AnimalFactory.createAnimal("dog");
a.eat();
a = AnimalFactory.createAnimal("cat");
a.eat();
a = AnimalFactory.ctrateAnimal("pig");
if(a != null){
a.eat();
}else{
System.out.println("暂时不提供这种动物!");
}
}
}
2.单例模式
A:单例模式的思想
保证类在内存中只有一个对象
B:如何实现类在内存中只有一个对象?
a:把构造方法私有
b:在成员位置自己创建一个对象
c:通过一个公共的方法提供访问
C:饿汉式(不会出问题的单例模式)
public class Student {
private Student(){
}
private static Student s = new Student();
public static Student getStudent(){
return s;
}
}
public calss StudentDemo{
public static void main(String[] args){
Student s1 = Student.getStudent();
Student s2 = Student.getStudent();
System.out.println(s1 == s2);//true
}
}
D:懒汉式(可能存在线程安全问题)
public calss Student{
private Student(){
}
private static Student s = null;
public synchronized static Student getStudent(){
if(t == null){
t = new Student();
}
return t;
}
}
public class StudentDemo{
public static void main(String[] args){
Student s1 = Student.getStudent();
Student s1 = Student.getStudent();
System.out.println(s1 == s2);//true
}
}
0 0
- 设计模式部分总结
- 《大话设计模式》部分模式总结(一):
- 《大话设计模式》部分模式总结(二)
- 《大话设计模式》部分模式总结(三)
- 学习设计模式笔记(一)部分总结
- 学习设计模式笔记(二)部分总结
- 大话设计模式总结【第一遍】(部分1)
- 部分J2EE设计模式
- 设计模式感悟(部分)
- 设计模式:第一部分
- iOS-部分设计模式
- 部分设计模式介绍
- 设计模式部分整理
- 《设计模式解析》第三部分 设计模式
- 《设计模式解析》第三部分 设计模式
- 设计模式 ----- 设计模式总结
- Java基础部分设计模式
- 设计模式设计程序部分例子
- 设置UIlabel偏移,UITextField偏移 UIEdgeInsets 说明 设置 UILabel 和 UITextField 的 Padding 或 Insets
- could not initialize proxy - no Session
- 面相对象案例总结
- MD5总结
- Android启动过程的底层实现
- 设计模式部分总结
- 【译】Android应用架构
- jquery插件
- Java获取当前日期的前一个月,前一天的时间
- Window ferformance toolkit 学习
- Android 4.4.2 - java.lang.RuntimeException: Performing stop of activity that is not resumed
- iOS 数字保留两位小数
- 网络编程部分案例总结
- debian安装rz sz