Big ball of mud. , . , . , .
Software Bloat. λ , ó ( , ), . , , .
Yo-Yo problem. - , , . , . -.
Magic Button. , , , .
Magic Number. , .
Gas Factory. .
Analiys paralisys. , , , .
Interface Bloat. , , .
Smoke And Mirrors. , , , . .
Improbability Factor. , . , - , . ( ), .
reeping featurism. , , , , . , , .
Accidental complexity. , . .
Ambiguous viewpoint. - . . .
|
|
Boat anchor. , . , . ,, - . .
Busy spin. , , . , . . , .
Caching Failure. (bug), () , , . , , .
5
A
. .
1. , , . .
2. , , . .
3. . .
4. , , . .
5. , , , , . .
6. , , , . .
5
5.1.
?
1) Factory;
2) Prototype;
3) Builder;
4) Singleton.
5.2.
? ( )
1) Factory;
2) Command;
3) Strategy;
4) Singleton.
5.3.
?
1) Visitor;
2) Composite;
3) Prototype;
4) Observer.
5.4.
, ?
1) Strategy;
2) Decorator;
3) Template Method;
4) Visitor.
5.5.
.
|
|
1) Adapter;
2) Decorator;
3) Proxy;
4) Bridge.
, . . Java : , , , ( ) . , , Cloneable Serializable, .
public abstract, public, static final, . , implements, . , , .
, .
:
[ public ] interface [ extends 1, 2,, N] {
/* */ }
:
/* # 1: : LineGroup.java, Shape.java */
package chapt06;
public interface LineGroup {
// public abstract
double getPerimeter(); //
}
package chapt06;
public interface Shape extends LineGroup {
//int id; // ,
//void method(){} /* ,
! */
double getSquare(); //
}
' I ', Shape IShape.
, Shape, . getPerimeter() getSquare().
public, , , . .
Java , C++ . .
:
[] class implements 1, 2,, N {
/* */ }
1, 2,, N . , , , . , . , , abstract.
/* # 2: : Rectangle.java */
package chapt06;
public class Rectangle implements Shape {
private double a, b;
public Rectangle(double a, double b) {
this. a = a;
this. b = b;
}
//
public double getSquare() { //
return a * b;
}
//
public double getPerimeter() {
return 2 * (a + b);
}
}
/* # 3: : Circle.java */
package chapt06;
public class Circle implements Shape {
private double r;
public Circle(double r) {
this. r = r;
}
public double getSquare() { //
return Math.PI * Math.pow(r, 2);
}
public double getPerimeter() {
return 2 * Math.PI * r;
|
|
}
}
/* # 4: : Triangle.java */
package chapt06;
/* getSquare() */
public abstract class Triangle implements Shape {
private double a, b, c;
public Triangle(double a, double b, double c) {
this. a = a;
this. b = b;
this. c = c;
}
public double getPerimeter() {
return a + b + c;
}
}
/* # 5: : Runner.java */
package chapt06;
public class Runner {
public static void printFeatures(Shape f) {
System. out. printf(":%.2f : %.2f%n",
f.getSquare(), f.getPerimeter());
}
public static void main(String[] args) {
Rectangle r = new Rectangle(5, 9.95);
Circle c = new Circle(7.01);
printFeatures (r);
printFeatures (c);
}
}
:
:49,75 : 29,90
:154,38 : 44,05
Runner printFeatures(Shape f), , . , , (
r). printFeatures() ? , Shape. , , , .
ShapeCreator , , . , . , . , , .
/* # 6: : ShapeCreator.java */
package chapt06;
public class ShapeCreator {
public static void main(String[] args) {
Shape sh; /* */
Rectangle re = new Rectangle(5, 9.95);
sh = re;
sh.getPerimeter(); // Rectangle
Circle cr = new Circle(7.01);
sh = cr; //
sh.getPerimeter(); // Circle
// cr=re; // !
}
}
, , . :
Circle c = new Rectangle(1, 5);
Java , (unnamed default package), package . package , , , .. , , . package . :
package chapt06;
chapt06. : chapt06.Student. :
package chapt06.bsu;
, Java, :
package ();
import ();
(public) ()
|
|
()
:
- ,
www.bsu.by by.bsu;
, : eun;
, .
, . . , , . : , , .
by.bsu.eun |
by.bsu.eun.administration.constants |
by.bsu.eun.administration.dbhelpers |
by.bsu.eun.common.constants |
by.bsu.eun.common.dbhelpers.annboard |
by.bsu.eun.common.dbhelpers.courses |
by.bsu.eun.common.dbhelpers.guestbook |
by.bsu.eun.common.dbhelpers.learnres |
by.bsu.eun.common.dbhelpers.messages |
by.bsu.eun.common.dbhelpers.news |
by.bsu.eun.common.dbhelpers.prepinfo |
by.bsu.eun.common.dbhelpers.statistics |
by.bsu.eun.common.dbhelpers.subjectmark |
by.bsu.eun.common.dbhelpers.subjects |
by.bsu.eun.common.dbhelpers.test |
by.bsu.eun.common.dbhelpers.users |
by.bsu.eun.common.menus |
by.bsu.eun.common.objects |
by.bsu.eun.common.servlets |
by.bsu.eun.common.tools |
by.bsu.eun.consultation.constants |
by.bsu.eun.consultation.dbhelpers |
by.bsu.eun.consultation.objects |
by.bsu.eun.core.constants |
by.bsu.eun.core.dbhelpers |
by.bsu.eun.core.exceptions |
by.bsu.eun.core.filters |
by.bsu.eun.core.managers |
by.bsu.eun.core.taglibs |
. 6.1.
. :
// # 7: : CommonObject.java
package by.bsu.eun.objects;
public class CommonObject implements Cloneable {
public CommonObject() {
super ();
}
public Object clone()
throws CloneNotSupportedException {
return super. clone();
}
}
, by.bsu.eun.objects. , , CommonObject.java objects, , , bsu, . , , . , : by.bsu.eun.objects.CommonObject. , import. :
import by.bsu.eun.objects.CommonObject;
import by.bsu.eun.objects.*;
, , . import , .
:
// # 8: : UserStatistic.java
package by.bsu.eun.usermng;
public class UserStatistic
extends by.bsu.eun.objects.CommonObject {
private long id;
private int mark;
public UserStatistic() {
super ();
}
public long getId() {
return id;
}
public void setId(long id) {
this. id = id;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this. mark = mark;
}
}
. , .
// # 9: : CreatorStatistic.java
package by.bsu.eun.actions;
import by.bsu.eun.objects.CommonObject;
import by.bsu.eun.usermng.UserStatistic;
public class CreatorStatistic {
public static UserStatistic createUserStatistic(long id) {
UserStatistic temp = new UserStatistic();
temp.setId(id);
// id
int mark = ;
temp.setMark(mark);
return temp;
}
public static void main(String[] args) {
UserStatistic us = createUserStatistic (71);
System.out.println(us.getMark());
}
}
, , , (unnamed). unnamed- . , .
|
|
, , .
// # 10: : ImportDemo.java
package chapt06;
import static java.lang.Math.*;
public class ImportDemo {
public static void main(String[] args) {
double radius = 3;
System. out. println(2 * PI * radius);
System. out. println(floor (cos (PI /3)));
}
}
, Math.E, :
import static java.lang.Math.E;
import static java.lang.Math.cos; //
, .
Java () , , , . , . , , -. ( ). . .
, . ( ) . , , , . , .
, , . ( ) . static. - , .
, static, . , . -.
(inner)
(inner) . , . ( ) (enclosing) . , , :
public class Ship {
//
// abstract, final, private, protected -
publicclass Engine { //
//
public void launch() {
System. out. println(" ");
}
} //
public void init() { //
//
Engine eng = new Engine();
eng.launch();
}
}
Engine Ship - , Ship. () :
Ship.Engine obj = new Ship().new Engine();
. private, - . obj, , . . protected , .
, , Ship$Engine.class.
, . , (final static). , . :
public class WarShip extends Ship {
protected class SpecialEngine extends Engine {}
}
( extends .), , .
public class Motor extends Ship.Engine {
public Motor(Ship obj) {
obj. super ();
}
}
Motor Ship, Engine, Motor.
, .
final, abstract, private, protected, public.
- .
/* # 11: : Student.java: AnySession.java */
package chapt06;
public class Student {
private int id;
private ExamResult[] exams;
public Student(int id) {
this. id = id;
}
private class ExamResult { //
private String name;
private int mark;
private boolean passed;
public ExamResult(String name) {
this. name = name;
passed = false;
}
public void passExam() {
passed = true;
}
public void setMark(int mark) {
this. mark = mark;
}
public int getMark() {
return mark;
}
public int getPassedMark() {
final int PASSED_MARK = 4; //
return PASSED_MARK;
}
public String getName() {
return name;
}
public boolean isPassed() {
return passed;
}
} //
public void setExams(String[] name, int [] marks) {
if (name.length!= marks.length)
throw new IllegalArgumentException();
exams = new ExamResult[name.length];
for (int i = 0; i < name.length; i++) {
exams[i] = new ExamResult(name[i]);
exams[i].setMark(marks[i]);
if (exams[i].getMark() >= exams[i].getPassedMark())
exams[i].passExam();
}
public String toString() {
String res = ": " + id + "\n";
for (int i = 0; i < exams.length; i++)
if (exams[i].isPassed())
res += exams[i].getName() + " \n";
Else
res += exams[i].getName() + " \n";
return res;
}
}
package chapt06;
public class AnySession {
public static void main(String[] args) {
Student stud = new Student(822201);
String ex[] = {"Me",""};
int marks[] = { 2, 9 };
stud.setExams(ex, marks);
System. out. println(stud);
}
}
:
: 822201
Me
( ExamResult), Student. ExamResult Student, , .
. , . , , . , , static, .
/* # 12: , : TeacherLogic.java*/
package chapt06;
public abstract class AbstractTeacher {
private int id;
public AbstractTeacher(int id) {
this. id = id;
}
public abstract boolean excludeStudent(String name);
}
package chapt06;
public class Teacher extends AbstractTeacher {
public Teacher(int id) {
super (id);
}
public boolean excludeStudent(String name) {
return false;
}
}
package chapt06;
public class TeacherCreator {
public TeacherCreator(){}
public AbstractTeacher createTeacher(int id) {
//
class Dean extends AbstractTeacher {
Dean(int id){
super (id);
}
public boolean excludeStudent(String name) {
if (name!= null) {
//
return true;
}
else return false;
}
} //
if (isDeanId (id))
return new Dean(id);
else return new Teacher(id);
}
private static boolean isDeanId(int id) {
//
return (id == 777);
}
}
package chapt06;
public class TeacherLogic {
public static void excludeProcess(int deanId,
String name) {
AbstractTeacher teacher =
new TeacherCreator().createTeacher(deanId);
System. out. println(": " + name
+ " :" + teacher.excludeStudent(name));
}
public static void main(String[] args) {
excludeProcess (700, "");
excludeProcess (777, "");
}
}
:
: :false
: :true
Dean createTeacher(int id), , . , , .
TeacherCreator$1Dean, . 1 , .
(nested)
, .
-, .
static, (nested). , . , , .
, . . , .
/* # 13: : Ship.java: RunnerShip.java */
package chapt06;
public class Ship {
private int id;
// abstract, final, private, protected -
public static class LifeBoat {
public static void down() {
System. out. println(" !");
}
public void swim() {
System. out. println(" ");
}
}
}
package chapt06;
public class RunnerShip {
public static void main(String[] args) {
//
Ship.LifeBoat.down();
//
Ship.LifeBoat lf = new Ship.LifeBoat();
//
lf.swim();
}
}
. lf .
, , . , , .
/* # 14: : Faculty.java: University.java */
package chapt06;
public interface University {
int NUMBER_FACULTY = 20;
class LearningDepartment { // static
public int idChief;
public static void assignPlan(int idFaculty) {
//
}
public void acceptProgram() {
//
}
}
}
.
(anonymous)
() , .. , , , , . new.
, , () . , , - ( ) .
. , .
/* # 15: : TypeQuest.java: RunnerAnonym.java */
package chapt06;
public class TypeQuest {
private int id = 1;
public TypeQuest() {
}
public TypeQuest(int id) {
this. id = id;
}
public void addNewType() {
//
System. out. println(
" ");
}
}
package chapt06;
public class RunnerAnonym {
public static void main(String[] args) {
TypeQuest unique = new TypeQuest() { // #1
public void addNewType() {
//
System. out. println(
" ");
}
}; //
unique.addNewType();
new TypeQuest(71) { // #2
private String name = "Drag&Drop";
public void addNewType() {
// #2
System. out. println(" " + getName());
}
String getName() {
return name;
}
}.addNewType();
TypeQuest standard = new TypeQuest(35);
standard.addNewType();
}
}
: