Skip to content

Apply design pattern to some classes - #29

Open
terry2511 wants to merge 64 commits into
tonysparks:masterfrom
terry2511:master
Open

Apply design pattern to some classes#29
terry2511 wants to merge 64 commits into
tonysparks:masterfrom
terry2511:master

Conversation

@terry2511

Copy link
Copy Markdown
Contributor

We apply design pattern to some classes.

cokacider and others added 30 commits May 2, 2018 11:43
Refactoring Operation
- Introduce Explaining Variable
- Extract Method

Refactoring target
- updateCameraForRoamingMovements(TimeStep timeStep)

For readability
To remove duplicated codes
Target : Tri class in seventh.math package
Reason : An inheritance should from an IS-A relationship, but tri(if i
guessed correctly, the meaning of this is triple) is not a pair.
Refactoring Operation:
- Replace Magic Number with Symbolic Constant

Refactoring Target:
- the number of array of Button and Label

because the numeric value(7) had no obvious meaning,
I replace the value with constant
Refactoring Operation:
- Extract Method

Refactoring Target:
- createUI method

I think it needs to isolate independent parts of code.
createTitleLabel -> setupTitleLabel
createCancelButton -> setupCancelButton
ProgressBarView
I extracted the source code that needs explanation.
Refactoring Operation:
- Extract Method

Refactoring Target:
- refreshButtons method

'refresh' contains 'initializing' and 'setup'.
it is needed to separate these two different functions.
Refactoring Operation:
- Extract Method

Refactoring Target:
- refreshButtons method
- setupWeaponClasses method

to remove duplicated codes
ease to change the weapon types
Refactoring operation:
-Replace Data Value with Object
-Form Template Method

Refactoring target:
-getClassDescription method

to reduce duplicated codes
if new descriptions of weapons are added, you can simply add a new
subclass without touching the existing code
2.ClienteGame.java
	public void applyGameUpdate()
  Game.java
    public void update()
    public boolean playerSwitchedTeam()
  PlayerAwardSystem.java
  	public void roundEnded()
  	public void addKill()
3.too many functions, duplicated  	codes in one method
Target : ClientMain class in seventh
Reason : I extracted the source codes by function.
bananapizza
1.
Refactoring Operation:
Extract Method (contactDedicatedServer method)

Refactoring Target:
init method in GameServer class of seventh.server package

Reason:
to satisfy SRP

2.
Refactoring Operation:
Extract Method (printHeader,pringBody,printFooter method)

Refactoring Target:
dump method in BitPacker class of harenet package

Reason:
divide dump into 3 parts to satisfy SRP
printFooter was duplicate code (dump,dumpBytes)

3.
Refactoring Operation:
Extract Method (removeDisconnectedPeer)

Refactoring Target:
disconnect method in Host class of harenet package

Reason:
decouple removing peer from disconnect to satisfy SRP
2.FastMath.java
	static public final int random()
	static public final float random()
	public static float a_sqrt
	public static float a_isqrt
3.placing the result of an expression in a local variable for later use
in the code.
Refactoring Seventh.game.type.obj.ObjectiveScript.java
Refactoring WeaponClassDialog, CameraController classes
2.ClienteGame.java
	public void applyGameUpdate()
  Game.java
    public void update()
    public boolean playerSwitchedTeam()
  PlayerAwardSystem.java
  	public void roundEnded()
  	public void addKill()
3.too many functions, duplicated  	codes in one method


1.Replace Temp with Query

2.FastMath.java
	static public final int random()
	static public final float random()
	public static float a_sqrt
	public static float a_isqrt
3.placing the result of an expression in a local variable for later use
in the code.
aikaran and others added 24 commits May 8, 2018 14:43
Target Class : seventh.shared Arrays class
Reason : Arrays class implement quicksort and using it.
1. Name of applied design pattern
- Template Method Pattern

2. target
- methods in World class
getNorthZone
getNorthWestZone
getNorthEastZone
getEastZone
getSouthZone
getSouthWestZone
getSouthEastZone
getWestZone

3. reason
- to remove duplicated codes.
1. Name of applied design pattern
- Abstract Factory Pattern

2. target
- add TeamStrategyFactory abstract class,
DefaultAISystemTeamStrategyFactory class.
- delete switch context in init(GameInfo) method in DefaultAISystem
class and add factory context.

3. reason
- make it easy to add another AISystem and maintain AIStrategy codes.
seventh.shared.Arrays class
separate implementation of sorting method from Arrays
Target : seventh.shared.Arrays class
Reason : Factory Method pattern can create objects with common ancestor
Target : seventh.shared.Arrays and, seventh.shared.SortStrategyFactory
Reason : Two more factories are not necessary. Only one factory is
sufficient.
Target : seventh/ai/basic/group/AIGroupAction
		 seventh/ai/basic/group/AIGroupAction
		 seventh/ai/basic/group/AIGroupAction
		 seventh/ai/basic/group/AIGroupAction
		 seventh/ai/basic/group/AIGroupAction
		 seventh/ai/basic/group/AIGroupAction
Reason : Separate implementation of start and getAction method from
AIGroupAction Class for avoiding duplication
Target : seventh/ai/basic/group/AIGroupAction
		 seventh/ai/basic/group/AIGroupAttackAction
		 seventh/ai/basic/group/AIGroupDefendAction
		 seventh/ai/basic/group/AttackGetAction
		 seventh/ai/basic/group/AttackStart
		 seventh/ai/basic/group/DefendGetAction
		 seventh/ai/basic/group/DefendStart
		 seventh/ai/basic/group/GetAction
		 seventh/ai/basic/group/Start

Reason : Separate implementation of start and getAction method from
AIGroupAction Class for avoiding duplication
Target : seventh/ai/basic/group/AIGroupAction
		 seventh/ai/basic/group/AIGroupAttackAction
		 seventh/ai/basic/group/AIGroupDefendAction
		 seventh/ai/basic/group/AttackGetAction
		 seventh/ai/basic/group/AttackStart
		 seventh/ai/basic/group/DefendGetAction
		 seventh/ai/basic/group/DefendStart
		 seventh/ai/basic/group/GetAction
		 seventh/ai/basic/group/Start

Reason : Separate implementation of start and getAction method from
AIGroupAction Class for avoiding duplication
Target : 
       seventh/ai/basic/FeelSensor
       seventh/ai/basic/Sensors
       seventh/ai/basic/SightSensor
       seventh/ai/basic/SoundSensor
       seventh/ai/basic/SensorFactory
Reason : For useful skill to prepare for changes in objects
template method pattern
strategy pattern
Design Pattern: Factory Method Pattern
Apply design patterns - Observer, Template Method, Factory Method
@tonysparks

Copy link
Copy Markdown
Owner

Please fix formatting issues (mainly tab -> 4 spaces).

}
System.out.println();
System.out.println("+--------------- ------------- ------- ------ --- -- -- - -- -- --");
StatementFactory statementFactory = new StatementFactory();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot of extra code for not a lot of benefit. You want to create Factories when there a lot of different types or configurations.

This also adds in an extra layers of complexity -- instead of reading the code inlined, the reader has to now understand StatementFactory, Statement and the concrete classes of BitStatement and ByteStatement (along with grappling the inheritance).


import seventh.shared.TimeStep;

public class SensorFactory {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very similar to the Statement comment -- I don't see the added benefit of SensorFactory. The Sensors class already houses all available Sensors -- which negates the need for this class.

int adjacentZoneY = bounds.y - (bounds.height/2 + fuzzy + minDistance);
Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY);
return adjacentZone;
return new NorthZone().getAdjacentZone(bounds, fuzzy, minDistance);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, breaking the code up into separate classes makes the code more readable. However, this does create more objects (and these methods are called frequently any given game frame) which creates a lot of garbage on the heap -- which in turn causes the Garbage Collector to do more work (which increases the game pauses).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants